Board Logo

A PHP Function To Fill Content Array From MySQL Result Resource
Justin - 10/10/2006 at 03:40 PM

Here is a PHP function I wrote to automatically fill the content array from a php MySQL result resource. I know this code could be cleaner but it works, but if you can figure out a way to clean it up feel free to post it.

Code:

function fill_js_table_content_from_mysql($result_obj)
{
echo '<script type="text/javascript">var TABLE_CONTENT = [';

$i = 1;

while($row = mysql_fetch_array($result_obj))
{
$j = 0;

echo '[';
while($j < mysql_num_fields($result_obj))
{
if(is_numeric($row[$j]))
{
echo isnull($row[$j]);
}
else
{
echo '"' . isnull($row[$j]) . '"';
}

if(($j + 1) == mysql_num_fields($result_obj)) { break; }

echo ',';
$j++;
}

if($i == mysql_num_rows($result_obj)) { echo ']'; }
else { echo '],'; }
$i++;
}

echo '];</script>';
}


Your going to need this little function as well:

Code:

function isnull($input) {
if(empty($input))
{
return ('---');
}
else
{
return ($input);
}
}


Justin - 10/10/2006 at 03:42 PM

Here is an example usage:

<?php
$sql = "SELECT ins.date, ins.time FROM ins, transactions WHERE transactions.employee_id = " . mysql_smart_quote($_SESSION['session_id']) . " ORDER BY ins.date";
$result = mysql_query($sql) or die(fatal_error_alert(mysql_error(), $sql));
fill_js_table_content_from_mysql($result);
?>
<script type="text/javascript">
new TTable(TABLE_CAPT, TABLE_CONTENT, TABLE_LOOK);
</script>


Softcomplex: http://www.softcomplex.com/
Back to forum: http://www.softcomplex.com/forum/