Getting multiple arrays into AJAX from PHP

Viewed 166

EDIT

enter image description here

I need to get two fetched arrays into one array, and use echo json_encode to send it back to AJAX.

Here is my PHP code:

header('Content-Type: application/json');
$arr = array();
$user = $_SESSION['username'];
$id_logged = $_SESSION['login_id'];



$date1 = $_POST['current_year'];

    //$res = array();
    $c = "cash";
    $i = "installment";
    //SUM of cash paid Month
    $sql = "SELECT 
    count(app_id) as patients, 
    t.m as month 
FROM (    
   SELECT 1 AS m UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
   SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION
   SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS t
LEFT JOIN dentist.appointment ON t.m = month(date_app) AND
          id_logged = :logged AND year(date_app) = :year_now
GROUP BY t.m";
$sqlStmt = $conn->prepare($sql);
$sqlStmt->bindValue(":logged", $id_logged);
$sqlStmt->bindValue(":year_now", $date1);
$exec = $sqlStmt->execute();
$res1 = $sqlStmt->fetchAll();

$sql2 = "SELECT 
    count(app_id)+1 as patients, 
    t.m as month 
FROM (    
   SELECT 1 AS m UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
   SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION
   SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS t
LEFT JOIN dentist.appointment ON t.m = month(date_app) AND
          id_logged = :logged AND year(date_app) = :year_now
GROUP BY t.m";
$sqlStmt2 = $conn->prepare($sql2);
$sqlStmt2->bindValue(":logged", $id_logged);
$sqlStmt2->bindValue(":year_now", $date1);
$exec2 = $sqlStmt2->execute();
$res2 = $sqlStmt2->fetchAll();

$res['one']=$res1;
$res['two']=$res2;

echo json_encode($res);

And here the response at XHR:

enter image description here

enter image description here

Now at the AJAX script, I need to get each part of the array so I can display it on a chart:

success:function(res)
{
  var patientData = [];
  $.each(res, function( key, row)
  {
    patientData.push(row['one']);
    console.log(patientData);
  });
  //Bar Chart Script;
 }

But I can't see any data at the console, nor at the bar chart. The result of the console is undefined:

enter image description here

3 Answers
Related