Re: Newbie with sorting problem

From: Date: Tue, 06 Jan 2004 12:44:29 +0000
Subject: Re: Newbie with sorting problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-173841@lists.php.net to get a copy of this message
Just add order by to the sql query instead eg "SELECT id, name, surname, address FROM addresses order by name asc"; or descending on surname eg "SELECT id, name, surname, address FROM addresses order by surname desc"; hope this helps Pete Bart Koppe wrote:
Hi all, I'm quite a newbie ot php/mysql, so my question might be oversimple (though i tried for hours to understand..) I'm making an addressbook, as a try out, to see how it al works. All is great actually, but only the sorting of the addresses seems complicated for me.. I have a databse called 'addressen', and four fields of this db are called 'id', 'name', 'surname' and 'address' To display the content i use this: <?
    include( "file_that_connects_with_my_database.php");
    $result = mysql_query(
                  "SELECT id, name, surname, address FROM addresses");
      if (!$result) {
          echo( "<p>Error performing query: " .
                  mysql_error() . "</p>");
           exit();
      }
       while ( $row = mysql_fetch_array($result) ) {
         $addressid = $row["id"];
          $nametext = $row["name"];
           $surnametext = $row["surname"];
           $addresstext = $row["address"];
           //the table
          echo( "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td>");
          echo( $addressid. "</td><td>");
          echo( $nametext. "</td><td>");
          echo( $surnametext. "</td><td>");
          echo( $addresstext "</td><td>");
          echo( "</tr></table>");
      }
?> This scripts works, but.. my desire is now to be able to sort the output, wich is generated by the 'while' loop, for instance on 'name', or on 'surname' . sort(), ksort()? I couldn't understand the explanations i found (and so declared myself as newbie :) Can anybody help me with this? I would be delighted... Thanks in advange! Bart Koppe


« previous php.general (#173841) next »