Load Json Data into an AmChart - php

I beginner to Amchart and am test load data in Amchart using php
this is my format json
[{"date":"2018-01-01","column-1":"5187"},{"date":"2018-01-02","column-1":"15790"},{"date":"2018-01-03","column-1":"15826"},{"date":"2018-01-04","column-1":"17026"},{"date":"2018-01-05","column-1":"17908"},{"date":"2018-01-01","column-2":"2143"},{"date":"2018-01-02","column-2":"6192"},{"date":"2018-01-03","column-2":"6106"},{"date":"2018-01-04","column-2":"6532"},{"date":"2018-01-05","column-2":"7000"}]
and this is my code
var data1 = <?php
include('connect.php');
$data = array();
$arr = array();
$query = "SELECT * FROM shell_table_graph";
$db = mysql_query($query);
while($rows = mysql_fetch_assoc($db)) {
$data[] = ['column' => $rows['column'], 'formula' => $rows['calculation']];
}
foreach($data as $rowss) {
$formula = $rowss['formula'];
$query = "SELECT Date, SUM(".$formula.") AS Formula FROM eod_split_interval WHERE Date BETWEEN '2018-01-01' AND '2018-01-05' GROUP BY Date";
$db = mysql_query($query);
while($r = mysql_fetch_assoc($db)) {
$arr[] = ['date' => $r['Date'], $rowss['column'] => $r['Formula']];
$code = json_encode($arr);
}
}
echo $code;
?>;

Related

Create a Multidimensional Array and json_encode

I have to get a JSON response from the database:
id and last_active:
$users = array();
$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
while ($row = $stmt->fetch_assoc()) {
$users[]['id'] = $row['id'];
$users[]['last_active'] = $row['last_active'];
}
echo json_encode($users);
The array must be as follows:
$users = array (
0 => array(1, 1522921015),
1 => array(2, 1522921019),
2 => array(3, 1522921102),
3 => array(4, 1522921195),
4 => array(5, 1522921034)
);
How to properly build the multidimensional array with the query result?
Try this code
$users = array();
$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
$i=0;
while ($row = $stmt->fetch_assoc()) {
$users[$i]['id'] = $row['id'];
$users[$i]['last_active'] = $row['last_active'];
$i++;
}
echo json_encode($users);
You can use temporary array like this:
$users = array();
$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
while ($row = $stmt->fetch_assoc()) {
$tempArray = []; // added
$tempArray[] = $row['id'];
$tempArray[] = $row['last_active'];
$users[] = $tempArray; // added
}
echo json_encode($users);
or directly assign both values :
$users[] = array($row['id'],$row['last_active']);
Remove json_encode just print $user
You should push an array without keys to get the expected data:
$users = array();
$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
while ($row = $stmt->fetch_assoc()) {
$users[] = [ $row['id'], $row['last_active'] ];
}
So, $users will be equals to:
$users = array (
0 => array(1, 1522921015),
1 => array(2, 1522921019),
2 => array(3, 1522921102),
3 => array(4, 1522921195),
4 => array(5, 1522921034)
);
Or, using the MYSQLI_NUM options with fetch_array():
while ($row = $stmt->fetch_array(MYSQLI_NUM)) {
$users[] = $row;
}
my choise will be like this:
$users = array();
$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
while ($row = $stmt->fetch_assoc()) {
$users[] = [
$row['id'],
$row['last_active'],
];
}
echo json_encode($users);

php mysql_fetch_assoc to json encode

I am Using Following code php array to encode Json
$query = "SELECT * FROM register WHERE email='$email'AND password='$password'AND status!='0'";
$result = mysql_query($query) or die('Errant query: ' . $query);
$numResults = mysql_num_rows($result);
if ($numResults > 0)
{
$data = array();
while ($row = mysql_fetch_assoc($result))
{
$data = $row;
}
echo json_encode($data);
}
IT gives Me result like
{"id":"26","fname":"Shankar","lname":"Salunkhe","category_name":"2"}
But I wanted To result like
{"SignIn":[{"id":"26","fname":"Shankar","lname":"Salunkhe","category_name":"2"}],"errors":[],"totalNumberOfRecords":1,"responseCode":"00000"}
How Can I do That
Or Suggest Me any other Method to do that
You need to do something like below to get the desired output.
$query = "SELECT * FROM register WHERE email='$email'AND password='$password'AND status!='0'";
$result = mysql_query($query) or die('Errant query: ' . $query);
$numResults = mysql_num_rows($result);
if ($numResults > 0)
{
$data = array();
while ($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
$result = ['SignIn' => $data, 'totalNumberOfRecords' => $numResults, 'errors' => [], 'responseCode' => 0000];
echo json_encode($result);
exit;
}
You have to store all information in a array to get what you want.
For example,
$data = array("id" =>"26","fname"=>"Shankar","lname"=>"Salunkhe","category_name"=>"2");
$array = array("SignIn" => $data, "errors" => [], "totalNumberOfRecords" => 1,"responseCode" => "00000");
$result = json_encode($array);

Insert results of multiple tables into one json_encode()

Excuse my language mistakes, I speak French.
I did some research on this site and elsewhere, but what I have found didn't solve my problem.
I am doing a site where people can post their stories. I'm working on a page where the author should be able to see his stories and chapters of each of them.
I do a query on two tables to get the titles of stories and chapters. I have a table "stories" in which there is the title and summary of each story (and other stuff like id and date of publication). And another table called "chapters", in which there is the title of each chapter and the id of the story to which they belong.
I would like to retrieve these items later in my html, to have something like this:
Story title 1
Chapter 1
Chapter 2
So I do a json_encode () but JSONLint said that the json I get is invalid. PHP is new to me, I began to learn so I'm probably not writing my code correctly.
Here is my code:
<?php
session_start();
require_once('connexion_db.php');
if($db=connect()){
$userID = $_SESSION['id'];
$arr = array();
$reqRecits = $db->query('SELECT titre, id FROM recits WHERE user_id = '.$userID.'');
$resRecits = $reqRecits->fetchAll(PDO::FETCH_ASSOC);
foreach ($resRecits as $r){
$recitID = $r['id'];
$recitTitre = $r['titre'];
$reqChapitres = $db->query('SELECT id, titre_chapitre FROM chapitres WHERE recit_id = '.$r['id'].'');
$resChapitres = $reqChapitres->fetchAll(PDO::FETCH_ASSOC);
foreach ($resChapitres as $c){
$chapTitre = $c['titre_chapitre'];
}
$arr = array('titre'=>$recitTitre,'chapitre'=>$chapTitre);
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
}
}else{
echo "bdd non connectée.";
}
?>
I tested the method proposed here, but the result is worse.
I don't know what to do :(
Thanks for your help!
It seems like you'd want the inner loop to go more like this:
$chapTitre = array();
foreach ($resChapitres as $c){
$chapTitre[] = $c['titre_chapitre'];
}
So your resulting JSON would include all the chapter titles.
Also, as it stands, you're listing a series of unconnected JSON objects, which should probably be a single array to be a legal JSON object.
foreach ($resRecits as $r){
$recitID = $r['id'];
$recitTitre = $r['titre'];
$reqChapitres = $db->query('SELECT id, titre_chapitre FROM chapitres WHERE recit_id = '.$r['id'].'');
$resChapitres = $reqChapitres->fetchAll(PDO::FETCH_ASSOC);
$chapTitre = array();
foreach ($resChapitres as $c){
$chapTitre[] = $c['titre_chapitre'];
}
$arr[] = array('titre'=>$recitTitre,'chapitre'=>$chapTitre);
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
Will collect them all and output as a list.
[
{
'titre': 'Title 1',
'chaptitre': ['Chapter 1', 'Chapter 2']
},
{
'titre': 'Title 2',
'chaptitre': ['Chapter 1', 'Chapter 2', 'Chapter 3']
},
]
<?php
session_start();
require_once('connexion_db.php');
if($db=connect()){
$userID = $_SESSION['id'];
$arr = array();
$reqRecits = $db->query('SELECT titre, id FROM recits WHERE user_id = '.$userID.'');
$resRecits = $reqRecits->fetchAll(PDO::FETCH_ASSOC);
$chapTitres = array();
foreach ($resRecits as $r){
$recitID = $r['id'];
$recitTitre = $r['titre'];
$reqChapitres = $db->query('SELECT id, titre_chapitre FROM chapitres WHERE recit_id = '.$r['id'].'');
$resChapitres = $reqChapitres->fetchAll(PDO::FETCH_ASSOC);
foreach ($resChapitres as $c){
$chapTitres[] = $c['titre_chapitre'];
}
$arr[] = array('titre' => $recitTitre, 'chaptitres' => $chapTitres);
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
}else{
echo "bdd non connectée.";
}
?>
> For Me this is Worked !
<?php
header('content-type: application/json');
date_default_timezone_set('Asia/Dhaka');
$DatabaseName = "";
$HostPass = "";
$HostUser = "";
$HostName = "LocalHost";
$db_connect = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$userid = $_GET['id'];
$status = $_GET['status'];
$rows = array();
$datas = array();
$result = mysqli_query($db_connect, "SELECT id FROM drivers WHERE phone = '$userid'");
if ($result)
{
$row = mysqli_fetch_assoc($result);
$id = $row['id'];
$result = mysqli_query($db_connect, "SELECT `delivery_id` , `date`, `time` FROM `driver_orders` WHERE driver_id='$id' AND `status` ='$status'");
mysqli_set_charset($db_connect,"utf8");
while ($row = mysqli_fetch_assoc($result))
{
$d_id = $row['delivery_id'];
$data = mysqli_query($db_connect, "SELECT r_name,r_number,r_zone,r_address,amount FROM deliveries WHERE id = '$d_id'");
while ($row2 = mysqli_fetch_assoc($data))
{
$datas[] = array(
"r_name" => $row2['r_name'],
"delivery_id" => $row['delivery_id'],
"time"=> $row['time'],
"date"=> $row['date'],
"r_number" => $row2['r_number'],
"r_zone" => $row2['r_zone'],
"amount" => $row2['amount'],
"r_address" => $row2['r_address']
);
}
}
}
echo json_encode($datas);
?>

How to convert mysqli_fetch_assoc results into json format?

i am trying to convert the result of my query into a json format so i can grap it with jquery in another file. I dont get any errors but its not recognised as json.
$patientquery = mysqli_query($connect, "SELECT * FROM login WHERE assignedTo='$logID'");
$numrows = mysqli_num_rows($patientquery);
if($numrows > 0)
{
while($rows = mysqli_fetch_assoc($patientquery))
{
$dbloginID = $rows['loginID'];
$dbname = $rows['name'];
$result[] = array('patient'=>array('id' => $dbloginID, 'name' => $dbname));
}
}
else
{
$result[] = 'No Patients yet';
}
echo json_encode($result);
please try this:
$patientquery = mysqli_query($connect, "SELECT * FROM login WHERE assignedTo='$logID'");
$numrows = mysqli_num_rows($patientquery);
$result = array();
if($numrows > 0)
{
while($rows = mysqli_fetch_assoc($patientquery))
{
$dbloginID = $rows['loginID'];
$dbname = $rows['name'];
$result['patient'][] = array('id' => $dbloginID, 'name' => $dbname);
}
}
else
{
$result[] = 'No Patients yet';
}
echo json_encode($result);
You should declare $result outside while loop like this
$result = array();

Creating an associative array in PHP from sql results, for json

I'm trying to create an associative array from sql results for json_encode.
here's my code:
$timelineQuery = "SELECT * FROM timeline_table";
$contentQuery = "SELECT * FROM content_table";
$picQuery = "SELECT * FROM pic_table";
$sql = mysql_query($timelineQuery) or die(mysql_error());
$sql2 = mysql_query($contentQuery) or die(mysql_error());
$sql3 = mysql_query($picQuery) or die(mysql_error());
$mainArray = array(
'timeline' => $timelineArray = array(
'content' => $contentArray = array(
'pictures' => $picArray = array(),
),
),
);
while($row = mysql_fetch_assoc($sql)) {
$timelineArray[] = $row;
}
while($row2 = mysql_fetch_assoc($sql2)) {
$contentArray[] = $row2;
}
while($row3 = mysql_fetch_assoc($sql3)) {
$picArray[] = $row3;
}
echo stripslashes(json_encode($mainArray));
If I json_encode my $mainArray as it is, the returned json has the syntax I'm looking for, but I've not been able to fill the array without adding it to the end of my array.
{"timeline":{"content":{"pictures":[]}}}
first:
while($row = mysql_fetch_assoc($sql)) {
$timelineArray[] = $row;
}
while($row2 = mysql_fetch_assoc($sql2)) {
$contentArray[] = $row2;
}
while($row3 = mysql_fetch_assoc($sql3)) {
$picArray[] = $row3;
}
then:
$mainArray = array(
'timeline' => $timelineArray = array(
'content' => $contentArray = array(
'pictures' => $picArray = array(),
),
),
);
echo stripslashes(json_encode($mainArray));
you defined your array with empty arrays and didn't renew it's states.

Categories