SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: while

  1. #1
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Norway
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    while

    hey, I'm a newbie at php and I usually use while when printing results from a database like the one below
    PHP Code:
      while ($row mysql_fetch_object ($result))
       { 
      echo 
    $row->data_from_database;
      } 
    I just wanted to know if there is any other way to print data from a query without using while or any loops ? maybe assign the data to an array or something

    Thanks

  2. #2

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, there isnt really another option. Do you have a particular reason for asking?

  3. #3
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Norway
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes. I'm posting my code I use for a database search here:
    PHP Code:
     <?php
     
    require("dbsettings.php");
     include (
    "header.php");
     
     
    //Function.php > Search
             
     /*Next-Previous Settings*/
     
    if(!isset($start)) 
         
    $start 0
         
    $next=$start $end;
         
    $prev=$start $end;
         
    $end=25;
                     
         if (
    $country <> "") {
         
                 
    $color1 "#F5F5F5";  
                 
    $color2 "#FFFFFF";  
                 
    $row_count 0;
                                     
     
    //Form Variables
     
    $order $_POST["order"];
     
    $field $_POST["field"];
     
     
    $order $_REQUEST["order"];
     
    $field $_REQUEST["field"];
     
     
    $query "
     select users.uid as userid
          , users.name as username
          , users.state as statename
          , users.city as cityname
          , country.id as countryid
          , country.country as countryname
       from users
     inner
       join country
         on users.country = country.id
      where users.country = '
    $country' ORDER BY ".$field." ".$order." LIMIT ".$start.", ".$end." ";
     
     
    $result mysql_query ($query$connection) or die("SELECT Error [$query]:".mysql_error());
     
     echo 
    "<fieldset><legend>Search Results for </legend>";
     
     if (isset(
    $order)) {
     echo 
    "Sort $field $order";
     }
     else {echo 
    "You can also do an <a href=\"index.php?option=search\">advanced search</a>";}
             echo 
    "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
             echo 
    "<tr class=\"search_column\" valign=\"top\">";
             echo 
    "<td width=\"20%\">Country</td>
                   <td width=\"20%\"><a href=\"search.php?country=" 
    .$country"&search=active&field=statename&order=asc\"><img src='asc.png' border='0' hspace='3'/></a> State <a href=\"search.php?country=" .$country"&search=active&field=statename&order=desc\"><img src='desc.png' border='0' hspace='3'/></a></td>
                   <td width=\"20%\"><a href=\"search.php?country=" 
    .$country"&search=active&field=cityname&order=asc\"><img src='asc.png' border='0' hspace='3'/></a> City <a href=\"search.php?country=" .$country"&search=active&field=cityname&order=desc\"><img src='desc.png' border='0' hspace='3'/></a></td>
                   <td width=\"20%\"><a href=\"search.php?country=" 
    .$country"&search=active&field=username&order=asc\"><img src='asc.png' border='0' hspace='3'/></a> Name <a href=\"search.php?country=" .$country"&search=active&field=username&order=desc\"><img src='desc.png' border='0' hspace='3'/></a></td>";
             echo 
    "</tr>";
             
                     while (
    $row mysql_fetch_object ($result))
                     {
                         
    $row_color = ($row_count 2) ? $color1 $color2;
                         
                     
                             echo    
    "<tr class=\"middle\" valign=\"top\">";
                             echo    
    "<td bgcolor=\"$row_color\">" $row->countryname "</td>";
                             echo    
    "<td bgcolor=\"$row_color\">" $row->statename "</a></td>";
                             echo    
    "<td bgcolor=\"$row_color\">" $row->cityname "</td>";
                             echo    
    "<td bgcolor=\"$row_color\"><a href=\"profile.php?uid=" $row->userid "\">" $row->username "</a>"
                             echo    
    "</td></tr>";
     
                         
    $row_count++; 
                     } 
     
             echo 
    "</td>";
             echo 
    "</tr>";
             echo 
    "<tr valign=\"bottom\" align=\"center\">";
             echo 
    "<td align=\"center\" colspan=\"4\">";
                             
             
    //second script added for navigation
                 
    $query "SELECT count(*) FROM users WHERE (country = ".$country.")";
                 
    $result mysql_query($query$connection) or die("SELECT Error [$query]:".mysql_error());
                 
    $numrows array_shift(mysql_fetch_row($result)); 
     
                         
                     if(
    $start 0
                     {
                         echo 
    " <b><a href=\""$PHP_SELF ."?start=".($prev $end). "&country=" .$country"&search=active&field=".$field."&order=".$order."\"><<</a></b> ";
                     }
                                 
                     if(
    $numrows > ($start $end)) 
                     {
                         echo 
    " <b><a href=\""$PHP_SELF ."?start=" .($start $end). "&country=" .$country"&search=active&field=".$field."&order=".$order."\">>></a></b> ";
                     }
                                     
             echo 
    "</td>";
             echo 
    "</tr>";
             echo 
    "<tr>";
             echo 
    "<td height=\"10\" colspan=\"3\" class=\"bottom\">There are ".$numrows." player(s) in your country</td>";
             echo 
    "</tr>";
             echo 
    "</table>";
     
         }
     echo 
    "</fieldset>";
     
     if (!isset(
    $country)) {
     echo 
    "You must have a more specific search, please try again <a href=\"index.php?option=search\">here</a>";
     }
     include (
    "footer.html");
      
    ?>
    and I want this line:
    PHP Code:
     echo "<fieldset><legend>Search Results for </legend>"
    to show the country the search was done for, but this line is before the while loop so I'm not sure how to do that without using another db query (if at possible)

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is $country a number or a string? You could output that in the line as

    PHP Code:
     echo "<fieldset><legend>Search Results for $country</legend>"
    One option to access a specific row and column of a mysql result set is to use mysql_result().

  5. #5
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Norway
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $country variable is a number

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, generally if you already know the value, just output it before loop:

    PHP Code:
    echo "foo $value bar";
    while(
    $row mysql_fetch_xxx())
       
    output info 
    If you don't know the value (i.e. it comes from query) here is the simple (though pretty silly) way to do it:

    PHP Code:
    $first_time TRUE;
    while(
    $row mysql_fetch_xxx()) {
        if(
    $first_time) {
               echo 
    $row["some_value"];
               
    $first_time FALSE;
         }
         
    output other info from $row


  7. #7
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha I use the above all the time, is there a better option then ^^ ??

    Quaint Tech
    - Blog on web development and web technology.


Bookmarks

Posting Permissions

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