0

I'm wondering if it is possible to combine many queries into one array and return it. The problem is that some of those queries already are arrays...here's what I mean:

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

Here is the php file with the queries:

<?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;
    }
?>

If I have more queries like this, how can I gather them together to be able to do something like:

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

I understand that $daily has a LIMIT 1 with it, but others like $mainnews will be big arrays. How would I set up the PHP echo and get it back in JS? I guess if all else fails, I could do multiple ajax calls but that would be a waste...

4
  • Just join the arrays, json encode them, and return them to the ajax function. Commented Apr 8, 2013 at 15:46
  • Just how I did it there? Commented Apr 8, 2013 at 15:47
  • This all looks fine. Are you getting any errors? Commented Apr 8, 2013 at 15:56
  • 1
    I'm an idiot...I actually wrote it right here and wrong in my original code. I messed up in JS when I was getting it back. Sorry for the waste of time! Maybe this will help someone in the future :) Commented Apr 8, 2013 at 15:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.