How can I display this MySQL loop in a line Highchart? - php

I'm trying to display this query output in a Highchart (line). I’dlike to know how to input this MySQL loop into the Highchart.
<?php
$qu = "SELECT *,COUNT(url) FROM clicks WHERE url='aaaa' GROUP BY date";
$result = mysql_query($qu) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$hits = $row['COUNT(url)'];
$date = $row['date'];
}?>

Maybe this gives you some idea:
$label = array();
while($row = mysql_fetch_array($result))
{
$label[] = $row["date"];
$data_count[] = (float)$row["COUNT(url)"];
}
$series = array();
$series[] = array("name"=> 'total', "color" => "#4572a7", "data" => $data_count);
$data = array();
$data["chart"]["renderTo"] = "report";
$data["chart"]["defaultSeriesType"] = "column";
$data["title"]["text"] = "Some Title Here";
$data["series"] = $series;
$data["xAxis"]["categories"] = $label;
$data["yAxis"]["allowDecimals"] = true;
header('Content-Type: application/json; charset: utf-8;');
echo json_encode($data);

Related

JSON output returns null using PHP ajax

When I view the record from the database, the result is displayed as null.
This is what I tried so far:
<?php
include("db.php");
$fetchqry = "SELECT mid, planid, paid_date, expire_date FROM payment";
$result = mysqli_query($conn, $fetchqry);
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$mid = $row['mid'];
$planid = $row['planid'];
$date1 = $row['expire_date'];
if (strtotime(date("Y/m/d")) < strtotime($date1))
{
$status = "Active";
}
else
{
$status = "Expired";
}
}
echo json_encode($row);
?>
Add the values you read from the DB to an array, then encode that.
$output = [];
$today = strtotime(date("Y/m/d"));
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$mid = $row['mid'];
$planid = $row['planid'];
$date1 = $row['expire_date'];
if($today < strtotime($date1))
{
$status = "Active";
}
else
{
$status = "Expired";
}
$output[] = ['mid' => $mid, 'planid' => $planid, 'status' => $status];
}
echo json_encode($output);

Creating an array from other arrays

I'm pulling data from a table in two different queries, and I am trying to compare the date field from both results. When the date is equal I want to add the two ending balances from each query together and put the results into a new array. The problem I am having is on line 59, The error I get is
Notice: Undefined offset.
This is what I have:
include "../sqlConnect.php";
mysqli_set_charset($dbScrap, 'utf8');
$dataArray[] = array();
$dateArray[] = array();
$balanceArray[] = array();
//Smaller
$query = "SELECT Account, Date ,SUM(EndingBalance) AS 'Month_Total' FROM gl_period_posting_history
INNER JOIN gl_account
ON gl_period_posting_history.AccountKey = gl_account.AccountKey
Where gl_account.Account= '5010-15-0000' AND FiscalYear > 2012
GROUP BY Account, Date";
$result = mysqli_query( $dbScrap, $query) or die("SQL Error 1: " .
mysqli_error($dbScrap));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$EndingBalance15 = $row['Month_Total'];
$Date15 = $row['Date'];
$dateArray[] = array(
'Date' => $Date15
);
$balanceArray[] = array(
'EndingBalance' => $EndingBalance15
);
}
\\Bigger
$query1 = "SELECT Account, Date ,SUM(EndingBalance) AS 'Month_Total' FROM gl_period_posting_history
INNER JOIN gl_account
ON gl_period_posting_history.AccountKey = gl_account.AccountKey
Where gl_account.Account ='5010-08-0000' AND FiscalYear > 2012
GROUP BY Account, Date";
$result = mysqli_query( $dbScrap,$query1) or die("SQL Error 1: " . mysqli_error($dbScrap));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$Date08 = $row['Date'];
$EndingBalance08 = $row['Month_Total'];
for($i = 0; $i < $dateArray; $i++){
if($Date08 == $dateArray[$i]) {
$message = "Date Equal";
$EndingBalance = $EndingBalance08 + $balanceArray[$i];
$Date = $Date08;
}else{
$message = "Date Not Equal";
$Date = $Date08;
$EndingBalance = $EndingBalance08;
}
}
$dataArray[] = array(
'EndingBalance' => $EndingBalance,
'Date' => $Date,
'Message' => $message
);
}
echo "<pre>";
print_r($dataArray);
//echo json_encode($dataArray);
echo "</pre>";
$result->close();
/* close connection */
$dbScrap->close();
Thank you for your help.
The problem is in for loop, correction:
for ($i = 0; $i < count($dateArray); $i++){
In the loop where you fetch results from your first query, use the date from each row as the key in $balanceArray, like this:
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$EndingBalance15 = $row['Month_Total'];
$Date15 = $row['Date'];
$balanceArray[$Date15] = $EndingBalance15;
}
Then in the loop where you fetch results from your second query, you can use isset to check if that date exists in the results from the first query, like this.
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$Date08 = $row['Date'];
$EndingBalance08 = $row['Month_Total'];
if (isset($balanceArray[$Date08])) {
$message = "Date Equal";
$EndingBalance = $EndingBalance08 + $balanceArray[$Date08];
} else {
$message = "Date Not Equal";
$EndingBalance = $EndingBalance08;
}
$dataArray[] = array(
'EndingBalance' => $EndingBalance,
'Date' => $Date08,
'Message' => $message
);
}

How to output like this in php

I want to achieve this output in php.
[
{
"id": 1388534400000,
"author": "Pete Hunt",
"text": "Hey there!"
},
{
"id": 1420070400000,
"author": "Paul O’Shannessy",
"text": "React is *great*!"
}
]
I have a while loop in my backend below.
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json['id'] = $row['id'];
$json['author'] = $row['author'];
$json['text'] = $row['text'];
}
echo json_encode($json);
It only returns the last row in the database, and I want to display them all.
Thank you.
If your $row contains only these three fields from database then use below code otherwise #govindkr13 answer
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
$json = [];
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[] = $row;
}
echo json_encode($json);
You are simply overwriting your $json array every time. Try this:
$i=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$i]['id'] = $row['id'];
$json[$i]['author'] = $row['author'];
$json[$i]['text'] = $row['text'];
$i++;
}
echo json_encode($json);
use this
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
$final = [];
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json['id'] = $row['id'];
$json['author'] = $row['author'];
$json['text'] = $row['text'];
$final[] = $json;
}
echo json_encode($final);
Maybe this can help.
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$row['id']]['author'] = $row['author'];
$json[$row['id']]['text'] = $row['text'];
}
$i=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$i]['id'] = $row['id'];
$json[$i]['author'] = $row['author'];
$json[$i]['text'] = $row['text'];
$i++;
}
echo json_encode($json);
try this
$count=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$count]['id'] = $row['id'];
$json[$count]['author'] = $row['author'];
$json[$count]['text'] = $row['text'];
$count++;
}
echo json_encode($json);

While Loop only returns one row from 9000+ records

I had a script for this that worked great in PHP 5.3. New host only supports 5.5 so I've done some modifying. So far, I can only get one row to be returned. I've been agonising for hours and I just can't see why.
Is this possibly because I'm not using array_push() after the while loop? In my original script I had it but found out with this one I got a 500 Internal Server Error with it which disappeared if I took it out.
<?php
$mysqli = mysqli_connect("localhost", "user", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$row_array = array();
$query = "SELECT title,year,author,journal,keywords,abstract,url FROM entries";
if ($result = mysqli_query($mysqli, $query)) {
/* fetch object array */
while ($row = mysqli_fetch_assoc($result)) {
$row_array['title'] = $row['title'];
$row_array['year'] = $row['year'];
$row_array['author'] = $row['author'];
$row_array['journal'] = $row['journal'];
$row_array['keywords'] = $row['keywords'];
$row_array['abstract'] = $row['abstract'];
$row_array['url'] = $row['url'];
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
What get's returned is this:
{"data":{"title":"ERGOT The Genus Claviceps","year":"1999","author":null,"journal":null,"keywords":"LSD","abstract":null,"url":null}}
Which is fine and dandy and all except there are about 9500 other rows that should be coming back with it.
You need to have a multi-dimensional array and you have taken a single dimensional array.
So, every time record comes, it overwrites older one.
Hence, you are getting the last record only.
Modify your code to:
$i=0;
$row_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
++$i;
}
$row_array= array();
while ($row = mysqli_fetch_assoc($result)) {
$tmp = array();
$tmp['title'] = $row['title'];
$tmp['year'] = $row['year'];
$tmp['author'] = $row['author'];
$tmp['journal'] = $row['journal'];
$tmp['keywords'] = $row['keywords'];
$tmp['abstract'] = $row['abstract'];
$tmp['url'] = $row['url'];
$row_array[] = $tmp;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
Your while loop keeps overwriting the same array elements in the $row_array array.
To do what you're trying to do:
while($row = mysqli_fetch_assoc($result)) {
$row_array[] = $row;
}
Your $row_array replaces every time and you get the last one. change your code to:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
$i++;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';

Form json using php json_encode failed

By using below code
$data = array();
$sql = "SELECT * FROM list";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data['title'] = $row['title'];
$data['name'] = $row['name'];
}
}
echo json_encode($data);
I got 1 result, I can get full result if I do $data[] = $row['title'], but I want to make the result like this
{'title' : ['title 1','title 2'], 'name':['John','Amy']}
You are overwriting the title in each loop iteration. You need to accumulate all the titles and then set it in your data array.
$data = array();
$sql = "SELECT title FROM mainlist";
$result = $db->query($sql);
$titles = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$titles[] = $row['title'];
}
}
$data['title'] = $titles;
echo json_encode($data);
The easiest away is probably this:
$rows = array();
$sql = "SELECT * FROM list";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
}
echo json_encode($rows);
you could achieve by using group_concat on each of the columns in your query. That way you do not need to loop the result again and add column etc...
$sql = "SELECT group_concat(title) as title,group_concat(name) as name FROM list";
$result = $db->fetch(PDO::FETCH_ASSOC);
echo json_encode($result);
Try this:
$titles = array();
$names = array();
$sql = "SELECT title,name FROM mainlist";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$titles[] = $row['title'];
$names[] = $row['name'];
}
}
echo json_encode(array("title" => $titles, "name" => $names));
UPDATE
Updated my code to let you manage an undefined number of columns as result
$out = array();
$sql = "SELECT * FROM wp_cineteca";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$keys = array_keys($row);
for ($i = 0; $i < count($row); $i++) {
$out[$keys[$i]][] = $row[$i];
}
}
}
echo json_encode($out);

Categories