#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    30
    Rep Power
    0

    Help to display random records


    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 Code:
    <?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($resultMYSQL_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

    Code:
    <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>
    Last edited by requinix; June 19th, 2013 at 02:39 AM. Reason: adding code tags
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,959
    Rep Power
    9397
    Please use &amp;#91;code] and &amp;#91;php] tags when posting code.


    Saying that it creates errors doesn't do us one bit of good unless you tell us what errors.
    Meanwhile, your query will grab two random results but the output will look absolutely nothing like the example HTML.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    30
    Rep Power
    0
    this part works fine. i sorted it out

    PHP Code:

    ?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
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,959
    Rep Power
    9397
    Thankfully you mentioned that in your first post otherwise I'd think you had some kind of error somewhere, or you couldn't figure out how to get the output into a certain format.

    The HTML for a dropbox box looks like
    Code:
    <select name="form field name">
        <option value="form field value">label</option>
        <option value="form field value">label</option>
        <option value="form field value">label</option>
        ...
    </select>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    30
    Rep Power
    0
    i have a code like this in my HTML

    <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>

    i think i have 9 in all. how can i display random records from mysql in them. i need to display an image as well.

    can the previous code in the previous thread do it?

IMN logo majestic logo threadwatch logo seochat tools logo