tags:

views:

101

answers:

2

This is what I have got. It displays just one single record. Can anyone please tell me how can I retrieve multiple records from the table.

$.getJSON('getinfo.php', { id:id }, parseInfo);
function parseInfo(data) {
    $('div#info').html(data.name +': '+ data.title); 
}

The getinfo.php part is as follows:

$id = (isset($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : 0;
$id = trim($id);

$result = mysql_query("SELECT * FROM tab WHERE col=\"$id\" );
while ($row = mysql_fetch_assoc($result)) {
    $test[$id] = array('name' => $row["name"], 'title' => $row["title"]);
}

Many thanks in advance. DJ

A: 

$id never changes, so you keep rewriting the same entry. Did you perhaps want $test[$id] = Array() outside the loop, and $test[$id][] = ... inside?

Ignacio Vazquez-Abrams
Thank you for your quick reply. I'm new to jquery, and I'm wondering how would I be able to retrieve and display that array returned from sql query inside the parseInfo() function, one row at a time.Cheers!
DGT
Use a `for/in` loop. http://w3schools.com/js/js_loop_for_in.asp
Ignacio Vazquez-Abrams
A: 

Maybe you should start by making your page work without javascript & AJAX. What is the purpose of this page? Is $id is a unique/a key in the database?

For me, the AJAX is just a progressive enhancement. The page in my application should working without javascript enabled. When javascript enabled, then I add a fancy features using AJAX & jQuery.

Donny Kurnia
Thanks for your reply, Donny. I got it working now, with some help, of course. Here is the thread: http://stackoverflow.com/questions/2308225/format-json-value-with-a-php-function-and-print-result-inside-a-divid Cheers!
DGT