Skip to content
Advertisement

Search query by size in PHP

I want my site to search for any product that belonged to particular size when clicked any size option in the select input menu on mobile version like shop by size menu in www.brastop.com on mobile version. My problem is on how to extract size variable from option tag and put it in the select query in sizeresult.php. below are my codes:

  1. for select menu

    <option selected disabled>Select Your size:</option>
    <?php

    $get_sizes = "select * from sizes";
    $run_sizes = mysqli_query($dbc,$get_sizes);

    while ($row_sizes=mysqli_fetch_array($run_sizes)){

        $size_id = $row_sizes['size_id'];
        $size_name = $row_sizes['size'];

        echo "

          <option value='sizeresults.php?$size_id'> $size_name </option>


          ";

    }

    ?>
</select>

my problem is how to extract the $size_name from option tag and put it in between the two single quotes after the WHERE CLAUSE on the first line in the next code snipet where I wrote sizes.size = =’%$size_name%’

for sizeresult.php

$run_products = mysqli_query($dbc,"SELECT * FROM products INNER JOIN SIZES USING (size_id) WHERE sizes.size ='%$size_name%'");
while($row_products=mysqli_fetch_array($run_products)){

   $pro_id = $row_products['product_id'];

   $pro_title = $row_products['product_title'];

   $pro_price = $row_products['product_price'];

   $pro_sale_price = $row_products['product_sale'];

   $pro_url = $row_products['product_url'];

   $pro_img1 = $row_products['product_img1'];

   $pro_label = $row_products['product_label'];

   $manufacturer_id = $row_products['manufacturer_id'];

   $get_manufacturer = "select * from manufacturers where manufacturer_id='$manufacturer_id'";

   $run_manufacturer = mysqli_query($dbc,$get_manufacturer);

   $row_manufacturer = mysqli_fetch_array($run_manufacturer);

   $manufacturer_title = $row_manufacturer['manufacturer_title'];

   if($pro_label == "sale"){

       $product_price = " <del> NGN $pro_price </del> ";

       $product_sale_price = "/ NGN $pro_sale_price ";

   }else{

       $product_price = "  NGN $pro_price  ";

       $product_sale_price = "";

   }

   if($pro_label == ""){

   }else{

       $product_label = "

           <a href='#' class='label $pro_label'>

               <div class='theLabel'> $pro_label </div>
               <div class='labelBackground'>  </div>

           </a>

       ";

   }

   echo "

       <div class='col-md-4 col-sm-6 center-responsive'>

           <div class='product'>

               <a href='$pro_url'>

                   <img class='img-responsive' src='admin_area/product_images/$pro_img1'>

               </a>

               <div class='text'>

               <center>

                   <p class='btn btn-primary'> $manufacturer_title </p>

               </center>

                   <h3>

                       <a href='$pro_url'>

                           $pro_title

                       </a>

                   </h3>

                   <p class='price'>

                   $product_price &nbsp;$product_sale_price

                   </p>

                   <p class='button'>

                       <a class='btn btn-default' href='$pro_url'>

                           View Details

                       </a>

                       <a class='btn btn-primary' href='$pro_url'>

                           <i class='fa fa-shopping-cart'></i> Add to Cart

                       </a>

                   </p>

               </div>

               $product_label

           </div>

       </div>

       ";

}


?>

Advertisement

Answer

Here are things you can do to fix your code.

  1. write your form as following.

     <form action="sizeresults.php" method="POST"><!-- use post method not must but safe -->
     <select name="size"  onchange="this.form.submit()"><!-- i assumed the select name to be size -->
         <option selected disabled>Select Your size:</option>
         <?php
             $get_sizes = "select * from sizes";
             $run_sizes = mysqli_query($dbc,$get_sizes);
             while ($row_sizes=mysqli_fetch_array($run_sizes)){
                 $size_id = $row_sizes['size_id'];
                 $size_name = $row_sizes['size'];
                 echo "<option value='$size_name'> $size_name </option>";//changed value
             }
         ?>
     </select>
    
  2. add the following line right above $run_products = mysqli_query(… in sizeresults.php file.

    $size_name=$_POST['size']; //used to get the value of option to be used in query.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement