I have used the PHP Gantt Class from Github here:
https://github.com/bastianallgeier/gantti
I have a php script which generates a gantt chant from data in a php array:
$data = array();
$data[] = array(
'label' => 'Project 1',
'start' => '2012-04-20',
'end' => '2012-05-12'
);
$data[] = array(
'label' => 'Project 2',
'start' => '2012-04-22',
'end' => '2012-05-22'
);
Instead of this I would like to use a mysql database to print out results as arrays with a layout that matches the previous way of loading data:
$query=mysql_query("select main_task AS 'label', start, end from tasks") or
die(mysql_error());
// Collect the results
while($obj = mysql_fetch_object($query)) {
$arr[] = $obj;
}
// JSON-encode the response
$json_response = json_encode($arr);
// Return the response
echo $json_response;
This is the error response I get:
[{"label":"aris","start":"2012-05-15","end":"2012-07-03"},{"label":"test","start":"2012-06-01","end":"2012-07-03"},{"label":"test1","start":"2012-06-01","end":"2012-08-05"},{"label":"Adams","start":"2012-05-06","end":"2012-06-17"},{"label":"hellooo","start":"2012-07-22","end":"2012-09-05"},{"label":"hello 2","start":"2012-05-11","end":"2012-06-03"}]
Fatal error: Cannot use object of type stdClass as array in /home/inse1d/public_html/gantti-master/lib/gantti.php on line 44
You have to do as
while($obj = mysql_fetch_object($query)) {
$arr[] = array('label'=>$obj->label,'start'=>$obj->start,'end'=>$obj->end);
}
And then
$json_response = json_encode($arr);
Related
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']),
);
}
}
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']
);
}
I am trying to make a dynamic JSON array in PHP, however when I try to do so it returns "Array". Here is the code I am currently using:
<?php
require '../../scripts/connect.php';
$array = '';
if($result = $db->query("SELECT * FROM art") or die ($db->error)){
if($count = $result->num_rows) {
while($row = $result->fetch_object()){
$array .= array(
'title' => $row->title,
'image' => "http://www.thewebsite.com/img/2.jpg",
'rating' => 7.7,
'releaseYear' => 2003,
'genre' => array(
'0' => $row->category,
'1' => $row->subcategory
)
);
}
}
}
echo json_encode($array);
?>
Can anyone suggest how I might go about fixing this?
And if anyone has suggestions about creating a dynamic JSON array, some help would be much appreciated.
Change your declaration of $array to be an array:
$array = array();
Then in your while loop, when you add the new array to $array, push it like this:
$array[] = array('title'=>$row->title, etc...)
I am trying to make and return json data using codeigniter. I want to receive that data in this format
[
{
'title': 'this is title',
'desc': 'THis is desc'
},
{
'title': 'this is title',
'desc': 'THis is desc'
}
]
But I am receiving it this way
[[{"title":"this is title","desc":"this is desc"}],[{"title":"this is title","description":"this is desc"}]]
how can I change this format to above one?
here is my code
public function v1 () {
$this->load->model('model_jokes');
$jokes = $this->model_jokes->readJokes();
$arr = array();
foreach ($jokes as $joke) {
$arr[] = array(
array(
'title' => $joke->title,
'description' => $joke->joke
)
);
}
echo json_encode($arr);
}
Make the assignment inside foreach as
$arr[] = array(
'title' => $joke->title,
'description' => $joke->joke
);
Otherwise you will get a multi-dimensional array for each $joke.
You are adding array of array element each time in a loop. Instead just add single array.
public function v1 () {
$this->load->model('model_jokes');
$jokes = $this->model_jokes->readJokes();
$arr = array();
foreach ($jokes as $joke) {
$arr[] = array(
'title' => $joke->title,
'description' => $joke->joke
);
}
echo json_encode($arr);
}
Try :
echo '<pre>'.json_encode($arr).'</pre>';
$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"]);