Jump to content

Listing Problem


jblack

Recommended Posts

Alright the problem is that when I use the following script, it lists everything I'd like to be listed. It just does it in an unorderly manner.An example of this would be like the following figure:[] [] [] [] [] [] [] [] [] [][] [] [] [] [] [][] [] [] [] [] [] [] [] [][] [] [][] = Listing optionHere's the script:

<?php$dbhost = "host";$dbuser = "dbuser";$dbpass = "password";mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to database.");mysql_select_db($dbuser);?><html><head><title>Useless Games</title><style>body {margin: 0;background-color: black;color: #973232;}td {border: 1px #973232 solid;}.header {background-image: url(images/ugbackground.gif);}.game {width: 125px;height: 100px;float: left;margin: 20px;text-align: center;}</style></head><body link="#973232" alink="#973232" vlink="#973232"><table align="center" cellpadding="0" cellspacing="0" width="100%"><tr><td class="header" colspan="2" align="center" height="200"><img src="images/ug.gif"></td></tr></table><table width="15%" align="left"><tr><td valign="top"><center><h2 style="margin: 0;">Links</h2></center></td></tr><tr><td><a href="index.html">Home</a><br /><a href="category.php">Games</a></td></tr></table><table width="85%" align="right"><td><?php?><h2>Games</h2><br><?php$i = 1;while($i < 32) { // Run Query $q = "SELECT * FROM games WHERE id = $i"; $rs = mysql_query($q) or die("Could not perform query. " . mysql_error()); $rw = mysql_fetch_row($rs); // Write Games List $write = "<div class='game'><a href='games.php?game=$i'><img src='/games/thumbs/g$i.gif'><br />$rw[1]</a></div>"; echo $write; $i++;}?></td></tr></table></body></html>

Link to comment
Share on other sites

You will want to look into the ORDER BY clause in SQL http://www.w3schools.com/sql/sql_orderby.asp. It allows you to sort the results of the MySQL query based on a column in the table.Also, why are you storing output to $write before echoing it? Why not just echo directly. Just something I found particularly odd. :)EDIT: While you're looking at that, look up LIMIT as well, you can limit the number of results to 32 in the query as well. No need to count with $i.

Link to comment
Share on other sites

Then add an "ORDER BY `name` ASC|DESC" (decide whether you want ascending or descending order and include only one of ASC and DESC) to the query. You did read the link I gave you didn't you? It has an example exactly like your problem.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...