Results 1 to 4 of 4

Thread: [solved] arranging sorted result

  1. #1

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    [solved] arranging sorted result

    i don't know if my subject meets my need here. hehe. i'm quiet new to this thing and done a 'hello world" so far. i wanted to sort a result by asc/desc when sort button is clicked. this is what i've got so far.
    PHP Code:
    <style>
    body,table,input{
       font:9pt arial;
    }
    td,th{
       padding:3px;
    }
    th{
       color:white;
    }
    </style>
    <?php
       mssql_connect
    ("x","sa","password");
       
    mssql_select_db("northwind");
       echo
    "<input type=submit value='sort'><br><br>";
       echo
    "<table cellspacing=1 bgcolor=#666699 width=100%>";
       echo
    "<tr><th>ID</th><th>Description</th><th>Region</th></tr>";
       
    load("select * from territories");
       echo
    "</table><br>";
       
       function 
    load($s){
          
    $result=mssql_query($s);
          
    $i=0;
          while(
    $row=mssql_fetch_array($result,MSSQL_NUM)){
             if(
    $i%2==0) echo"<tr bgcolor=white>";
             else echo
    "<tr bgcolor=#f4f4f4>";
             foreach(
    $row as $r) echo "<td>$r</td>";
             echo
    "</tr>";
             
    $i++;
          }
       }
    ?>
    i don't know how to have the page knowing wether it's asc/desc sort of the result. any help would be greatly appreciated.
    Last edited by brown monkey; Aug 4th, 2004 at 04:57 AM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: arranging sorted result

    The best way to do this is to use a link which reverses the sorty order. You can append a query string to a link refering back to your PHP script and pick up the value from the $_GET array.

    You can see a similar method used by these forums. The following link:
    Code:
    http://www.vbforums.com/showthread.php?s=&threadid=299757
    The bit after the question mark is the query string. You can then pick up the values of these variables in your PHP script as follows:
    PHP Code:
    $thread $_GET['threadid']; 
    I have made a slight modification to your code to enable you to sort.
    PHP Code:
    <?php
      mssql_connect
    ("x","sa","password");
      
    mssql_select_db("northwind");

      
    /* get the search order if specified
         1 - is for ascending
         0 - is for descending
       */
      
    $order = isset ($_GET['order'])?int_val($_GET['order']):1  

      
    if ($order == 0)
        
    $orderby 'ORDER BY [i]ColName[/i] DESC';
      else if (
    $order == 1)
        
    $orderby 'ORDER BY [i]ColName[/i] ASC';
      else
        
    $orderby ''
    ?>
    <table cellspacing="1" bgcolor="#666699" width="100%">
      <tr>
        <th>ID</th>
        <th>Description</th>
        <th>Region</th>
      </tr>
    <?php load("select * from territories $orderby"); ?>
    </table>
    <a href="<?php echo ($_SERVER['PHP_SELF'] . 'order=' . !$order?>">Sort</a>
    <?php
       
    function load($s){
          
    $result=mssql_query($s);
          
    $i=0;
          
          while(
    $row=mssql_fetch_array($result,MSSQL_NUM)) {
            if(
    $i%2==0
              echo
    "<tr bgcolor=white>";

            else 
              echo
    "<tr bgcolor=#f4f4f4>";
      
            foreach(
    $row as $r) echo "<td>$r</td>";
              echo
    "</tr>";
            
            
    $i++;
          }
       }
    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    thanks for the reply mate. i'll try this when i get home. thanks very much.

  4. #4

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    hehehe. thanks mate. sooo much. this is my code now.
    PHP Code:
    <?php
       mssql_connect
    ("x","sa","password");
       
    mssql_select_db("northwind");

       
    $order=1;
       
    $order=($_GET['order']==1)?0:1;

       
    $orderby=($order==0)?'order by territoryid desc':'order by territoryid asc';
    ?>

    <table cellspacing="1" bgcolor="#666699" width="100%">
    <tr><th>ID</th>
       <th>Description</th>
       <th>Region</th>
    </tr>

    <?php load("select * from territories $orderby"); ?>
    </table>
    <a href="<?php echo ($_SERVER['PHP_SELF'] . '?order=' $order)?>">Sort</a>
    <?php
       
    function load($s){
          
    $result=mssql_query($s);
          
    $i=0;
          while(
    $row=mssql_fetch_array($result,MSSQL_NUM)) {
             if(
    $i%2==0) echo"<tr bgcolor=white>";
             else echo
    "<tr bgcolor=#f4f4f4>";
             foreach(
    $row as $r) echo "<td>$r</td>";
             echo
    "</tr>";
             
    $i++;
          }
       }
    ?>

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