Database Code
  Home arrow Database Code arrow Table Drawer (Powerful table dumper)
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Webcast: What is new in Viper 2 for developers?

    Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications.
    FREE! Go There Now!


    NEW! IBM – Taking Web 2.0 to Work

    David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    Role of Integrated Requirements Management in Software Delivery

    As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change.
    FREE! Go There Now!


    NEW! Webcast: Extreme transaction processing with WebSphere Extended Deployment

    In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications.
    FREE! Go There Now!


    NEW! Rational Build Forge Express eKit

    Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast.
    FREE! Go There Now!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Software Change and Configuration Management Solution Guidelines

    This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management.
    FREE! Go There Now!


    NEW! Download a free trial of WebSphere Business Modeler Advanced V6.1.1

    Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z

    IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects.
    FREE! Go There Now!


    NEW! Maintaining QoS and Process Integrity in an SOA Environment

    This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    DATABASE CODE ARTICLES

    - 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
    - CSV to SQL convertor





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek