Handling foreach or forloop with array values PHP MYSQL - php

Output Array (The variable name is $r)
Array
(
[Jan-14] => 7588793.52
[Feb-14] => 9944970.87
[Mar-14] => 8567790.20
[Apr-14] =>
[May-14] =>
[Jun-14] =>
[Jul-14] =>
[Aug-14] =>
[Sep-14] =>
[Oct-14] =>
[Nov-14] =>
[Dec-14] =>
)
What I have done so far is..
while($r = mysql_fetch_assoc($query)) {
$series1['data'][] = $r['Jan-14'];
$series2['data'][] = $r['Feb-14'];
.... it will go till Dec-14.......
array_push($result,$series1);
array_push($result,$series2);
.... it will go till Dec-14.......
}
Expected Output:
The code should look something like this this (dynamic)
while($r = mysql_fetch_assoc($query)) {
for($i=1;$i<=count($r);$i++){
$series.$i['data'][] = ??????????
array_push($result,$series.$i);
..................
}
}
Help me out. Don't talk about the db structure or normalization. The db was given by my client.
Thanks,
Kimz

Do you mean like this?
$result = array();
foreach($r as $month => $value) {
$result[] = array('data' => array($value));
}

Related

Passing variable to array

I am passing data base objects to an array.
I need to include another variable to the array. The variable is $latitud_usuario.
Here is the code:
if ($result->num_rows > 0) {
while ($obj = $result->fetch_object()) {
$arr[] = array('nombre_doctor' => $obj->nombre_doctor,'apellido1_doctor' => $obj->apellido1_doctor,'apellido2_doctor' => $obj->apellido2_doctor,'ciudad_doctor' => $obj->ciudad_doctor, 'latitud_doctor' => $latitud_usuario);
}
}
}
echo json_encode($arr);
If I create the array including only fetched objects, the JSON sent is ok, but after including the last array object:
'latitud_doctor' => $latitud_usuario
the JSON is not received as it should.
I guess this last array object expression is wrong.
Any hint is welcome.
Try this
if ($result->num_rows > 0) {
while ($obj = $result->fetch_object()) {
$arr[] = array('nombre_doctor' => $obj->nombre_doctor,'apellido1_doctor' => $obj->apellido1_doctor,'apellido2_doctor' => $obj->apellido2_doctor,'ciudad_doctor' => $obj->ciudad_doctor, 'latitud_doctor' => $latitud_usuario);
$arr['latitud_doctor']=$latitud_usuario;
}
}
}
echo json_encode($arr);
Here's a version that works (using a dummy $obj object):
$obj = (object) array('nombre_doctor'=> 6, 'apellido1_doctor' => 'whatever1', 'apellido2_doctor' => 'whatever2',
'ciudad_doctor' => 'Montreal', 'latitud_usuario' => '35463');
$arr[] = array('nombre_doctor' => $obj->nombre_doctor,'apellido1_doctor' => $obj->apellido1_doctor,
'apellido2_doctor' => $obj->apellido2_doctor,'ciudad_doctor' => $obj->ciudad_doctor,
'latitud_doctor' => $obj->latitud_usuario);
echo json_encode($arr);

Store array into array of array

Im generating Chart4PHP.
In sample it takes data like this
$p->data = array(array(array("2010/10",-48),array("2011/01",238),array("2011/02",395)));
I have array "rows" constructed of row[date][units].
Im storing it in this way:
$rows = array();
for(...)
{
$row[date] = $mydate;
$row[units]= $myunits;
$rows[]=$row;
}
What I should make additionally to be able to use it as $p->data = $rows;
To add the extra array container, call array() with the rows array as the argument.
$data = array(array('date' => "2010/10", 'units' => -48),
array('date' => "2011/01", 'units' => 238),
array('date' => "2011/02", 'units' => 395));
foreach ($data as $d) {
$mydate = $d['date'];
$myunits = $d['units'];
$rows[] = array($mydate, $myunits);
}
$p->data = array($rows);

Extracting data from a database into array

I'm building an angular application, so I have a PHP script extracting the database data into a JSON when requested.
This is what I'm using to extract the data into an array:
$values = array();
$query = "SELECT * FROM photos ORDER BY id";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQL_ASSOC)) {
array_push($values, $row);
}
json_encode($values);
But it gives no result.
After some testing, if I change json_encode($values); to print_r($values); it actually print the table array, here an extract:
Array (
[0] => Array (
[id] => 4
[title] => Feel the Moment
[views] => 6
)
It seems the script is not creating a valid array.
$query = "SELECT * FROM photos ORDER BY id";
if ($result = $this->mysqli->query($query)) {
$json = array();
while ($row = $result->fetch_assoc()) {
$json[] = $row;
}
echo json_encode($json);
}
Your while is not executing, probably because you don't have any returned values from database.
I tested this code and it works well:
$json = array();
$json[] = array(
"id" => 1,
"category" => 'a',
"tags" => 't');
$json[] = array(
"id" => 2,
"category" => 'b',
"tags" => 'tt');
echo json_encode($json);
The result is:
[{"id":1,"category":"a","tags":"t"},{"id":2,"category":"b","tags":"tt"}]
And you'd better use mysqli_fetch_row or mysqli_fetch_assoc.
Try to debug your code. You may use var_dump($result->fetch_assoc()) to see if the query has some results.

Creating JSON array with information retrieved from database

I am trying to create a JSON array with the information selected from a database but I cannot give the array a name.
while($row = mysql_fetch_array($result))
{
$arr = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
echo json_encode($arr);
}
I want to see the result;
{"events":[{"isim":"eere","yer":"dddd","top":"asdfsdffgdfgdfg","tar":"2013-10-18","saat":"12:46"}{"isim":"fhjfr","yer":"yhjrhj","top":"ryjryjrj","tar":"2013-10-30","saat":"12:45"}{"isim":"sfsgsg","yer":"sfgssfg","top":"sgsfgsg","tar":"2013-10-31","saat":"12:45"}]}
But I cannot see the
{"events":[
in the beggining and
]}
at the end.
Thank you.
To generate valid JSON, you first need to add everything to a multi-dimensional array and only then when that is complete, encode it:
$arr = array();
while($row = mysql_fetch_array($result))
{
$arr[] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
// or perhaps just: $arr[] = $row;
}
echo json_encode($arr);
Also note that the mysql_* functions are deprecated.
To put everything under an events key, you would need something like:
$arr['events'][] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);

Dividing rows into dimensional array

I want to get the list for the food from the database into array order by category so that I can split each entry into the categories.
Like this..
$menu = array(
'Appetizers' => array(
'Chicken Tenders' => '$2.99',
'Twisted Chips' => '$1.99'
),
'Seafood' => array(
'Bayou Tilapia' => '$4',
'Grill Atlantic Salmon' => '$3.99'
),
'Steaks & Combos' => array(
'Cowboy Grande Sirloin' => '$7.99'
)
)
Here is what I did.
$db->Query("SELECT menuTitle,menuCategory,menuPrice FROM menu");
$menu = array();
while ($row = $db->Row()) {
$a = array($row->menuCategory => array($row->menuTitle=>$row->menuPrice));
array_push($menu,$a);
}
It doesn't seem to work. Would you please advise how to achieve this?
replace:
$a = array($row->menuCategory => array($row->menuTitle=>$row->menuPrice));
array_push($menu,$a);
with:
$menu[$row->menuCategory][$row->menuTitle]=$row->menuPrice;
The code in the while block is not exactly seeming right, as you always redefine the $a array while iterating.
while ($row = $db->Row()) {
$a[ $row->menuCategory ][ $row->menuTitle] = $row->menuPrice;
array_push($menu,$a);
}
This way, you will only append new keys to the array.
$db->Query("SELECT menuTitle,menuCategory,menuPrice FROM menu");
$menu = array(); $a = array();
while ($row = $db->Row()) {
$a[$row->menuCategory ][$row->menuTitle] = $row->menuPrice;
}
array_push($menu,$a);
echo "<pre>";print_r($menu);echo "</pre>";

Categories