Flot with json and php - php

I made a json array with php. It works but it gives only the 'data' result from the first label and not the rest. Beside that it starts everytime with a number. How can i fix this?
$sql = $site->mysqli->query("SELECT id, title, event_start_date FROM events");
while($row = $sql->fetch_assoc()) {
$dataset[] = array('label' => $row['title']);
$sql2 = $site->mysqli->query("SELECT timestamp, event_id, COUNT(timestamp) AS row_count FROM order_products WHERE event_id = '".$row['id']."' GROUP BY DATE(FROM_UNIXTIME(timestamp))");
$count2 = $sql2->num_rows;
while($row2 = $sql2->fetch_assoc()) {
$dataset['data'][] = array(intval($row2['timestamp'] * 1000), (int)$row2['row_count']);
}
}
$results[]=$dataset;
die(Json_encode($results));
Result:
[{"0":{"label":"Label 1"},"data":[[1392894348000,10],
[1392998502000,3],[1393066962000,5],[1393262541000,3],
[1393577348000,5],[1393704543000,2],[1393780878000,1],
[1393882353000,3],"1":{"label":"Label 2"},"2":{"label":"Label 3"}}]

You should empty your $dataset for each event:
$sql = $site->mysqli->query("SELECT id, title, event_start_date FROM events");
while($row = $sql->fetch_assoc()) {
$dataset = array('label' => $row['title'], 'data' => array());
$sql2 = $site->mysqli->query("SELECT timestamp, event_id, COUNT(timestamp) AS row_count FROM order_products WHERE event_id = '".$row['id']."' GROUP BY DATE(FROM_UNIXTIME(timestamp))");
$count2 = $sql2->num_rows;
while($row2 = $sql2->fetch_assoc()) {
$dataset['data'][] = array(intval($row2['timestamp'] * 1000), (int)$row2['row_count']);
}
$results[]=$dataset;
}
die(json_encode($results));

Related

PHP Why If / Else statement not working in While loop?

This is my code and I don't know why IF/ELSE statement is not working
$user_id = $_POST['user_id'];
$strSQL = "SELECT chat.user_id,max(date) max_date ,user.user_id,user.firstname,user.lastname,user.picture
FROM ( select from_user_id as user_id,date,isread from chat
WHERE to_user_id = '$user_id'
Union select to_user_id as user_id,date,isread from chat WHERE from_user_id = '$user_id' ) as chat join user on user.user_id = chat.user_id
where user.status != 'block' group by chat.user_id order by max_date DESC";
$chatdata = array();
$objQuery = mysql_query($strSQL);
while($row = mysql_fetch_assoc($objQuery)){
$frduser_id = $row['user_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$picture = $row['picture'];
$strSQL2 = "SELECT isread from chat WHERE to_user_id = '$user_id' and from_user_id = '$frduser_id'
ORDER BY date DESC LIMIT 1";
//array_push($chatdata, $row);
$objQuery2 = mysql_query($strSQL2);
if (empty($objQuery2)) {
$row2 = 1;
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}else{
$row2 = mysql_fetch_assoc($objQuery2);
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}
}
You can use mysql_num_rows to get count of rows inside results.
$objQuery2 = mysql_query($strSQL2);
$objQuery2Rows = mysql_num_rows($strSQL2);
if ( $objQuery2Rows < 1) {
$row2 = 1;
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}else{
$row2 = mysql_fetch_assoc($objQuery2);
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}

JSON PHP Merge Same Inserted Data

I have this JSON which has the same ord_name , cust_id , Ord_num
{"orders":[{"id":"1","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"2","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"30.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"},{"id":"2","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"4","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"60.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"},{"id":"3","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"1","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"15.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"}],"o_total":[{"total":"105"}]}
my problem is , how to merge or just Overwrite programmatically the JSON with the 'same' ord_num , customer_id and ord_name
field that will update are qty = 7 , price_x_quan
What i want : Nestea in a bottle will have qty = 7 , price_x_quan = 105
this is my code for ordershow
<?php
mysql_connect('localhost','root','')or die ('No Connection');
mysql_select_db('dbmoms');
//$ord = $arr['ord_num']
$sum=0;
$total = $sum;
$sql1 ="SELECT * FROM orders ORDER BY id desc LIMIT 1";
if($row=mysql_fetch_array(mysql_query($sql1))){
$order_id=$row['ord_num'];
}
$sql ="SELECT * FROM orders WHERE ord_num = '$order_id' ";
$result = mysql_query($sql);
$arr["orders"] = array();
while($row = mysql_fetch_assoc($result)){
$arr['orders'][]= $row ;
$sum = $sum+$row['price_x_quan'];
}
$arr['o_total'][] = array('total' => "$sum" );
$json_encoded_string = json_encode($arr);
$json_encoded_string = str_replace("\\/", '/', $json_encoded_string);
echo $json_encoded_string;
?>
please help !
Above:
$arr['orders'][]= $row ;
Put:
// Specify custom values for Nestea..
if( $row[ 'ord_desc' ] == 'Nestea in a bottle' )
{
$row[ 'ord_qty' ] = '7';
$row[ 'price_x_quan' ] = '105.00';
}
You can do this right in your MySQL query using SUM function with a GROUP BY clause.
As a side note you should consider using mysqli (where the trailing i stands for improved) or PDO for accessing your database, instead of the deprecated mysql interface. Now why on earth should I bother to do that?
$sql1 ="SELECT * FROM orders ORDER BY id desc LIMIT 1";
if($row=mysql_fetch_array(mysql_query($sql1))){
$order_id=$row['ord_num'];
}
// Use SUM and GROUP BY to let the database do the math, introducing
// the calculated 'sum_price_x_quan' and 'sum_ord_qty' columns
$sql = "
SELECT ord_num,
ord_desc,
sum(price_x_quan) as sum_price_x_quan,
sum(ord_qty) as sum_rod_qty
FROM orders
WHERE ord_num = '$order_id'
GROUP BY ord_num
";
$result = mysql_query($sql);
$arr = array();
if ($row = mysql_fetch_assoc($result)) {
$arr['orders'][] = $row ;
$arr['o_total'][] = array('total' => $row["sum_price_x_quan"]);
}
$json_encoded_string = json_encode($arr);
echo $json_encoded_string;
Output:
{
"orders": [
{
"ord_num": "13211554feec24bff73",
"ord_desc": "Nestea in a bottle",
"sum_price_x_quan": "105.00",
"sum_ord_qty": "7"
}
],
"o_total": [
{
"total": "105.00"
}
]
}

how to print json array to decoded?

I am learning JSON.
getDatas.php contents:
$query = "SELECT domain FROM access WHERE userid = '".$userid."'";
$result = mysql_query($query) or die(mysql_error());
$res = array();
while($row = mysql_fetch_array($result)){
$query2 = "SELECT COUNT(*) as cnt from clients WHERE domain ='".$row['domain']."'";
$res2 = mysql_query($query2) or die(mysql_error());
$res2 = mysql_fetch_array($res2);
$res[] = array('domain' => $row['domain'], 'count' => $res2['cnt']);
};
echo json_encode($res);
The output is:
[{"domain":"www.domain1.com","count":"2"},{"domain":"www.domain2.com","count":"42"},{"domain":"www.domain3.com","count":"61"}]
How do I print like this?
How do I get out clean?
www.domain1.com - 2
www.domain2.com - 42
www.domain3.com - 61
Try this:
$query = "SELECT a.domain, COUNT(c.domain) as cnt
FROM `access` as a
LEFT JOIN `clients` as c on c.domain = a.domain
WHERE a.userid = '".$userid."'
GROUP BY c.domain";
$result = mysql_query($query) or die(mysql_error());
$res = array();
while($row = mysql_fetch_array($result)){
$res[] = array($row['domain'] => $row['cnt']);
};
echo json_encode($res);
you only need to use
echo json_encode($res);

Group By 2 Columns with similar info to link with a 2nd table

I have two columns with similar info:
column 1 = item 1, item 3, item 5
column 3 = item 3, item 5, item 8
I want to display how many of each item are in total from both columns.
I have this:
$sql = mysql_query("SELECT FirstTypeID, SecondTypeID, ThirdTypeID, DesignID, COUNT(DesignID) FROM designs WHERE Approved = '1' GROUP BY FirstTypeID, SecondTypeID, ThirdTypeID");
while ($row = mysql_fetch_array($sql)) {
$DesignID = stripslashes($row['DesignID']);
$FirstTypeID = stripslashes($row['FirstTypeID']);
$SecondTypeID = stripslashes($row['SecondTypeID']);
$ThirdTypeID = stripslashes($row['ThirdTypeID']);
$Total = stripslashes($row['COUNT(DesignID)']);
}
$result2 = mysql_query("SELECT * FROM types WHERE TypeID = '$FirstTypeID' OR TypeID = '$SecondTypeID' OR TypeID = '$ThirdTypeID'");
while ($row2 = mysql_fetch_array($result2)) {
echo "<li><a href='index_type.php?TypeID=".$row2{'TypeID'}."'>".$row2{'TypeName'}." (" . $Total . ")</a></li>";
}
but I'm not getting the result that I want, it's only giving me results from one column.
I'm not sure if this is what you are asking for but well here it is
$typesIDs = array(type0 => "", type1 => "", type2 => ""...);
foreach($typesID as $index=>$value){
$query = "SELECT COUNT(*) AS total FROM designs COLUMN1 = ".$index." OR COLUMN2 = ".$index;
$sql = mysql_query($query);
$row = mysql_fetch_array($sql);
array[$index] => $row["total"]
}

temporary table and foreach

$date['"01.06.2012"'] = '2012-06-01';
$date['"01.05.2012"'] = '2012-05-01';
$date['"01.04.2012"'] = '2012-04-01';
$date['"01.03.2012"'] = '2012-03-01';
$date['"01.02.2012"'] = '2012-02-01';
$date['"01.01.2012"'] = '2012-01-01';
foreach($date as $month => $actual_date)
{ /* start foreach loop */
$query = "select Player, SUM(points) AS score from rankings WHERE
`drop_date` > '$actual_date'
AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR)
GROUP BY Player
ORDER BY SUM(sa_points) LIMIT 0,5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['player'];
echo $row['score']; }
} /* end foreach loop */
Now I just want add another query to this resulting table, I have tried create temporary table but didnt know where to put it according to the foreach loop and kept getting either parse errors or last $actual_date results.
Is this what you are trying to reach?
$date['"01.06.2012"'] = '2012-06-01';
$date['"01.05.2012"'] = '2012-05-01';
$date['"01.04.2012"'] = '2012-04-01';
$date['"01.03.2012"'] = '2012-03-01';
$date['"01.02.2012"'] = '2012-02-01';
$date['"01.01.2012"'] = '2012-01-01';
mysql_query('CREATE TEMPORARY TABLE results (player varchar(100), score int)');
foreach($date as $month => $actual_date)
{ /* start foreach loop */
$query = "insert into results (player, score)
select Player, SUM(points) AS score from rankings WHERE
`drop_date` > '$actual_date'
AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR)
GROUP BY Player
ORDER BY SUM(sa_points) LIMIT 0,5";
mysql_query($query) or die(mysql_error());
} /* end foreach loop */
$result = mysql_query('select * from results') or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['player'];
echo $row['score'];
}

Categories