hance2105 Posted June 19, 2013 Share Posted June 19, 2013 hi guys, i am creating a price comparison website and i have my main content which should display 6 random product and their details(price, image, name). i am testing this code below but i am having an error.<?php //Create the connection and select the DB include('db_connect.php'); // Select records from the DB $query = "SELECT * FROM tblproduct ORDER BY Rand() LIMIT 2"; $result = mysql_query($query); // Display records from the table echo "<table border='1'>"; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>"; } echo "</table>"; ?>in fact the product details below should display in below and i dnt know how to select random records from mysql table so that it is displayed randomly in below<div class="center_content"><div class="center_title_bar">Featured Products</div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="#">Product 1</a></div><div class="product_img"><a href="">Image 1</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="#">Product 2</a></div><div class="product_img"><a href="#">Image 2</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="#">Product 3</a></div><div class="product_img"><a href="#">Image 3</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div> <div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="#">Product 4</a></div><div class="product_img"><a href="#">Image 4</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="details.html">Product 5</a></div><div class="product_img"><a href="details.html">Image 5</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="details.html">Product 6</a></div><div class="product_img"><a href="details.html">Image 6</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div> <div class="center_title_bar">Recommended Products</div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="#">Product 7</a></div><div class="product_img"><a href="#">Image 7</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="#">Product 8</a></div><div class="product_img"><a href="#">Image 8</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div><div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"> <div class="product_title"><a href="#">Product 9</a></div><div class="product_img"><a href="#">Image 9</a></div><div class="prod_price"><span class="reduce">%normal%</span> <span class="price">%promo%</span></div> </div><div class="bottom_prod_box"></div> <div class="prod_details_tab"><a href="#" title="header=[Add to fav] body=[ ] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" title="header=[Compare] body=[ ] fade=[on]"><img src="Images/compare-icon.png" alt="" title="" border="0" class="left_bt" /></a><a href="#" class="prod_details">details</a> </div> </div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/279338-displaying-random-records-in-dropboxes/ Share on other sites More sharing options...
hance2105 Posted June 19, 2013 Author Share Posted June 19, 2013 this part works fine. i sorted it out ?php //Create the connection and select the DB include('db_connect.php'); // Select records from the DB $query = "SELECT * FROM tblproduct ORDER BY Rand() LIMIT 2"; $result = mysql_query($query); // Display records from the table echo "<table border='1'>"; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>"; } echo "</table>"; ?> the records are displayed randomly....NOW how do i display it in my html drop boxes? this is where i am stuck please help me out Quote Link to comment https://forums.phpfreaks.com/topic/279338-displaying-random-records-in-dropboxes/#findComment-1436786 Share on other sites More sharing options...
ginerjm Posted June 19, 2013 Share Posted June 19, 2013 What drop down boxes? I didn't see any in your long sample of code. Simply loop thru your results and build the <option> statements using an element of your $row instead of building table rows. BTW - why so maaaannny <div>? Bad practice. Also - you'll be happier in the long run if you use associative arrays when retrieving your results. Using 'num' can lead to trouble down the road if you change your table structure. Unless you have an awful lot of fields in your table, using nums doesn't buy you a thing. Quote Link to comment https://forums.phpfreaks.com/topic/279338-displaying-random-records-in-dropboxes/#findComment-1436818 Share on other sites More sharing options...
hance2105 Posted June 19, 2013 Author Share Posted June 19, 2013 hmmm ok let me explaini am creating a price comparison website. i have a products table where product details are stored. in my index page, i want to display 9 products in a 3x3 table. the products are to be selected randomly from the database table and displayed.the first code i tested to see if i can display products randomly. it works except that the product image is not displayed.the second code is the one i developed for my index page where the products are to be displayed.hope this clears what i want to do Quote Link to comment https://forums.phpfreaks.com/topic/279338-displaying-random-records-in-dropboxes/#findComment-1436856 Share on other sites More sharing options...
ginerjm Posted June 19, 2013 Share Posted June 19, 2013 The background may come in handy later on. For now - where are the dropdown boxes you referred to? And if you wish to build them , my prev post tells you what to change. And again - why all the divs? That is not how you build a web page. Quote Link to comment https://forums.phpfreaks.com/topic/279338-displaying-random-records-in-dropboxes/#findComment-1436877 Share on other sites More sharing options...
Solution hance2105 Posted June 20, 2013 Author Solution Share Posted June 20, 2013 think i got it...thnx Quote Link to comment https://forums.phpfreaks.com/topic/279338-displaying-random-records-in-dropboxes/#findComment-1437006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.