swbdesigns Posted February 28, 2011 Share Posted February 28, 2011 Hello, I have a stock page on my site and want to use the code to display a small stock area on my home page. I am not sure on how I would limit the number of records or randomize the records shown. The code is <?php $coachselect=mysql_query("SELECT * FROM coaches WHERE DISPLAY = '1' ORDER BY STOCK")or die(mysql_error()); while($row = mysql_fetch_array($coachselect)){ ?> <ul class="stock-home"> <li><h4><a href="details-id-<?php echo $row['ID']; ?>.html" target="_blank"></a></h4></li> <li><img src="./<?php echo $row['IMAGES'] . "/" . $row['DEFAULT_IMAGE'] ; ?>" width="187" height="138" alt="lehel virage for sale" class="fl"/></li> <li><?php echo $row['NAME']; ?></h3></li><li><?php echo $row['DESC_SHORT']; ?></li> <li><a href="details.php?id=<?php echo $row['ID']; ?>" target="_blank">more info...</a></li> <li><?php echo $row['DESC_2']; ?></li> </ul><!--close stock home--> <?php } ?> I am guessing that something needs to be added here $coachselect=mysql_query("SELECT * FROM coaches WHERE DISPLAY = '1' ORDER BY STOCK")or die(mysql_error()); while($row = mysql_fetch_array($coachselect)){ ?> But am not sure how to do it? Does anyone have any advice or could point me to a tutorial? Here is the rest of the code, from the top of the page. $CON=mysql_connect($DB_SERVER, $DB_USER, $DB_PASS) or die(mysql_error()); $DB_SELECT=mysql_select_db($DATABASE) or die(mysql_error()); if (isset($_POST)) { foreach ($_POST as &$value) { $value = trim(htmlspecialchars($value, ENT_QUOTES)); } } if (isset($_GET)) { foreach ($_GET as &$value) { $value = trim(htmlspecialchars($value, ENT_QUOTES)); } } $_SERVER['PHP_SELF'] = filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?> Many Thanks Dan Link to comment https://forums.phpfreaks.com/topic/229159-how-to-limit-and-randomize-the-data-displayed/ Share on other sites More sharing options...
AbraCadaver Posted February 28, 2011 Share Posted February 28, 2011 LIMIT and ORDER BY RAND() Link to comment https://forums.phpfreaks.com/topic/229159-how-to-limit-and-randomize-the-data-displayed/#findComment-1180904 Share on other sites More sharing options...
swbdesigns Posted February 28, 2011 Author Share Posted February 28, 2011 Thanks for the reply, doo i add that here? $coachselect=mysql_query("SELECT * FROM coaches WHERE DISPLAY = '1' ORDER BY STOCK LIMIT 0, 8 ORDER BY RAND() ")or die(mysql_error()); cheers Link to comment https://forums.phpfreaks.com/topic/229159-how-to-limit-and-randomize-the-data-displayed/#findComment-1180906 Share on other sites More sharing options...
AbraCadaver Posted February 28, 2011 Share Posted February 28, 2011 FYI, rand() is slower on large datasets: SELECT * FROM coaches WHERE DISPLAY = '1' ORDER BY RAND(), STOCK LIMIT 8 Link to comment https://forums.phpfreaks.com/topic/229159-how-to-limit-and-randomize-the-data-displayed/#findComment-1180907 Share on other sites More sharing options...
swbdesigns Posted February 28, 2011 Author Share Posted February 28, 2011 Cheers Shawn, It should be okay it currently has 30 records and I can assume it will never get to the hundreds! Thanks again Link to comment https://forums.phpfreaks.com/topic/229159-how-to-limit-and-randomize-the-data-displayed/#findComment-1180911 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.