/* /----------------
************** showtable()
/* \----------------------> Displays table from db
Example Usage:
$dbfields = array('id', 'rule', 'topic_id', 'votes', 'poster', 'time');
// Fields to be displayed
$tabletitles=array('Id', 'Rule', 'Topic', 'Votes', 'Poster', 'Time', 'mod', 'del');
// Desired Titles of table to be shown
$extra=array(
'items'=> array (2),
'i1'=>array('table'=>'topics', 'f1'=>'id_topic', 'f2'=>'topic')
);
// Replace third row (which has numeriacl values) with values from another table, that corresponds to those numbers
showtable('mytable', 'id', $dbfields , $tabletitles, $extra);
// mytable = table name, id = name of primary key
*/
function showtable ($table, $order, $fields, $naslovi="", $extra="", $where="") {
$con=dbopen();
$query="SELECT ";
foreach($fields as $value)
{
$query.=$value.", ";
}
$query = rtrim($query, ', ');
$query.=" FROM $table $where ORDER BY $order";
$result = dbq($query, $con);
dbclose($con);
if (is_array($naslovi)) $number_naslovi = count($naslovi);
if (is_array($fields)) $number_cols = count($fields);
else $number_cols = mysql_num_fields($result);
//echo "Tabela: $table2show";
echo "
\n";
echo "\n";
for ($i=0; $i<$number_naslovi; $i++)
{
echo "| " . $naslovi[$i] . " | \n";
}
echo "
\n"; //end table header
/*$number_cols = mysql_num_fields($result);
echo "\n";
echo "\n";
echo "| ID | \n".$naslovc." | \n";
echo "
\n"; //end table header
*/
//table body
while ($row = mysql_fetch_row($result))
{
//if (!isset($boja)) $boja = '#99ccff';
// $boja = ($boja=='#99CCFF' ? '#CCCCCC' : '#99CCFF');
if (!isset($boja)) $boja = 'tr2';
$boja = ($boja=='tr1' ? 'tr2' : 'tr1');
// echo '' . "\n";
// echo '
' . "\n";
echo '
' . "\n";
for ($i=0; $i<$number_cols; $i++)
{
echo "| ";
if (!isset($row[$i])) //test for null
{echo "NULL";}
else {
if (is_array($extra)) {
$doned = 0;
$xz = count($extra['items']);
for ($z=0; $z<$xz; $z++)
{
if ($i == $extra['items'][$z]) {
$nj = $z + 1;
$itm = 'i'. $nj;
$con=dbopen();
$ff2= $extra[$itm]['f2'];
$tbl2= $extra[$itm]['table'];
$ff1= $extra[$itm]['f1'];
$query = "SELECT $ff2 FROM $tbl2 WHERE $ff1='$row[$i]'";
$result2 = dbq($query, $con);
dbclose($con);
$line2= mysql_fetch_array($result2);
echo $line2['0'];
$doned = 1;
}
}
}
if ($doned != 1) {
//echo $row[$i];
$row[$i] = strip_tags($row[$i]);
if (strlen($row[$i]) > 100) $row[$i]=substr( $row[$i], 0, strpos( $row[$i], ' ', 100 ) )."...";
echo $row[$i];
unset($doned);
}
}
echo " | \n";
}
echo ' |  | ';
echo "
\n";
}
echo "
";
}