PDA

View Full Version : Problems with TreeLoader from JSON Attributes with HTML TAGS or Line Break



nyvacol2005
7 Apr 2010, 2:36 PM
Hi,
I am developing with drupal and Ext JS.
Using an ExtJs form, I could save data to my database from an htmleditor widget but my TreeLoader could not load this data back because of line breaks and html tags generated by the htmleditor. Is this a bug with TreeLoader or a limitation with JSON.

Here is my code


$jsonArray = '[';

while($row = db_fetch_object($result)){

$opened = '<< Not Specified or Unknown >> ';
$notes = '<< Summary Unavailable >>';

$dateField = '';
$notesField = '';

if ($row->opened){
$opened = date('l, jS \of F Y', $row->opened);
$dateField = date('mdY', $row->opened);
}

if( trim($row->notes)){
$notes = $row->notes;
$notesField = $notes;
}

$jsonArray = $jsonArray.'{ id:"'.$row->cid.'", text: "'.$row->name.'", symbolicname: "'.$row->symbolicname.'", registered: "'.date('l, jS \of F Y h:i:s A',$row->dateReg).'", modified: "'.date('l, jS \of F Y h:i:s A',$row->dateModified).'", modified:"'.$row->dateModified.'", opened: "'.$opened.'", dateField:"'. $dateField .'", notes:"'.$notes.'", notesField:"'. $notesField.'", leaf: true,},';


}

$jsonArray = $jsonArray.']';

Please I need help on this. Or a better way of loading tree nodes with attributes containing line breaks and html.

nyvacol2005
7 Apr 2010, 10:12 PM
Found solution to the problem.
The error i made was manually forming the JSON string. Generally, avoid manual encoding. Rather construct an array and pass the array as an argument to php jason_encode function. Like here


$jsonArray[] = array('id' => $row->cid,'text'=>$row->name,'symbolicname' => $row->symbolicname, 'registered' => date('l, jS \of F Y h:i:s A',$row->dateReg), 'modified' => date('l, jS \of F Y h:i:s A',$row->dateModified), 'modified' => $row->dateModified, 'opened' => $opened, 'dateField' => $dateField, 'notes' => $notes, 'notesField' =>$notesField, 'leaf'=> true);




echo json_encode($jsonArray);

This will fetch you the desired result. Hopefully . . . :D