Jump to content

[SOLVED] Php json array problem


refiking

Recommended Posts

I'm not sure what I'm doing wrong, but when I have hardcoded dummy data, it works perfectly, but when I try to dynamically pull it from the db, I run into trouble.

 

This is what the dummy data returns:

[{"title":"Parking Lot Sp..","start":"2009-10-30","url":"#","className":"fin"},{"title":"Crosswalk Fix...","start":"2009-11-6","url":"#","className":"due1"},{"title":"LQ Continuation","start":"2009-11-8","url":"#","className":"due3"},{"title":"PS City Hall","start":"2009-11-11","url":"#","className":"due3"},{"title":"125, Main St","start":"2009-11-23","url":"#","className":"due5"},{"title":"James Post Re","start":"2009-11-23","url":"#","className":"due5"}]

This is the code for the dummy data

<?php


$year = date('Y');
$month = date('m');
$premonth = date('m')-1;
$nextmonth = date('m')+1;

echo json_encode(array(

	array(
		'title' => "Parking Lot Sp..",
		'start' => "$year-$premonth-30",
		'url' => "#",
		'className' => "fin"
	),

	array(
		'title' => "Crosswalk Fix...",
		'start' => "$year-$month-6",
		'url' => "#",
		'className' => "due1"
	),

	array(
		'title' => "LQ Continuation",
		'start' => "$year-$month-8",
		'url' => "#",
		'className' => "due3"
	),

	array(
		'title' => "PS City Hall",
		'start' => "$year-$month-11",
		'url' => "#",
		'className' => "due3"
	),

	array(
		'title' => "125, Main St",
		'start' => "$year-$month-23",
		'url' => "#",
		'className' => "due5"
	),

	array(
		'title' => "James Post Re",
		'start' => "$year-$month-23",
		'url' => "#",
		'className' => "due5"
	)


));

?>

 

This is what the dynamic data returns:

["Array,Array,Array,Array,Array"]

 

And here is the code for that page:

$sql = mysql_query("select name, date from jobs")or die(mysql_error());
$num = mysql_num_rows($sql);
$i = 1;
while($row = mysql_fetch_assoc($sql)){
$arrays .= 	array('title' => $row['name'], 'start' => date("Y-m-d", $row['date']), 'url' => "#", 'className' => "fin");
if ($num != $i){ $arrays .= ','; }
$i++;	
}


echo json_encode(array($arrays));

Link to comment
https://forums.phpfreaks.com/topic/182035-solved-php-json-array-problem/
Share on other sites

You are concatenating it like a string data:

$arrays .= 	array('title' => $row['name'], 'start' => date("Y-m-d", $row['date']), 'url' => "#", 'className' => "fin");

 

Try it like this:

$arrays[] = 	array('title' => $row['name'], 'start' => date("Y-m-d", $row['date']), 'url' => "#", 'className' => "fin");

 

It should work, I would also add this:

$i = 1;
$arrays=array();
while($row = mysql_fetch_assoc($sql)){

 

Just to define it :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.