AJAX Datasouce - is this the best way of creating it?
AJAX Datasouce - is this the best way of creating it?
matyhaty
Posts: 11Questions: 1Answers: 0
Hi All
Ive been using Datatables for a little while now, but by no means close to an expert (as the below will show!)
Im looking to move from DOM source to AJAX
Im using Codeigniter for the project, along with Datamapper. Datamapper is a ORM which creates a object. In the below code the object is $persons.
I need to be able to 'refresh' the data (hence moving away from DOM).
AJAX would be the better way, however the code below seems a long winded way for creating the array
Can anyone point me to a better place?
Thanks
[code]
<?php
$cols = array(
'Username' => 'username',
'Firstname' => 'firstname',
'Surname' => 'surname'
);
$aoColumns = '';
foreach($cols as $ckey => $cval)
{
$aoColumns .= '{ "sTitle": "'.$ckey.'" },
';
}
$aaData = '';
foreach($persons as $p)
{
$aaData .= '[ ';
foreach($cols as $ckey => $cval)
{
$aaData .= '"'.$p->$cval.'", ';
}
$aaData .= ' ], ';
}
?>
{
"aaData": [<?php echo $aaData; ?>],
}
[/code]
Ive been using Datatables for a little while now, but by no means close to an expert (as the below will show!)
Im looking to move from DOM source to AJAX
Im using Codeigniter for the project, along with Datamapper. Datamapper is a ORM which creates a object. In the below code the object is $persons.
I need to be able to 'refresh' the data (hence moving away from DOM).
AJAX would be the better way, however the code below seems a long winded way for creating the array
Can anyone point me to a better place?
Thanks
[code]
<?php
$cols = array(
'Username' => 'username',
'Firstname' => 'firstname',
'Surname' => 'surname'
);
$aoColumns = '';
foreach($cols as $ckey => $cval)
{
$aoColumns .= '{ "sTitle": "'.$ckey.'" },
';
}
$aaData = '';
foreach($persons as $p)
{
$aaData .= '[ ';
foreach($cols as $ckey => $cval)
{
$aaData .= '"'.$p->$cval.'", ';
}
$aaData .= ' ], ';
}
?>
{
"aaData": [<?php echo $aaData; ?>],
}
[/code]
This discussion has been closed.
Replies
[code]
<?php
$cols = array(
'Username' => 'username',
'Firstname' => 'firstname',
'Surname' => 'surname'
);
$json_object = array(
"aaData" => $cols
);
echo json_encode($json_object);
?>
[/code]