简体   繁体   中英

List products not added to the cart

i need to select from a DB only the items from table that are not yet added to cart, and if they have been added to unset the value from the $row array. this is so far what my problem looks like

$emptyCart=[];
$result = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result))
{
    echo "<div class='product_wrapper'>
    <form method='post' action=''>
      <input type='hidden' name='code' value=".$row['id']." />
      <table class='product'>
        <thead>
          <tr>
            <th class='image' rowspan='3'><img src='img1.jpg' /></th>
            <th class='title'>".$row['title']."</th>
            <th class='buy' rowspan='3'><button type='submit' class='buy' name='add'>Add</button></th>
          </tr>
          <tr>
            <td class='description'>".$row['description']."</td>
          </tr>
          <tr>
            <td class='price'>$".$row['price']."</td>
          </tr>
        </thead>
      </table>
    </form>";
};

You can exclude IDs from query.

Here an example assuming $cart is an array containing IDs. We build a comma separated string list of all those IDs with implode.

If cart is not empty we build a where clause for the query. Now the query won't contain the given IDs.

$cart    = [55, 123, 5564, 3422];
$list    = implode(', ', $cart);
$exclude = $list ? "WHERE `id` NOT IN($list)" : '';

$result  = mysqli_query($con, "SELECT * FROM `products` $exclude");

SELECT * FROM `products` WHERE `id` NOT IN(55, 123, 5564, 3422)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM