getJSON函数,从多个mysql查询返回编码数组

时间:2013-04-08 15:43:38

标签: php jquery mysql ajax json

我想知道是否可以将多个查询组合到一个数组中并返回它。问题是其中一些查询已经是数组......这就是我的意思:

JS:

function getNews() {
  $.getJSON(serviceURL + 'getmainnews.php', function(data) {
    $('#fnews li').remove();
    daily = data.items;
      $.each(daily, function(index, mnews) {
        //append the data to listview
      });
    });

这是包含查询的php文件:

<?php
  include 'connect_to_mysql.php';
  mysql_query("SET NAMES UTF8");
  $day = mysql_query("SELECT * FROM basket_news WHERE topic='daily' ORDER BY id DESC LIMIT 1");
  $daily = array();
  while ($r = mysql_fetch_assoc($day)){
    $daily[] = $r;
  }

  $sql = mysql_query("SELECT * FROM basket_news WHERE topic='' ORDER BY id DESC"); 
    $mainnews = array();
    while ($r = mysql_fetch_assoc($sql)){
      $mainnews[] = $r;
    }
?>

如果我有更多这样的查询,我如何将它们聚集在一起以便能够执行以下操作:

echo json_encode(array("items" => $daily, "allitems" => $mainnews));

据我所知,$ daily有一个LIMIT 1,但其他像$ mainnews将是大阵列。我如何设置PHP回声并将其恢复到JS?我想如果所有其他方法都失败了,我可以做多个ajax调用,但那会浪费......

0 个答案:

没有答案