JackW Posted April 25, 2007 Share Posted April 25, 2007 I am stumped. I want my site to display the information from the data base in a truly random order. I know how to write it to display in ASC (ascending) or DESC (descending) order but I want it to come up in a different order each time it displays. Is that possible? I have 30 rows of sponsors in the data base and want to display them 15 at a time but not in any specific order.It sounds simple but I can’t seem to find anything about it. Link to comment Share on other sites More sharing options...
pulpfiction Posted April 25, 2007 Share Posted April 25, 2007 If you are using SQL Server, then trySELECT col_name1, col_name2 FROM Tbl_nameORDER BY NEWID() Link to comment Share on other sites More sharing options...
JackW Posted April 25, 2007 Author Share Posted April 25, 2007 Thanks,Not sure I understand what you said. Here is what I have and it works great, but not random.Can I replace DESC with something that will make it random? I tried removing order by and it works but comes up in the same order most of the time. @ $db = mysql_pconnect('myserver','mydatabase','mypassword');mysql_select_db('mydatabase') or die( "Unable to select database");$query= 'SELECT * FROM `sponsors` order by `ID` DESC LIMIT 0, 15 ';$query_results=mysql_query($query);$match_results=mysql_num_rows($query_results);for ($i=0; $i < $match_results; $i++){$row=mysql_fetch_array($query_results);echo '<a href="';echo($row ['url'] ), '"', '>';echo($row ['name'] ), '</a><br>';echo($row ['comment'] ), '<br><br>';}mysql_close($db); Link to comment Share on other sites More sharing options...
jesh Posted April 25, 2007 Share Posted April 25, 2007 I found this link, it may help. pulpfiction's method is listed on that page as well:http://www.petefreitag.com/item/466.cfm Link to comment Share on other sites More sharing options...
JackW Posted April 25, 2007 Author Share Posted April 25, 2007 Thank you both. That did it. The new code is below and works like a charm. @ $db = mysql_pconnect('myserver','mydatabase','mypassword');mysql_select_db('mydatabase') or die( "Unable to select database");$query= 'SELECT * FROM `sponsors` order by RAND() LIMIT 0, 15 ';$query_results=mysql_query($query);$match_results=mysql_num_rows($query_results);for ($i=0; $i < $match_results; $i++){$row=mysql_fetch_array($query_results);echo '<a href="';echo($row ['url'] ), '"', '>';echo($row ['name'] ), '</a><br>';echo($row ['comment'] ), '<br><br>';}mysql_close($db); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.