Jump to content


Photo

getting stuff from MySQL DB and looping a display


  • Please log in to reply
2 replies to this topic

#1 loganbest

loganbest
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 08 April 2006 - 11:37 PM

I have been trying to get stuff to display on a page in a certain order from a DB. I have the connection done and keep meesing up the queries and the display looping.

Table name: magazines
Column: issue_id
Column: title
Column: num_of_articles

I need it to display in order of Issue ID


Can anyone help?

#2 IceHawk

IceHawk
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 09 April 2006 - 03:01 AM

The basic query you're looking for is;
select * from magazines order by issue_id asc
or
select * from magazines order by issue_id desc

The first will order the results on issue_id is ascending order, the second in descending order.
[a href="http://www.devtext.com" target="_blank"]DevText a community for all languages[/a]

#3 loganbest

loganbest
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 09 April 2006 - 03:52 AM

Yea I realized what I was doing wrong about 10 minutes after I posted this. For anyone else who had this similar question, here is my code:
total magazines created: 
   <b> <? db_connect();  
$result=mysql_query("SELECT * FROM magazines");
       $num_rows = mysql_num_rows($result);
       echo $num_rows;
       if ($num_rows == "0"){
              
       echo '<h1>There are no Magazines!</h1><br><br>';
       
      } else if (!$num_rows == "0"){ 
    //MySQL Query to get the data
       $sql = mysql_query("SELECT * FROM magazines ORDER BY issue_id ASC") or die (mysql_error());
       //prints the table
       echo '<br><br><table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td><b>Magazine Name</b></td>
        <td width="100"><b># of Articles</b></td>
    </tr>'; 
       // keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($sql)) {

// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['title'];
echo "</td><td>";
echo $row['num_of_art'];
echo "</td></tr>";
}
echo "</table>";
      }
       ?> </b>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users