jblack Posted September 23, 2008 Share Posted September 23, 2008 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 More sharing options...
AElliott Posted September 23, 2008 Share Posted September 23, 2008 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 More sharing options...
jblack Posted September 23, 2008 Author Share Posted September 23, 2008 how would I list the images out with the order by though? That's one thing that I need to figure out. Link to comment Share on other sites More sharing options...
AElliott Posted September 23, 2008 Share Posted September 23, 2008 Well... what's the table structure and on what criteria are you ordering them? Link to comment Share on other sites More sharing options...
jblack Posted September 23, 2008 Author Share Posted September 23, 2008 um its likeid | name | codeand i want to order by name Link to comment Share on other sites More sharing options...
AElliott Posted September 23, 2008 Share Posted September 23, 2008 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 More sharing options...
jlhaslip Posted September 23, 2008 Share Posted September 23, 2008 yes, ORDER BY `name` ASC LIMIT 32 (or something like that)and you will only do one query to get all the results into an array, then loop through the array to build the divs. Link to comment Share on other sites More sharing options...
jblack Posted September 23, 2008 Author Share Posted September 23, 2008 Alright..after a great deal of trial and error I've finally achieved my goal.On a side note i figured out why the list was so messed up in formatting, I had the heightfor the <div> tag too low. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.