Dynamic Data From Server (PHP) With DOM Objects
Dynamic Data From Server (PHP) With DOM Objects
thecountofzero
Posts: 21Questions: 0Answers: 0
I have the following requirement and am looking for examples on how to do so.
Retrieve data from server via PHP and display that data in the DataTable. Each row
will have plain text, textboxes, select dropdowns and checkboxes.
I see from the examples that it is possible to have these DOM objects (textboxes...)
at the content of a column, but what I would like to see is some sample code that
dynamically does this from the PHP.
Basically I am looking for the format of the PHP string that gets written and then loaded
by the DataTable.
I was looking at the server-side processing example and saw the following:
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['engine']).'",';
$sOutput .= '"'.addslashes($aRow['browser']).'",';
$sOutput .= '"'.addslashes($aRow['platform']).'",';
if ( $aRow['version'] == "0" )
$sOutput .= '"-",';
else
$sOutput .= '"'.addslashes($aRow['version']).'",';
$sOutput .= '"'.addslashes($aRow['grade']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
This example only handles plain strings for the column data. If I wanted the first column
to be a textbox, would it be as simple as the following:
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$sOutput .= "[";
$sOutput .= '"SOME TEXT",';
$sOutput .= '"'.addslashes($aRow['browser']).'",';
$sOutput .= '"'.addslashes($aRow['platform']).'",';
if ( $aRow['version'] == "0" )
$sOutput .= '"-",';
else
$sOutput .= '"'.addslashes($aRow['version']).'",';
$sOutput .= '"'.addslashes($aRow['grade']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
Thanks in advance,
Mike
Retrieve data from server via PHP and display that data in the DataTable. Each row
will have plain text, textboxes, select dropdowns and checkboxes.
I see from the examples that it is possible to have these DOM objects (textboxes...)
at the content of a column, but what I would like to see is some sample code that
dynamically does this from the PHP.
Basically I am looking for the format of the PHP string that gets written and then loaded
by the DataTable.
I was looking at the server-side processing example and saw the following:
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['engine']).'",';
$sOutput .= '"'.addslashes($aRow['browser']).'",';
$sOutput .= '"'.addslashes($aRow['platform']).'",';
if ( $aRow['version'] == "0" )
$sOutput .= '"-",';
else
$sOutput .= '"'.addslashes($aRow['version']).'",';
$sOutput .= '"'.addslashes($aRow['grade']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
This example only handles plain strings for the column data. If I wanted the first column
to be a textbox, would it be as simple as the following:
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$sOutput .= "[";
$sOutput .= '"SOME TEXT",';
$sOutput .= '"'.addslashes($aRow['browser']).'",';
$sOutput .= '"'.addslashes($aRow['platform']).'",';
if ( $aRow['version'] == "0" )
$sOutput .= '"-",';
else
$sOutput .= '"'.addslashes($aRow['version']).'",';
$sOutput .= '"'.addslashes($aRow['grade']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
Thanks in advance,
Mike
This discussion has been closed.
Replies
See http://php.net/manual/en/function.json-encode.php for info on converting a PHP array into JSON.
[code]// Smarty tag: jqdataTable
$datatypes = array('C'=>'string','N'=>'numeric','D'=>'date','X'=>'currency');
$keyids = array();
$aoColumns = array();
$i=0;
foreach($fieldlist as $f) { // process each column definition
$sName = $f['name'];
$sClass = $f['class'];
$bSearchable = $f['filter'] == 'Y' ? TRUE : FALSE;
$labeltxt = $f['label'];
$sType = $datatypes[$f['datatype']] != '' ? $datatypes[$f['datatype']] : 'html';
if (in_array($sName, $keyfields)) $keyids[$i] = $sName;
$aoColumns[] = array(
"sTitle" => $labeltxt,
"sName" => $sName,
"sClass" => $sClass,
"bSearchable" => $bSearchable,
"sType" => $sType,
);
$i++;
}
// Note: this is javascript code in a php string
$fnRowCallback = "function (nRow, aData, iDisplayIndex) {
var keyids = [".implode(',', array_keys($keyids))."];
var vals = [];
for ( var i in keyids ) {
vals.push(aData[keyids[i]]);
}
$(nRow).attr(\"id\",vals.valueOf());
return nRow;
}";
$dataTable = array(
'aoColumns' => $aoColumns,
'sPaginationType' => "full_numbers",
'oLanguage' => array("sSearch" => "Search all columns:"),
'bPaginate' => TRUE,
'bLengthChange' => TRUE,
'bFilter' => TRUE,
'bSort' => FALSE,
'bInfo' => TRUE,
'bAutoWidth' => FALSE,
'bProcessing' => FALSE,
'bServerSide' => TRUE,
'sAjaxSource' => "query.htm?formid=$p_formid&summary=Y",
'fnRowCallback' => "FNROWCALLBACK", // How do you embed a javascript function into JSON!?
);
$smarty->assign('fnRowCallback', "$fnRowCallback");
$smarty->assign($assign, json_encode($dataTable));[/code]Then, in my smarty template, I have this: [code] oTable = $("table#{/literal}{$formid}{literal}_summary").dataTable(
{/literal}{jqdataTable formid=$formid summary='Y' assign='dTable'}{$dTable|replace:'"FNROWCALLBACK"':$fnRowCallback}{literal}
);
[/code]And last but not least, the callback code to actually get the data:[code] $sColumns = array();
foreach($fieldlist as $f) {
$sColumns[] = $f['name'];
}
$aaData = array();
// NOTE: populate aaData
$sOutput = array(
'sEcho' => intval($_GET['sEcho']),
'sColumns' => implode(',',$sColumns),
'iTotalRecords' => ($tablerowcnt),
'iTotalDisplayRecords' => ($entrycnt),
'aaData' => $aaData,
);
echo json_encode($sOutput);[/code]