Php Programming Code Examples
Php > Database Related Code Examples
Table Drawer (Powerful table dumper)
Table Drawer (Powerful table dumper)
<?php
/*
To use, set the following variables as shown:
$perpage = 5;
$showPageChoice=1;
$columns = array("thread"=>"thread link", "member"=>"member name");
$spacerColumn; //will add a space between rows that do not have the same value in spacerColumn, used mainly for the forums
$columnsPre; //array that is similar in structure to columnsDisplay, but this one is eval'd BEFORE the display one, the dislpay one is echo'd
$columnsDisplay = array("member"=>"<a href=$SITE_ROOT/viewuser.html?id=".$row["member"].">".$row["member"]."</a>");
$defaultSort;
$baseSQL = "select * from $tables->forumMessages";
//NOTE! the columns array goes like this: REAL column name => description
//Hides your column names for more protection against SQL attacks
//hides your table names too
*/
if ( empty($page) ) {$page = 1;}
$finalSQL = $baseSQL;
if ( isset($spacerColumnSort) )
{
$finalSQL .= " order by " . $spacerColumnSort;
$orderSyntaxStarted=true;
}
if ( !empty($sort) )
{
if ( !in_array($sort, $columns) )
finalizeAndDie("Invalid sort key specified; Hack attempt of type SQL-injection has been logged");
$temp =array_keys($columns, $sort);
if ( $orderSyntaxStarted )
$finalSQL .= ",". $temp[0];
else
$finalSQL .= " order by " . $temp[0];
$sortFlags = $sortflags;
if ( $sortFlags == "reverse" )
$finalSQL .= " desc";
}
else if ( isset($defaultSort) )
if ( $orderSyntaxStarted )
$finalSQL .= "," . $defaultSort;
else
$finalSQL .= " order by " . $defaultSort;
$finalSQL .= " limit " . $perpage * ($page-1) . "," . $perpage;
$saveFinalSQL = $finalSQL;
//echo $finalSQL . '<hr>';
$finalSQL = mysql_query($finalSQL);
$total = mysql_query($baseSQL);
$sqlcount = mysql_num_rows($total);
$pagemin = ceil($sqlcount / $perpage);
$choosepage = "Pages: ";
for ( $n=1; $n <= $pagemin; $n++ )
{
if ( $n > 1 )
$choosepage .= " ";
if ( $n == $page )
$choosepage .= "<b>$n</b>";
else
$choosepage .= "<a href='$_SERVER[PHP_SELF]?page=$n&sort=".addslashes($sort)."&sortFlags=$sortFlags'>$n</a>";
}
if ( $showPageChoice == 1)
echo $choosepage;
//echo "</p>";
$keep="";
if ( isset($keepStatic) )
{
foreach ( $keepStatic as $t )
$keep .= "$t={$$t}";
$keep .= "&";
}
echo "<table border=0 cellpadding= cellspacing=5>";
$i=0;
foreach ( $columns as $t )
{
if ( $t == $sort && $sortFlags != "reverse" )
$temp = "&sortflags=reverse title='Click to sort in descending order by ".addslashes($t)."'";
else
$temp = " title='Click to sort in ascending order by ".addslashes($t)."'";
echo "<th> <a href=$_SERVER[PHP_SELF]?{$keep}sort=".urlencode($t).$temp.">$t</a> </th>";
}
$i=0;
while ( $row = mysql_fetch_array($finalSQL) )
{
if ( isset($spacerColumn) )
{
if ( $row[$spacerColumn] != $spacerTracker || !isset($spacerTracker) )
{
$i=0;
echo "<tr><td colspan=".sizeof($columns).">";
//echo $spacerLabel;
$spacerEval = 'echo "' . $spacerLabel . '";';
eval ($spacerEval);
echo " </td></tr>";
}
$spacerTracker=$row[$spacerColumn];
}
if ( $i++ % 2 == 0 )
echo "<tr bgcolor=B0C4DE>";
else
echo "<tr bgcolor=FOF8FF>";
foreach ( array_keys($columns) as $t )
{
echo "<td>";
if ( isset($columnsPre[$t]) )
{
//echo $columnsPre[$t];
eval($columnsPre[$t]);
}
if ( isset($columnsDisplay[$t]) )
{
eval("echo ".$columnsDisplay[$t].";");
}
else
echo $row[$t];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
if ( $showPageChoice == 1)
echo "</p>" . $choosepage;
//Unitialize vars, cuz some are optional and if someone calls this page twice on a page, some variables may remain from the first call (and optional ones on teh second one will be set to the first one's settings)
if ( isset($spacerColumn) ) unset ($spacerColumn);
?>