0

I am pretty knew to coding - so I hope my question doesn't seem too stupid. I am trying to create an array for a dropdown-list.

Currently I am using this

$(function(){
     $('.test').editable({    
           type: 'select',
           value: "keine",
           source: [
               {value: 'DD', text: 'DD'},
               {value: 'HEAL', text: 'HEAL'},
               {value: 'TANK', text: 'TANK'},
               {value: 'keine', text: 'keine'}
           ]
      }); 
});

which works perfectly for creating a manual dropdown-list.

But I am searching for a dynamic solution with which I can insert values from a specific table in my sql-database (USING SIMILAR SYNTAX TO THE EXAMPLE ABOVE - maybe using .$fetch['role']. ?).

Looking forward to receiving some suggestions.

1

1 Answer 1

1

Let's say you have a table roles with the roles, and you are using PHP's MySqli methods, then your PHP code could look like this:

<?php
// ...
// ... assuming $mysqli is your connection object:
// ...
$result = $mysqli->query("SELECT role FROM roles ORDER BY role") 
          or die($mysqli->error());

while ($row = $result->fetch_array()) {
    $source[] = (object) array("value" => $row[0], "text" => $row[0]);
}
?>

<script>
    $(function(){
        $('.test').editable({    
            type: 'select',
            value: "keine",
            source: <?=json_encode($source)?>
        }); 
    });
</script>

So this code goes through the results from the database and builds an PHP array which has the structure you need in JavaScript.

Once this variable $source has been filled, it is written to the JavaScript in JSON format, which is (as PHP generates it, with all non-ASCII escaped) compatible with JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.