Database Articles

  Home arrow Database Articles arrow Page 2 - Sorting Database Results with PHP
DATABASE ARTICLES

Sorting Database Results with PHP
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 17
    2002-09-24

    Table of Contents:
  • Sorting Database Results with PHP
  • The Code

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Sorting Database Results with PHP - The Code


    (Page 2 of 2 )

    <?php
    /* set the allowed order by columns */
    $default_sort 'last_name';
    $allowed_order = array ('joindate''last_name','first_name');

    /* if order is not set, or it is not in the allowed
     * list, then set it to a default value. Otherwise, 
     * set it to what was passed in. */
    if (!isset ($_GET['order']) || 
        !
    in_array ($_GET['order'], $allowed_order)) {
        
    $order $default_sort;
    } else {
        
    $order $_GET['order'];
    }

    /* connect to db */
    mysql_connect ('localhost','user','pass');
    mysql_select_db ('test');

    /* construct and run our query */
    $query "SELECT * FROM sometable ORDER BY $order";
    $result mysql_query ($query);

    /* make sure data was retrieved */
    $numrows mysql_num_rows($result);
    if (
    $numrows == 0) {
        echo 
    "No data to display!";
        exit;
    }

    /* now grab the first row and start the table */
    $row mysql_fetch_assoc ($result);
    echo 
    "&lt;TABLE border=1&gt;\n";
    echo 
    "&lt;TR&gt;\n";
    foreach (
    $row as $heading=&gt;$column) {
        
    /* check if the heading is in our allowed_order
         * array. If it is, hyperlink it so that we can
         * order by this column */
        
    echo "&lt;TD&gt;&lt;b&gt;";
        if (
    in_array ($heading$allowed_order)) {
            echo 
    "&lt;a href=\"{$_SERVER['PHP_SELF']}?order=$heading\"&gt;$heading&lt;/a&gt;";
        } else {
            echo 
    $heading;
        }                
        echo 
    "&lt;/b&gt;&lt;/TD&gt;\n";
    }
    echo 
    "&lt;/TR&gt;\n";

    /* reset the $result set back to the first row and 
     * display the data */
    mysql_data_seek ($result0);
    while (
    $row mysql_fetch_assoc ($result)) {
        echo 
    "&lt;TR&gt;\n";
        foreach (
    $row as $column) {
            echo 
    "&lt;TD&gt;$column&lt;/TD&gt;\n";
        }
        echo 
    "&lt;/TR&gt;\n";
    }
    echo 
    "&lt;/TABLE&gt;\n";
    ?>


    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.
    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


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