<?php

function table2js($r) {
/*
   Given an SQL resultset, format it as a 2-D array for javascript

See also: tablesort.html

   first determine numeric types, don't quote, 
   that they may be sorted numerically in Javascript (but account for nulls)
*/
for ($c=0; $c < pg_num_fields($r); $c++)
{ $t=   pg_field_type($r, $c);
 $t = $t[0];
 $q[$c]=($t=='i' or $t=='f' or $t=='n')? '':'"';
}
$comma=''; print "table = new Array(\n";
while ($row = pg_fetch_row($r))
{  print $comma;
   row2jsarray($row, $q);
     $comma =",\n";
}
print ")\n"; 
}  // end table2js
function row2jsarray($row, $q) {
print "  new Array(";
$comma='';
foreach ($row as $c=>$val):
   $val=htmlspecialchars($val);  // could have double quote
   $d=$q[$c];		 // " "  for chars, no quoutes for numbers
   $val = "$d$val$d";    // in case of jumeric null, this could be void, prevent that!
   if ($val=='') $val = "''";
	print "$comma $val";
   $comma =',';
endforeach;
print ")";
}  // end row2jsarray

?>