tags:

views:

31

answers:

2

this is continued from another question i asked: http://stackoverflow.com/questions/4033116/listing-out-json-data

my search only returns 1 item, im pretty sure the problem lies somewhere in my php, im not too sure if im adding to the array properly, or it could be the javascript wich you can see on the above link, but i doubt it.

my php code:


function mytheme_ajax_response() {
  $search = $_GET["search_text"];
  $result = db_query("SELECT nid FROM {node} WHERE title LIKE '%s%' AND type = 'product_collection'", $search);

  $noder = array();
  while ($record = db_fetch_object($result)) {
 $noder[] = $record;
  }

  $matches = array();
 $i = 0;
  foreach ($noder as $row) {
    $node = node_load($row->nid);
    $termlink = db_fetch_object(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $row->nid));
    $matches[$i]['title'] = $node->title;
 $matches[$i]['link'] = $termlink->tid;
  }
 ++$i;
 $hits = array();
 $hits['matches'] = $matches;
  print json_encode($hits);
  exit();
}
+2  A: 

You appear to be incrementing your $i variable AFTER the foreach loop. Therefore, $i is always 0 throughout your loop, so you are always setting the title and link values for $matches[0].

Adam Raney
whoah, man im stupid, need to eat more fish or something, thanks alot!
Thor
No problem, we all miss the obvious every now and then ;-)
Adam Raney
If this answer satisfies your question please mark it as the answer.
mives
A: 

Try this:

function mytheme_ajax_response() {
  $search = $_GET["search_text"];
  $result = db_query("SELECT nid FROM {node} WHERE title LIKE '%s%' AND type = 'product_collection'", $search);

  $noder = array();
  while ($record = db_fetch_object($result)) {
 $noder[] = $record;
  }

  $matches = array();
  foreach ($noder as $row) {
    $node = node_load($row->nid);
    $termlink = db_fetch_object(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $row->nid));
    $matches[] = array('title' => $node->title, 'link' => $termlink->tid);
  }
 $hits = array();
 $hits['matches'] = $matches;
  print json_encode($hits);
  exit();
}

The $i wasn't incrementing the code as it was outside the foreach loop. By making a second array as above you don't need it anyway... (hope this works)...

Simon