3

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

4
  • 1
    patientData.push(row); <- try this Commented Mar 18, 2016 at 6:51
  • Yeah and how I access the first array ? patientData['one'] or patientData[0] Commented Mar 18, 2016 at 6:55
  • patientData[0]. As you have pushed the rows into patientData which is an array Commented Mar 18, 2016 at 7:43
  • If you see at the PHP, when I was working with only single array that I am sending that inside a variable AS 'patients' and in AJAX, I am getting them as push(row['patients']). Now When I am sending 2 arrays, how should I write them ? Commented Mar 18, 2016 at 10:12

3 Answers 3

1

Here is how to do it:

    success: function(data){
        var patientData = [];
        var res = $.parseJSON(data);
        $.each(res, function(index, element) {
            alert(res[index]);
            patientData.push(res[index]);
        });
    }
Sign up to request clarification or add additional context in comments.

4 Comments

the alert[row[0]] gave me alert [object][object] and alert[res[0]] gave me alert undefined
you can't use res[0] outside the '$.each' loop, try patientData[0] instead
If you see at the PHP, when I was working with only single array that I am sending that inside a variable AS 'patients' and in AJAX, I am getting them as push(row['patients']). Now When I am sending 2 arrays, how should I write them ?
The same way you did before,in PHP put the the two arrays which you want to return in one array then pass it through json_encode and return it, in the Ajax success function you will have the same result and you don't need to change anything but this time patientData will be an object so to access it you have to use patientData[0][0]
1

You will get key("one", "two", ...) in key and row data in row

As $.each returns object property name(key) in first parameter and property value in second parameter so here we go as follow:

success:function(res){
  var res = $.parseJSON(res);
  console.log(res);
  var patientData = [];

  $.each(res, function (key, row){
    patientData.push(row);
    console.log(patientData);
  });
  //Bar Chart Script;
}

For example we can check the result like this

$.each({"one":[1, 2, 3, 4], "two":[5, 6, 7, 8]}, function (key, value){
  console.log(key, value);
  
  /* write to document */
  document.write("<br>");
  document.write("Key:" + key);
  document.write(", Value:" + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

6 Comments

As you defined patientData in success so you won't be able to access it outside this function; if you are trying to do so just saying ;)
If you see at the PHP, when I was working with only single array that I am sending that inside a variable AS 'patients' and in AJAX, I am getting them as push(row['patients']). Now When I am sending 2 arrays, how should I write them ?
@androidnation do you want to merge all records in single array?
No. I want to display the 2 arrays on the same chart
FYI, $.parseJSON is giving me an error on VM:4, so I think working without it is better
|
0

Try:

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

6 Comments

Yeah and how I access the first array ? patientData['one'] or patientData[0]
Check the modified answer $.each(res, function(key, row){ ...});
the modified code gave me at the console undefined
Are getting output in console.log(res); ??
I am getting the array console.log(res)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.