Page 1 of 1

selecting and displaying records randomly from mysql + php

Posted: Wed Jun 19, 2013 9:41 am
by hance
hi guys, am new out here and a complete newbie to php. hope i will get some help to complete my website.

below is a working code i wrote to select random records from my tbl_products in mysql database. it works fine.

Code: Select all

<?php  

//Create the connection and select the DB  
include('db_connect.php');  

// Select records from the DB  
$query = "SELECT * FROM tbl_product ORDER BY Rand() LIMIT 7";  
$result = mysql_query($query);  

// Display records from the table  
echo "<table border=\"5\" cellpadding=\"5\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#808080\" width=\"100%\" id=\"AutoNumber2\" bgcolor=\"#C0C0C0\"><tr>";  
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {  
echo "<tr><td>$row[1]</td><td>$row[2]</td><td>$row[8]</td></tr>";  
}  
echo "</table>"; 

?>
here is the html i created so that records are randomly displayed. it will display the product name, product image, price of product

[text]<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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Compare] body=[&nbsp;] 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>[/text]

how do i combine both so that the products can be displayed randomly in my html codes above?

can it be done this way?

Re: selecting and displaying records randomly from mysql + p

Posted: Wed Jun 19, 2013 9:33 pm
by Celauran
Just include your product markup inside your while loop.

Code: Select all

<?php while ($row = mysql_fetch_array($result)): ?>
<div class="prod_box">
	<div class="top_prod_box"></div>
	<div class="center_prod_box">            
		<div class="product_title"><a href="#"><?= $row['title']; ?></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=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
		<a href="#" title="header=[Compare] body=[&nbsp;] 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>
<?php endwhile; ?>
Also, since you're new, take a look at PDO before you pick up bad habits like mysql_ functions.

Re: selecting and displaying records randomly from mysql + p

Posted: Fri Jun 21, 2013 2:21 am
by hance
hello Celauran, thanks a lot...u greatly helped me and its working great except that i dnt know how to make all the images display same size in this line

Code: Select all

<div class="product_img"><a href=""><?= '<img src="Images/Products/'.$row['prod_photo'].'"/>'; ?></a></div>
find below the whole working code.

Code: Select all

<?php 

//Create the connection and select the DB 
include('db_connect.php'); 

// Select records from the DB 
$query = "SELECT * FROM tbl_product ORDER BY Rand() LIMIT 6"; 
$result = mysql_query($query); 

// Display records from the table ?>
 
<?php while ($row = mysql_fetch_array($result)): ?>
   
    	<div class="prod_box">
        	<div class="top_prod_box"></div>
            <div class="center_prod_box">            
                 <div class="product_title"><a href="#"><?= $row['prod_name']; ?></a></div>
                 <div class="product_img"><a href=""><?= '<img src="Images/Products/'.$row['prod_photo'].'"/>'; ?></a></div>
                 <div class="prod_price"><span class="reduce">Rs <?= $row['prod_price']; ?></span> <span class="price">Rs %promo%</span></div>                        
            </div>
            <div class="bottom_prod_box"></div>             
            <div class="prod_details_tab">
            <a href="#" title="header=[Add to fav] body=[&nbsp;] fade=[on]"><img src="Images/add_to_fav.png" alt="" title="" border="0" class="left_bt" /></a>
            <a href="#" title="header=[Compare] body=[&nbsp;] 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>
     		
        <?php endwhile; ?>
will be glad to have another helping hand from you

i thank you a lot for the previous one...

Re: selecting and displaying records randomly from mysql + p

Posted: Sat Jun 22, 2013 7:43 pm
by Celauran
Probably the easiest solution for the images is to use a little CSS to have them display a certain size. Alternately, you could use something like the GD library to resize the images on upload.

Re: selecting and displaying records randomly from mysql + p

Posted: Sun Jun 23, 2013 10:54 am
by hance
thnx for the help my friend i finally did it ;)

help - how i close the thread?