2

I am using jtables (http://www.jtable.org), but one of the options to create a combobox (dropdown menu) is such:

Branch: {
    title: 'Branch',
    type: 'list',
    options: {
        '1': 'Auckland',
        '2': 'Queensland'
    }
}

I want to be able to use a mysql query (JSON'ed?) for my "options" instead of hardcoding it. Any ideas?

3 Answers 3

3
Branch:{
  title: 'Page Name',
  width: '30%',
  options:  'FieldNameLoader.php?action=Branch',
  list :true
}

if($_GET["action"] == "Branch") {


$result = mysql_query("SELECT * from tblPagelist ORDER BY PageName ASC;");
$rows = array();
while ($row = mysql_fetch_array($result)) {
    $eil = array();
    $eil["DisplayText"] = $row[1];
    $eil["Value"] = $row[0];
    $rows[] = $eil;
    }
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Options'] = $rows;  
print json_encode($jTableResult);    }
Sign up to request clarification or add additional context in comments.

Comments

1

Use PHP's json_encode with mysql_fetch_array

PHP

while($row = mysql_fetch_array($query)) {
  $options[$row['id']] = $row['name'];
}

$options = json_encode($options);

JSON

Branch: {
  title: 'Branch',
  type: 'list',
  options: <?=$options?>
}

2 Comments

doing this doesnt load the page at all.. I even tried putting <?php=$options?> but still nothing.. I checked the PHP and echo'ed the json, it is showing properly, but somehow the jquery plugin is refusing to load. Using options: <?php echo json_encode($options) ?> inside the jquery, the page loads, but the plugin is not showing the options displayed by the json
OK, I was able to finally get it to work using <?php echo json_encode($options) ?> Just forgot to remove the comment of my include.. lol
0

What I do is on the php side I query everything and put it into an array and then echo it to the javascript page:

            Branch: {
                title: 'Branch',
                type: 'list',
                options: <?php echo json_encode($branchArray)?>
            }

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.