Stjerneklar
Stjerneklar

Reputation: 67

Get multiple tables as separate objects in one query

I am making an interface where a choice in the first dropdown decides what options are available in 6 subsequent dropdowns.

Each dropdown gets its value and displayname from a table like so:

        $arr = array();
        $rs = mysql_query("SELECT * FROM sn_roles");
        while($obj = mysql_fetch_object($rs)) {
            $arr[] = $obj;
        }
        $response = '{"roles":'.json_encode($arr).'}';
        echo $response;

And gets json encoded.

What i want to do is select id and name from multiple tables and get a json object collection per table.

Upvotes: 0

Views: 371

Answers (2)

Prashant Shukla
Prashant Shukla

Reputation: 329

you need to use ajax for each drop down option and store returned json object for particular table to a javascript object

     <select name="selector1" id="selector1" onChange="getoptions();" 
            <option value="1">--Select--</option>
            <option value="2">option1</option>
            <option value="3">option2</option>
            <option value="4">option3</option>
   </select>
   <select name="selector2" id="selector2">
    </select>
    <select name="selector3" id="selector3">
    </select>

and javascript code for the dropdown would be like

function getoptions(){

$('#selector2').html('<option value="">--Select--</option>');
$('#selector3').html('<option value="">--Select--</option>');
var selector1 = $('#selector1').val();
jQuery.get('getdropAjax.php', {'_action_':'GetDropValue', Selector1val : selector1 }, function(r) {
    for (var i in r.forselector2)
    {
        $('#selector2').append('<option value="'+i+'">'+r.forselector2[i]+'</option>');
    }
    for (var i in r.forselector3)
    {
        $('#selector3').append('<option value="'+i+'">'+r.forselector3[i]+'</option>');
    }
}, 'json');
}

And your php code for this will be like

<?php
$data = array();
switch ( $_GET['_action_'] ){
case 'GetDropValue':
$arr1 = array();
    $rs = mysql_query("SELECT `colname` FROM sn_roles");
    while($obj = mysql_fetch_object($rs)) {
        $arr1[] = $obj->colname;
    }
$data['forselector2'] = $arr1;
$arr2 = array();
    $rs = mysql_query("SELECT `colname` FROM table2");
    while($obj = mysql_fetch_object($rs)) {
        $arr2[] = $obj->colname;
    }
$data['forselector2'] = $arr2;
return json_encode($data);
?>

Upvotes: 1

Cups
Cups

Reputation: 6896

How about thinking of it like this:

function getSnDataJSON($table_name){

       $arr = array();
        $rs = mysql_query("SELECT id, name FROM sn_" . $table_name);
        while($obj = mysql_fetch_object($rs)) {
            $arr[] = $obj;
        }
        $response = '{"'.$table_name.'":'.json_encode($arr).'}';
        return $response;

}

echo getSnDataJSON('roles');

Encapsulate the fetching of things from you db in a function, for this to work as is, you'd need to tee up your table names and your form element names:

ie sn_roles in your db becomes roles in your form OR roles in your db stays as roles in your form

Thats one way of thinking about it, the only bit I am unsure of is the quoting of the intial json return.

enter code here$response = '{"'.$table_name.'":'.json_encode($arr).'}';

Upvotes: 0

Related Questions