1

UPDATE: the warning i get is '' Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in load_price.php on line 15 ''

I am making an e-commerce site, and I am trying to have to dropdown menus, one for selecting colour (works) and one for price (does not work)

After reading the same step for sorting the prices it does not work

The menus are linked to an external php script file, it must be something simple External file:

<?php include "db/connect.php";?>
<?php
$output = '';
if(isset($_POST["price"]))
{
     if($_POST["price_id"] != '')
     {
          $sql = "SELECT * FROM 'clothing' ORDER BY 'price' '".$_POST["price_id"]."'";
     }
     else
     {
          $sql = "SELECT * FROM clothing";
     }
     $result = mysqli_query($conn, $sql);
     while($row = mysqli_fetch_array($result))
     {
          $output .= "


          <div class=items>
          <a href=viewitem.php?itemid=".$row['itemid'].">
          <div class='effect'>
          <img class='image' src=clothing/" . $row["firstimage"]. " class=img-responsive style=width:100% alt=Image>
          <img class='image hover' src=clothing/" . $row["secimage"]. " class=img-responsive style=width:100% alt=Image>
          </div>
          <div class=item-names>" . $row["productname"]. "</div>
          <div class=item-prices>$" . $row["price"]. "</div>
          </div>
          ";
     }
     echo $output;
}
?>

HTML file
<script>
  $(document).ready(function(){
       $('#price').change(function(){
            var price_id = $(this).val();
            $.ajax({
                 url:"load_price.php",
                 method:"POST",
                 data:{price_id},
                 success:function(data){
                      $('#show_product').html(data);
                 }
            });
       });
  });
  </script>
<?php

function fill_price($conn)
{
     $output = '';
     $sql = "SELECT * FROM price";
     $result = mysqli_query($conn, $sql);
     while($row = mysqli_fetch_array($result))
     {
          $output .= '<option value="'.$row["price_id"].'">'.$row["price"].'</option>';
     }
     return $output;
}
 //load_data_select.php
 function fill_color($conn)
 {
      $output = '';
      $sql = "SELECT * FROM color";
      $result = mysqli_query($conn, $sql);
      while($row = mysqli_fetch_array($result))
      {
           $output .= '<option value="'.$row["color_id"].'">'.$row["color"].'</option>';
      }
      return $output;
 }
 function fill_product($conn)
 {

      $output = '';
      $sql = "SELECT * FROM clothing";
      $result = mysqli_query($conn, $sql);
      while($row = mysqli_fetch_array($result))
      {

        $output .= "

        ";
   }
      return $output;
 }
 ?>
    
           <select name="price" id="price">
                <option value="">Show All</option>
                <?php echo fill_price($conn); ?>
           </select>
                     <select name="color" id="color">
                          <option value="">Show All Colours</option>
                          <?php echo fill_color($conn); ?>
                     </select>
                     <br /><br />
                     <div id="show_product">
                          <?php echo fill_product($conn);?>
                     </div>


</div>
Asim
  • 31
  • 4
  • I guess you missed the `[ASC | DESC]` in your query – siva Dec 11 '17 at 12:00
  • @siva the price_id have the values from the data abse = screenshot of it - https://gyazo.com/453aca2abaea3c7fb3bc7dfd5631ac9a – Asim Dec 11 '17 at 12:02
  • are you sure not having your classes in single quotes works, like `item-prices` – Ylama Dec 11 '17 at 12:07
  • Your not sending key-value pair from your ajax request. `if(isset($_POST["price"])) {` it won't go inside of this. because there are no such keys like (price, price_id) in the request. you have to change `data:{price_id},` to `data:{'price': 'xxxx', 'price_id': price_id},` – siva Dec 11 '17 at 12:10
  • @siva thanks for your fast reply, I changed the ajax request and got this warning " Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in load_price.php on line 15 " which is the fetch array... is there anything in the code else needs to be chnaged or even a easier way of doing this?( show all works its just the the two ASC DESC... – Asim Dec 11 '17 at 12:22
  • Try to print the `$result` before going to the while loop – siva Dec 11 '17 at 12:25
  • That query is failing and returning false. [Refer](https://stackoverflow.com/questions/15439919/mysqli-fetch-array-expects-parameter-1-to-be-mysqli-result-boolean-given-in). Please upvote if i solved the issue – siva Dec 11 '17 at 12:34
  • @Ylama Thank you so much I didn't even see your comment until now I knew it would be so simple I'm so silly, Thanks again! – Asim Dec 11 '17 at 17:53
  • @Asim no problem ill just post it as an answer, thought everything looks oky excluding those 2 `classes` – Ylama Dec 12 '17 at 06:48
  • @Asim thanks for the accept can you maybe also upvote if correct fix, or is there something else problem i could help you with? – Ylama Dec 12 '17 at 08:32

1 Answers1

0

Syntax error.

So just add the classes in single quotes.

<?php
$output = '';
while($row = mysqli_fetch_array($result))
  {
      $output .= "
      <div class='item-names'>" . $row["productname"]. "</div>
      <div class='item-prices'>$" . $row["price"]. "</div>
       ";
 }
 echo $output;
?>

Image tag (example)

<td>
 <img class='product-image' src='http://localhost/product_loader/uploads/". $row["product_image_path"]. "'>
</td>

Fixing your image tag:

<img class='image img-responsive' src='clothing/". $row["firstimage"]. "'  style='width:100%;' alt='Image'>
Ylama
  • 2,449
  • 2
  • 25
  • 50