almystersv Posted January 9, 2008 Share Posted January 9, 2008 Hi Guys, I am creating a mock up of a stationary ordering system and have a side bar on main products page that shows a snippet of the shopping basket and i want it to show just the top three items in the basket. Here is my code that displays the basket on my products page... <?php if (isset ($_SESSION['basket'])) { foreach($_SESSION['basket'] as $key => $product) { ?> <tr> <td width="40%"><?php echo $_SESSION['basket'][$key]['productName']?></td> <td width="20%">£<?php echo $_SESSION['basket'][$key]['price']?></td> <td width="20%" align="center"><?php echo $_SESSION['basket'][$key]['quantity']?></td> <td><a href="removefrombasket.php?URN=<?php echo $product['URN']?>">[-]</a></td> </tr> <?php } ?> </table> <?php } else { ?> No Products Chosen. <?php } ?> the reason for this is that i have limited space and at current everytime something is added it keeps displaying it on this page, understandably, but I would like to know how I can change it to show just 3. Thanks Link to comment https://forums.phpfreaks.com/topic/85241-solved-limiting-the-loop/ Share on other sites More sharing options...
rhodesa Posted January 9, 2008 Share Posted January 9, 2008 add a counter <?php if (isset ($_SESSION['basket'])) { $n = 0; foreach($_SESSION['basket'] as $key => $product) { ?> <tr> <td width="40%"><?php echo $_SESSION['basket'][$key]['productName']?></td> <td width="20%">£<?php echo $_SESSION['basket'][$key]['price']?></td> <td width="20%" align="center"><?php echo $_SESSION['basket'][$key]['quantity']?></td> <td><a href="removefrombasket.php?URN=<?php echo $product['URN']?>">[-]</a></td> </tr> <?php $n++; if($n >= 3) break; } ?> </table> <?php } else { ?> No Products Chosen. <?php } ?> Link to comment https://forums.phpfreaks.com/topic/85241-solved-limiting-the-loop/#findComment-434857 Share on other sites More sharing options...
almystersv Posted January 9, 2008 Author Share Posted January 9, 2008 Superb, Thanks! Any chance of adding a link that pops up when it goes over 3?! So say 4 things are added, it will only show the top 3 but then a little link at the bottom will appear saying more... Which when click will jst link to the basket page. Thanks Link to comment https://forums.phpfreaks.com/topic/85241-solved-limiting-the-loop/#findComment-434858 Share on other sites More sharing options...
rhodesa Posted January 9, 2008 Share Posted January 9, 2008 in the if >= 3 statement, you can add more code before the break (like printing a more... link) as far as sometimes showing 3, and other times showing all, i would setup a $limit variable in the session to help keep track of which version to show. Then change the if to: if($limit && $n >= 3){.... Link to comment https://forums.phpfreaks.com/topic/85241-solved-limiting-the-loop/#findComment-434866 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.