php datatable array results - php

I am trying to extract data from mysql database into a datatable using ajax, and php.
The code for my response.php file is below:
<?php
$result = mysql_query("select * from orders");
while ($row = mysql_fetch_array($result)) {
$data = array(
array(
'Name' => $row['jobnumber'],
'Empid' => $row['ID'],
'Salary' => $row['product']
)
);
}
$results = array(
"sEcho" => 1,
"iTotalRecords" => count($data),
"iTotalDisplayRecords" => count($data),
"aaData" => $data
);
/*while($row = $result->fetch_array(MYSQLI_ASSOC)){
$results["data"][] = $row ;
}*/
echo json_encode($results);
?>
Why is this only returning one result in my front end table?
http://orca.awaluminium.com/test.php
link above shows table.

You're replacing value of $data instead of pushing new rows in an array.
Change the following line.
$data = array(
array(
'Name'=>$row['jobnumber'],
'Empid'=>$row['ID'], 'Salary'=>$row['product']
)
);
To
$data[] = array(
'Name'=>$row['jobnumber'],
'Empid'=>$row['ID'], 'Salary'=>$row['product']
);
Also put $data=array(); before string while() looop.

You have to do foreach
while ($row = mysql_fetch_array($result)){
foreach($row as $a)
{$data[] = array(
array('Name'=>$a['jobnumber'], 'Empid'=>$a['ID'], 'Salary'=>$a['product']),
);
}
}

Related

Convert PHP Array to JSON Array of object

I'm using Google Orgchart in my project. In that I'm returning JSON OBJECT from PHP file.
Problem
My Problem is when I hardcode the value, It works fine. When I return data from PHP file. It did not work. I guess the data format which is returning from PHP file is not correct. File below.
$result = mysql_query("SELECT * FROM emp");
while($row = mysql_fetch_array( $result )) {
$arr1 = array(
'v' => $row['name'],
'f' => $row['name']+'<div style="color:red; font-style:italic">President</div>',
'' => $row['rep'],
'' => $row['des'],
);
array_push($dataarray, $arr1);
}
echo json_encode($dataarray);
which returns object like below
How it Should be
My hardcorded JSON OBJECT below
[
[{v:'Prabhkar', f:'Prabhkar<div style="color:red; font-style:italic">President</div>'},
'', 'The President'],
[{v:'Raguram', f:'Raguram<div style="color:red; font-style:italic">GM</div>'},
'Prabhkar', 'GM']
]
Console Screenshot below:
Do I need to create a one more array in PHP file. How I suppose to change the PHP array according to above screenshot. sorry for my english. Thank you.
You need to wrap 'v' and 'f' in a array, then push other values to parent array.
$result = mysql_query("SELECT * FROM emp");
while($row = mysql_fetch_array( $result )) {
$arr1 = array(
array(
'v' => $row['name'],
'f' => $row['name'] . '<div style="color:red; font-style:italic">President</div>'
),
$row['rep'],
$row['des']
);
array_push($dataarray, $arr1);
}
echo json_encode($dataarray);
In your hard coded array the first key has an array inside so you must change your code like this
$result = mysql_query("SELECT * FROM emp");
$dataarray = [];
while($row = mysql_fetch_array( $result )) {
$arr1 = array(
array(
'v'=> $row['name'],
'f' => $row['name'].'<div style="color:red; font-style:italic">President</div>',),
$row['rep'],
$row['des'],
);
array_push($dataarray, $arr1);
}
echo json_encode($dataarray);
Your internal structure is wrong. Your internal structure is an array, with the first being a map, followed by two values. Your current implementation is an array, with only a map.
$result = mysql_query("SELECT * FROM emp");
while($row = mysql_fetch_array( $result )) {
$arr1 = array(
array(
'v' => $row['name'],
'f' => $row['name'] . '<div style="color:red; font-style:italic">President</div>',
),
$row['rep'],
$row['des']);
array_push($dataarray, $arr1);
}
echo json_encode($dataarray);

Multidimensional array in PHP with keys

I making a script to monitorize data from some VPSs, which is being stored in a MySQL database.
$result = mysql_query("SELECT * FROM data");
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'id' => $row['id'],
'hostname' => $row['hostname'],
'loadavrg' => $row['load average']
);
}
I would like to separase data by hostnames so I can read it like the following example:
foreach($data['sitename.com'] as $d)
echo $d['loadavrg'];
I already tried with the following code (just for testing), but didn't work:
$result = mysql_query("SELECT * FROM data WHERE hostname='sitename.com'");
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'sitename.com' => array(
'id' => $row['id'],
'loadavrg' => $row['load average']
)
);
}
I'm just missing the right way and syntax to achieve it :X
Every element should be added as subarray of 'sitename.com':
while ($row = mysql_fetch_array($result)) {
$data['sitename.com'][] = array(
'id' => $row['id'],
'loadavrg' => $row['load average']
);
}
Try this,
while ($row = mysql_fetch_array($result)) {
$hostname = $row['hostname'];
$data[$hostname][] = array(
'id' => $row['id'],
'loadavrg' => $row['load average']
);
}

full calendar getting just the first event

I'm trying to fetch events from database to calendar but at this stage it just outputs the first event it gets from the database and the json when i echo it it returns just the first event
<?php
include("dbcon.php");
$events1 = array();
$sql345="select * from takimet";
$result = mysql_query($sql345)or die(mysql_error());
while ($row=mysql_fetch_array($result)){
$id = $row['agent_id'];
$title = $row['ezitimi'];
$start = $row['datestamp'];
$events1 = array(
'id' => "$id",
'title' => "$title",
'start' => "$start"
);
}
echo json_encode($events1);
?>
You are over writing the same array occurance each time round you loop.
Instead
$events1 = array();
while ($row=mysql_fetch_array($result)){
$events1[] = array( //<--- changed
'id' => $row['agent_id'],
'title' => $row['ezitimi'],
'start' => $row['datestamp']
);
}

Get all data from sql php

I want to get all locations in table travel_location in mysql. This is my code
$select = $this->_db_table->select()->from(travel_location, array('*'));
$result = $this->_db_table->fetchAll($select);
if(count($result) == 0) {
throw new Exception('not found',404);
}
while ($row1 = mysql_fetch_array($result)){
$user_object = new Api_Model_User($row1);
$count = 1;
$json = array($json[$count] = array(
'travel_location_id' => $user_object->travel_location_id,
'city_id' => $user_object->city_id,
'user_id' => $user_object->user_id,
'location_name' => $user_object->location_name,
'description' => $user_object->description,
'longitude' => $user_object->longitude,
'latitude' => $user_object->latitude,
'created_time' => $user_object->created_time,
'updated_time' => $user_object->updated_time));
$count++;
}
It doesn't work. I print $row1 = mysql_fetch_array($result) and it returns false, so I think it's wrong because of this line. How can I fix it?
If you use fetchAll from Zend_Db_Table you get a Zend_Db_Table_Rowset as result.
Try this:
foreach ($result as $row) {
// $row should be a Zend_Db_Table_Row object
// you can cast to array
$rowArray = $row->toArray();
$user_object = new Api_Model_User($rowArray);
}
Read more about here and here

Ouput data in JSON format on Array in PHP

$result = mysql_query($query);
$leaderboard = array();
while($row = mysql_fetch_assoc($result)) {
$leaderboard[$row["username"]] = $row["score"];
}
$output = array
(
'status' => 1,
'content' =>$leaderboard
);
print_r(json_encode($output));
right now the $output array is such JSON:
{"tim":"120","john":"45","larry":"56"}
but I want to have them as key-value pair so instead I want to be like:
{"name":"tim","score":120","name":"john","score="45", etc.}
and if I need that way, how do I modify the $leaderboard array so the output would be like that?
$leaderboard[] = Array('name' => $row["username"], 'score' => $row["score"]);

Categories