Results 1 to 2 of 2

Thread: order by links

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    45

    order by links

    Hi, im doing a ORDER BY php tutorial. I've followed every step carefully but it still won't work. I've copied the code from the tutorial and replaced all necessary parts but still it wont work. Someone that could point out the error?
    PHP Code:
    <?php
    /* set the allowed order by columns */
    $default_sort 'id';
    $allowed_order = array ('id''titel','genre''bolag');

    /* 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 ('xxxxxxx','xxxxxxxxx','xxxxxxx');
    mysql_select_db ('xxxxxxxxx');

    /* construct and run our query */
    $query "SELECT * FROM filmer 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";
    ?>
    Last edited by diablo_programmer; Aug 3rd, 2007 at 06:05 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width