Jump to content

Displaying random records in dropboxes


hance2105
Go to solution Solved by hance2105,

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

hmmm ok let me explain
i 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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.