Database Code

  Home arrow Database Code arrow Table Drawer (Powerful table dumper)
DATABASE CODE

Table Drawer (Powerful table dumper)
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-09-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    This is a script that you include into your file. It will draw a table for you. It's features include every-column sortable (asc/desc), multi-categories. I made this to be able to output tables quickly. Also, with a centralized drawing script, I can change the "theme" of my site easier, without using a real theme engine.

    Eg usage I use to output the different topics on my forum:
    $perpage = 15;
    $columns = array("title"=>"Topic", "numofposts"=>"numofposts", "lastpost"=>"Last post");
    $spacerColumn = "category";
    $spacerColumnSort = "$tables->categories.sortfromleft asc";
    $spacerLabel = '
    $row[categoryname]';
    $columnsDisplay = array("title"=>'"".$row["title"].""', "lastpost"=>'$row[lastupdated] . "->"');
    $defaultSort="sortOrder desc";
    $baseSQL = "select *, category as category, DATE_FORMAT($tables->forumTopics.lastupdated, \"%c-%e-%y at %r\") as lastupdated, $tables->forumTopics.id as id from $tables->forumTopics, $tables->categories where $tables->forumTopics.category=$cat and $tables->forumTopics.hidden=0 and $tables->forumTopics.category=$tables->categories.id";

    By : webhappy

    <?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>&nbsp;<a href=$_SERVER[PHP_SELF]?{$keep}sort=".urlencode($t).$temp.">$t</a>&nbsp;</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 " &nbsp; </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);
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap