More help with json
I'm still trying to get the grasp of using JSON with php and jquery.
I have a page which queries a database to get a开发者_StackOverflow中文版ll users who have upcoming birthdays (ignore the sql, its just for this example) The code grabs the database return and puts it into an array, which looks something like this:
Array (
[idnum] => Array (
[0] => 3
[1] => 10
[2] => 74
)
[name] => Array (
[0] => Betty Smith
[1] => Jim Pierce
[2] => Sam Smith
)
)
I want to put this data into some jquery to display to the user in an html select statement or something similar, so that they can make a choice, and then retain an IDnum for future code/ajax.
My code follows, in its entirety:
<?php
session_start();
$sql_server = 'xxxx';
$sql_db = 'xxxx';
$sql_username = 'xxxx';
$sql_password = 'xxxx';
$db = mysql_connect("$sql_server", "$sql_username", "$sql_password") or die ("Error connecting to database.");
mysql_select_db("$sql_db", $db) or die ("Couldn't select the database.");
list($currentdate) = mysql_fetch_row(mysql_query("SELECT currentdate FROM calendar",$db));
/////////////////////////
$result = mysql_query("SELECT IDvalue, name FROM people where birthday => $currentdate",$db);
$rowcheck = mysql_num_rows($result);
//echo $rowcheck;
$birthdays = array();
$i = '0';
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $col => $val){
if ($col == 'IDvalue') {
//print " $val ";
$birthdays['idnum'][$i] = $val;
}
if ($col == 'name') {
$birthdays['name'][$i] = $val;
}
}
$i++;
}
echo "</br></br><input type=\"button\" id=\"showbirthdays\" value=\"Show birthdays\"/>";
?>
<html>
<head>
</head>
<body>
<script src="jquery.js" type ="text/javascript"></script>
<script>
$('#showbirthdays').click(function() {
var jsonobj = <?php echo json_encode($birthdays); ?>;
//alert (jsonobj) // this does not seem to work. just gives [object Object]
// Here's where i'm really lost. I would like to use jquery to display
// a select box that shows the name of birthday people, and uses their IDnum as an identifer for further code.
// some kind of foreach with an html.push, and then append to a div on the page...?
});
</script>
</body>
</html>
}
?>
I would very much like to see exactly how to grab that array data within the jquery, and use it. Or, if someone has a suggestion of a different way to move the data, I'm interested in alternatives.
Edit:
$.each(jsonobj, function(){
//Here `this` will point to each element of the object
alert (this);
});
Works great, gives me output like this: "Betty Smith,Jim Pierce, Sam Smith".
$.each(jsonobj.name, function(){
This also works, and gives me separate output with the names,
Here's what I'm looking for:
var html = [];
html.push('<select name="birthdaynames" id="birthdayselect">')
$.each(jsonobj, function(){
html.push('<option name="asdf" id="this.IDnum">' + this.name + '</option>')
});
html.push('</select>')
$('#showbirthdays').append(html.join(''));
How do I get those specific values using (this)?
If <?php echo json_encode($birthdays); ?>
writes a well form js array or object then you can direct use it as
var jsonobj = <?php echo json_encode($birthdays); ?>;
alert
jsonobj is giving you [[object Object]
because it is a javascript object. You can try to alert jsonobj.length
if it is an array.
You can loop through this object using $.each
loop.
var html = [];
html.push('<select name="birthdaynames" id="birthdayselect">')
$.each(jsonobj, function(){
html.push('<option value="'+this.IDvalue + '">' + this.name + '</option>')
});
html.push('</select>')
$('#showbirthdays').append(html.join(''));
[object Object]
is correct, when you are trying to alert a JavaScript object.
Try to alert(jsonobj.idnum)
or alert(jsonobj.name)
.
To make a <select>
you can use $.each
to loop through the data.
var $sel = $('<select name="birthdaynames" id="birthdayselect">');
$.each(jsonobj.idnum, function(i,v){
var name = jsonobj.name[i];
var $opt = $('<option/>').val(v).text(name);
$sel.append($opt);
});
$('#showbirthdays').append($sel);
You can simply do
jsonobj.your_array_key;
to access the json object data into javascript. If you have any array then just do jquery each with it.
精彩评论