Prev | Current Page 107 | Next

Larry Ullman

"Building a Web Site with Ajax: Visual QuickProject Guide"

mysql_
real_escape_string($_GET[???last_name??™]) . ???%??™ ORDER BY
last_name, first_name???;
$r = mysql_query($q, $dbc);
if (mysql_num_rows($r) > 0) {
134 Appendix
2 Initialize an array that will store the results.
3 Add each record as a new item in the $data array.
4 Use a library to turn the array into JSON format, and
print the results. (See extra bits on page 146.)
$data = array();
while ($row = mysql_fetch_array($r, MYSQL_NUM)) {
$data[] = array (???name??™ => $row[0],
???department??™ => $row[2],
???email??™ => $row[1]);
} // End of WHILE loop.
echo json_encode($data) . ???\n???;
}
mysql_close($dbc);
} // End of $_GET[???last_name??™] IF.
?>
Appendix 135
accessing JSON data
To fi nish changing the search from Chapter 7 to use JSON, you must alter the
search.js fi le. All of the relevant changes go within the handleResponse()
function (although you also have to change the reference to the search_
results_xml.php script earlier in the JavaScript fi le, if you rename that fi le):
1 Retrieve the data in ajax.responseText.
2 Convert the data to an object using eval().
3 Check that some records were returned by looking at the array??™s size.


Pages:
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119