2

Am creating a E-Commerce website using PHP. I have product page(product.php), in that page i need to scroll the related items based on categories. I did everything, but related items repeat two times(i mean one item comes two times but i want only once).

My code is following :

    // Class "item active"

<div class="item active">

    <?php
    $select = mysql_query("select * from product where categories ='$categories' limit 4");
    while($rows = mysql_fetch_array($select))
    {
        $p_id = $rows['product_id'];
                          $p_img = $rows['image'];
                          $p_color = $rows['color'];
                          $p_name = $rows['product_name'];
                          $p_desc = $rows['product_detsils'];
                          $categories = $rows['categories'];
                          $p_offer = $rows['offer'];
                          $p_price = $rows['buying_price'];
    ?>
        <div class="col-sm-3">
             <div class="product-image-wrapper">
                   <div class="single-products">
                          <div class="productinfo text-center">
                                             <a href="product.php?product_id=<?php echo $p_id; ?>">
                <img src="images/product/<?php echo $p_img;?>.jpg" alt="" />
                <h2><i class="fa fa-inr"></i> <?php echo $p_price;?></h2>
                 <p><?php echo $p_name;?></p>
                                               </a>
            <button type="button" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</button>
                           </div><!--productinfo text-center end-->
                     </div><!--single-products-->
                                 </div><!--product-image-wrapper end-->
                             </div><!--col-sm-3 end-->
    <?php } ?>

    </div><!--item active end-->




     //Classs "item"

    <div class="item ">

        <?php
        $select = mysql_query("select * from product where categories ='$categories' limit 4");
        while($rows = mysql_fetch_array($select))
        {
            $p_id = $rows['product_id'];
                              $p_img = $rows['image'];
                              $p_color = $rows['color'];
                              $p_name = $rows['product_name'];
                              $p_desc = $rows['product_detsils'];
                              $categories = $rows['categories'];
                              $p_offer = $rows['offer'];
                              $p_price = $rows['buying_price'];
        ?>
            <div class="col-sm-3">
                 <div class="product-image-wrapper">
                       <div class="single-products">
                              <div class="productinfo text-center">
                                                 <a href="product.php?product_id=<?php echo $p_id; ?>">
                    <img src="images/product/<?php echo $p_img;?>.jpg" alt="" />
                    <h2><i class="fa fa-inr"></i> <?php echo $p_price;?></h2>
                     <p><?php echo $p_name;?></p>
                                                   </a>
                <button type="button" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</button>
                               </div><!--productinfo text-center end-->
                         </div><!--single-products-->
                                     </div><!--product-image-wrapper end-->
                                 </div><!--col-sm-3 end-->
        <?php } ?>

        </div><!--item end-->
Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109
Nisha Chinnapa
  • 97
  • 1
  • 2
  • 8
  • 1
    Don't use mysql_* functions! http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Smuuf Aug 26 '15 at 10:27
  • Thanks for replaying, what i want to use ? @Smuuf – Nisha Chinnapa Aug 26 '15 at 10:30
  • 1
    mysqli_* or PDO. Because in nearly years mysql_* won't work anymore.. – Hearner Aug 26 '15 at 10:31
  • Are you sure products are not doubled in database? What is the HTML output from that piece of code? –  Aug 26 '15 at 10:33
  • 1
    Also - escape strings coming from external sources (eg. database) properly. Use `` instead of ``. You can't possibly always know what product names will be put it and where will they come from. https://www.owasp.org/index.php/HTML_Injection – Smuuf Aug 26 '15 at 10:34
  • i updated only once@caCtus – Nisha Chinnapa Aug 26 '15 at 10:38
  • I hadn't seen the whole code. You are querying the database twice with the same query and displaying items twice. Remove one of these, or describe what you are trying to do and why you are doing it twice. –  Aug 26 '15 at 10:41
  • i did class **item activate** but i want only **item**. Just a look my code @caCtus – Nisha Chinnapa Aug 26 '15 at 10:42
  • 1
    Remove `item activate` part if you don't want it. Or be more precise about why you can't remove it. –  Aug 26 '15 at 10:43

1 Answers1

3

You are executing same query twice that's why you product listed twice use only one like:

<div class="item active">

    <?php
    $select = mysql_query("select * from product where categories ='$categories' limit 4");
    while($rows = mysql_fetch_array($select))
    {
        $p_id = $rows['product_id'];
                          $p_img = $rows['image'];
                          $p_color = $rows['color'];
                          $p_name = $rows['product_name'];
                          $p_desc = $rows['product_detsils'];
                          $categories = $rows['categories'];
                          $p_offer = $rows['offer'];
                          $p_price = $rows['buying_price'];
    ?>
        <div class="col-sm-3">
             <div class="product-image-wrapper">
                   <div class="single-products">
                          <div class="productinfo text-center">
                                             <a href="product.php?product_id=<?php echo $p_id; ?>">
                <img src="images/product/<?php echo $p_img;?>.jpg" alt="" />
                <h2><i class="fa fa-inr"></i> <?php echo $p_price;?></h2>
                 <p><?php echo $p_name;?></p>
                                               </a>
            <button type="button" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</button>
                           </div><!--productinfo text-center end-->
                     </div><!--single-products-->
                                 </div><!--product-image-wrapper end-->
                             </div><!--col-sm-3 end-->
    <?php } ?>

    </div><!--item active end-->

and as look like you trying to wind in two div with two different class then first you need to identified what point you need to segregate.

Yogendra
  • 2,139
  • 3
  • 14
  • 27