php-JSON输出的结构

php-JSON输出的结构,php,ajax,json,forms,Php,Ajax,Json,Forms,我问的另一个问题是: 我的搜索只返回1项,我很确定问题出在我的php中的某个地方,我不太确定我是否正确地添加到数组中,或者它可能是您在上面的链接上看到的javascript,但我对此表示怀疑 我的php代码: function mytheme_ajax_response() { $search = $_GET["search_text"]; $result = db_query("SELECT nid FROM {node} WHERE title LIKE '%s%' AND ty

我问的另一个问题是:

我的搜索只返回1项,我很确定问题出在我的php中的某个地方,我不太确定我是否正确地添加到数组中,或者它可能是您在上面的链接上看到的javascript,但我对此表示怀疑

我的php代码:


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();
}

您似乎在foreach循环之后增加$i变量。因此,在整个循环中,$i始终为0,因此您总是为$matches[0]设置标题和链接值。

尝试以下方法:

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();
}

$i没有增加代码,因为它在foreach循环之外。通过制作如上所述的第二个数组,您无论如何都不需要它。。。(希望这能起作用).

你能告诉我们你的产量吗?哇,老兄,我真蠢,需要多吃点鱼什么的,谢谢!没问题,我们时不时都会错过显而易见的事情;-)如果此答案满足您的问题,请将其标记为答案。