0

I'm trying to create a categories list, where user will be able to choose item with certain category, but as soon as I'm opening any of the categories, where "soon = '2'", there is only one item coming up. Everything else works.

The code bellow is for categories, where user can chose which caegory s/he want to see.

//navbar.php
<!-- Categories -->
              <div class="dropdown-item menu_cat" type="button" id="catMenuButton"><i class="fas fa-caret-left"></i> Categories</div>
              <form method="POST" class="dropdown-menu dropleft categories_menu row" id="cat_dropdown">
                <button class="dropdown-item" name="candle"> Candles</button>
                <button class="dropdown-item" name="cloth"> Clothes</button>
                <button type="submit"class="dropdown-item" name="tech"> Tech</button>
                </form>

Than here is everything else, that is suppose to exectute commands that will show products from category that user choose. The issue comes upp where 'if($row["soon"] == "2")' is.

//products.php

<?php
    if(isset($_POST['candle'])){
      $sql = "SELECT * FROM products WHERE category = 'candle'";
    }else if(isset($_POST['cloth'])){
      $sql = "SELECT * FROM products WHERE category = 'cloth'";
    }else if(isset($_POST['tech'])){
      $sql = "SELECT * FROM products WHERE category = 'tech'";
    }else{
      $sql = "SELECT * FROM products";
    }
  $result = $conn->query($sql);
  if (!empty($result) && $result->num_rows > 0) {
      while ($row = $result->fetch_assoc()){
        if($row["soon"] == "2"){
        ?>
      <div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
        <div class="card-header">
          <h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
        </div>
        <div class="card-body">
          <h2 class="card-title pricing-card-title"> </h2>
          <img class="pic" src="productsImages\<?php echo $row["productID"]; ?>.jpeg" height="150px" width="120px"></img>
          <br>
          <p>
            <?php echo $row["productPrice"]; ?> SEK
          </p>
          <a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
            <div class="row">
              <button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
            </div>
          </a>
        </div>
      </div>
      
      <?php }else if($row['soon'] == "1" && isset($_POST['tech']) || isset($_POST['candle'])){?>
      <div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
        <div class="card-header">
          <h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
        </div>
        <div class="card-body">
          <h2 class="card-title pricing-card-title"> </h2>
          <img class="pic_soon" src="productsImages/comingSoon.png" height="150px" width="120px"></img>
          <style>
          </style>
          <br>
          <p>
            <?php echo $row["productPrice"]; ?> SEK
          </p>
          <a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
            <div class="row">
              <button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
            </div>
          </a>
        </div>
      </div>

  <?php
  
    }
}
}
else if(empty($result) || $result->num_rows == 0){
    echo "<h2 class='col-12'> Sorry! </h2>";
    echo "<h3 class='col-12'> There is not any product in this category yet! </h3>";
  }
  $conn->close();
  ?>
IMSoP
  • 89,526
  • 13
  • 117
  • 169
DaRt
  • 1
  • 1
  • 3
  • Change the logic and remove concurrent `$row = $result->fetch_assoc();` statement. – ino Feb 07 '22 at 11:49
  • `$row = $result->fetch_assoc();` is fetching the first row from the query, but you then don't do anything with it except check one of the values. You never show that row data anywhere. That fetch seems to be redundant. And the check for `if($row['soon'] == "1"...` would probably make more sense inside the while loop. – ADyson Feb 07 '22 at 11:51
  • @ADyson The thing is that I have multiple items, that have 'soon' set to "1", and if I would put it in while it would write same thing, for each item. Maybe there is a way to do it only one time – DaRt Feb 07 '22 at 11:56
  • @ino seems that it was unnecessary as you said, but still didn't fixed my issue :( – DaRt Feb 07 '22 at 11:59
  • Update your code here as well. – ino Feb 07 '22 at 12:14
  • `it would write same thing, for each item`...well surely you want that don't you? Because some items are available soon, and some are not. So you want to list them all. Maybe you should still put the product name in that message, then it's easy to tell which ones are available and which ones are coming soon. If you have 5 products all available soon, it makes no sense to show that message one for one of them – ADyson Feb 07 '22 at 12:33
  • 1
    @ADyson yeah, that's true, I didn't really though about it that way. But it surely makes more sense than how I wanted to do it in begining. – DaRt Feb 07 '22 at 13:19

2 Answers2

1

In the original post was duplicate $row = $result->fetch_assoc(); statement.

In updated post there is first condition out of the while loop.

Solution bellow is wrapping all the if($row['soon'] into the while loop:

<div class="card-deck col-lg-12 col-md-12 col-sm-12 justify-content-center text-center">
  <?php
    if(isset($_POST['candle'])){
      $sql = "SELECT * FROM products WHERE category = 'candle'";
    }else if(isset($_POST['cloth'])){
      $sql = "SELECT * FROM products WHERE category = 'cloth'";
    }else if(isset($_POST['tech'])){
      $sql = "SELECT * FROM products WHERE category = 'tech'";
    }else{
      $sql = "SELECT * FROM products";
    }
  $result = $conn->query($sql);
  if (!empty($result) && $result->num_rows > 0) {
      while ($row = $result->fetch_assoc()){
        if($row['soon'] == "1" && isset($_POST['tech']) || isset($_POST['candle'])){
          echo "<h2 class='col-12'> Sorry! </h2>";
          echo "<h3 class='col-12'> This product will be soon avaiable </h3>";
        }
        if($row["soon"] == "2"){
        ?>
      <div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
        <div class="card-header">
          <h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
        </div>
        <div class="card-body">
          <h2 class="card-title pricing-card-title"> </h2>
          <img class="pic" src="productsImages\<?php echo $row["productID"]; ?>.jpeg" height="150px" width="120px"></img>
          <br>
          <p>
            <?php echo $row["productPrice"]; ?> SEK
          </p>
          <a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
            <div class="row">
              <button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
            </div>
          </a>
        </div>
      </div>
  <?php
        }
  }
}else if($result->num_rows == 0){
    echo "<h2 class='col-12'> Sorry! </h2>";
    echo "<h3 class='col-12'> There is not any product in this category yet! </h3>";
  }
  $conn->close();
  ?>
</div>

  }
}else if($result->num_rows == 0){
    echo "<h2 class='col-12'> Sorry! </h2>";
    echo "<h3 class='col-12'> There is not any product in this category     yet! </h3>";
  }
  $conn->close();
  ?>
</div>

Recommendation: Always debug your scripts with enabled PHP Error Reporting!

ino
  • 2,345
  • 1
  • 15
  • 27
0

products.php

<?php
    if(isset($_POST['candle'])){
      $sql = "SELECT * FROM products WHERE category = 'candle'";
    }else if(isset($_POST['cloth'])){
      $sql = "SELECT * FROM products WHERE category = 'cloth'";
    }else if(isset($_POST['tech'])){
      $sql = "SELECT * FROM products WHERE category = 'tech'";
    }else{
      $sql = "SELECT * FROM products";
    }
  $result = $conn->query($sql);
  if (!empty($result) && $result->num_rows > 0) {
      while ($row = $result->fetch_assoc()){
        if($row["soon"] == "2"){
        ?>
      <div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
        <div class="card-header">
          <h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
        </div>
        <div class="card-body">
          <h2 class="card-title pricing-card-title"> </h2>
          <img class="pic" src="productsImages\<?php echo $row["productID"]; ?>.jpeg" height="150px" width="120px"></img>
          <br>
          <p>
            <?php echo $row["productPrice"]; ?> SEK
          </p>
          <a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
            <div class="row">
              <button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
            </div>
          </a>
        </div>
      </div>
      
      <?php }else if($row['soon'] == "1" && isset($_POST['tech']) || isset($_POST['candle'])){?>
      <div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
        <div class="card-header">
          <h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
        </div>
        <div class="card-body">
          <h2 class="card-title pricing-card-title"> </h2>
          <img class="pic_soon" src="productsImages/comingSoon.png" height="150px" width="120px"></img>
          <style>
          </style>
          <br>
          <p>
            <?php echo $row["productPrice"]; ?> SEK
          </p>
          <a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
            <div class="row">
              <button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
            </div>
          </a>
        </div>
      </div>

  <?php
  
    }
}
}
else if(empty($result) || $result->num_rows == 0){
    echo "<h2 class='col-12'> Sorry! </h2>";
    echo "<h3 class='col-12'> There is not any product in this category yet! </h3>";
  }
  $conn->close();
  ?>
DaRt
  • 1
  • 1
  • 3