0

I am trying to populate mysql data to a line chart using php.My data is in the following style in mysql database.

amt
---
700
250
180
190
720

For passing these values to line chart i need the data in the below format.

[700,250,180,190,720]

When i am trying with the below code, i am getting this below output.

["720"]

Here is my code.

<?php
$sql= "SELECT sum(wlt_txn_amount)as amt FROM wallet_txns where wlt_holder_id = '1' and wlt_txn_del_flg = 'N' and  wlt_txn_cat <> 'Transfer' and wlt_txn_type = 'Expense' and wlt_txn_date between DATE (DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 0 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 0 MONTH)))-1 DAY)) and date(CURDATE()) group by wlt_txn_cat ORDER BY wlt_txn_amount desc"; 
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
if ( mysqli_num_rows($result) >0) 
while($row = mysqli_fetch_array($result)){  
$a = $row['amt'];  
$c = array($a);
}
echo json_encode ($c);
 ?>

This is the first time i am trying to create a chart, if there is any big mistake in the code which i am using, sorry for that. Advance thanks for the advice and support.

1
  • Because you're reassigning $c in your while loop, so $c end up with the last value. Check this out to see how to add items to $c instead of reassigning it. Commented Aug 11, 2015 at 3:55

1 Answer 1

1

Your confusion is understandable, if you look at the line where you assign a value to $c you see that it is constantly being set to the amount value in the current row and throwing away the previously stored answers. The easiest way to fix this is to first say that $c is an array and then assign the values like this:

<? php
$sql = "SELECT sum(wlt_txn_amount)as amt FROM wallet_txns where wlt_holder_id = '1' and wlt_txn_del_flg = 'N' and  wlt_txn_cat <> 'Transfer' and wlt_txn_type = 'Expense' and wlt_txn_date between DATE (DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 0 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 0 MONTH)))-1 DAY)) and date(CURDATE()) group by wlt_txn_cat ORDER BY wlt_txn_amount desc";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));

$c = array();
if (mysqli_num_rows($result) > 0)
  while ($row = mysqli_fetch_array($result)) {
    $a = $row['amt'];
    // $c[] is shorthand for adding a variable to the end of the $c array
    $c[] = $a;
  }
echo json_encode($c); ?>

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the support. sorry for the delayed comment. the answer given is working, but the result is displaying inside " ". How can I remove this " " ? The output what i am getting is given below. ["1300.00","1500.00","899.00","250.00","250.00","720.00"]
Its ok, its working as expected. Thanks for the support.

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.