0

So I wanted to make a specific grid where It echos different 6 items every time the page refreshes!

I used this

                $stmt = $con->prepare("SELECT ID, In_Stock FROM items WHERE In_Stock = 1 ORDER BY ID DESC LIMIT 1");
            $stmt->execute();
            $items = $stmt->fetchAll();
            foreach ($items as $item) {
                $highID = $item['ID'];
            }

To get the highest ID

And then made a $itemid = rand(1, $highID); To Pick a random Item ID

And then used this code to echo the item // Echoing the Items

            $stmtx = $con->prepare("SELECT * FROM items WHERE ID = ? LIMIT 6");
            $stmtx->execute(array($itemid));
            $itemz = $stmtx->fetchAll();
            foreach ($itemz as $item) { ?>
                <div class="col-md-4 col-sm-12">
                    <div class="product text-center mt-5">
                        <div class="product-name pt-5">
                            <?php if($item['In_Stock'] == 1){ echo '<a href="product.php?itemid=' . $item['ID'] . '">' . $item['Name']  . '</a>'; }else{echo $item['Name'];} ?>
                        </div>
                        <div class="product-html">
                            <?php if($item['In_Stock'] == 1){echo "Avilable";}else{echo "Out of Stock";} ?>
                        </div>
                    </div>
                </div>
            <?php }

Put it echod 1 time only! Becuase rand() worked one time. Is there's any better way to do it ? or how can I force rand() to echo value 6 times.

Ahmed Maher
  • 45
  • 1
  • 8

0 Answers0