agazad Posted October 11, 2007 Share Posted October 11, 2007 Hi, had a quick search but couldn't find the exact solution. I'm new-ish to PHP and have been struggling with this for hours now ??? The function below shows the contents of my basket (i.e. displays the items in the basket, and the quantities (product ids are stored in a session). I need to copy this function, and modify it so that it outputs a list of *all* the products in the session, disregarding quantities (i.e. if you have two of something in the basket, I want it to display the same details twice as opposed to display one with a quantity box). It's a case of stripping some of the functionality from this function - but I'm not clever enough to know what Any help much appreciated. Code is from a brilliant simple PHP shop by the watchmaker project. function showBasket() { global $db; $basket = $_SESSION['basket']; if ($basket) { $items = explode(',',$basket); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="basket.php?action=update" method="post" id="basket">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM products WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="basket.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$title.' - '.$catalogue.'</td>'; $output[] = '<td>£'.$price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update basket</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping basket is empty.</p>'; } return join('',$output); } Quote Link to comment Share on other sites More sharing options...
agazad Posted October 11, 2007 Author Share Posted October 11, 2007 sorry folks..bumping unsolved question Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted October 11, 2007 Share Posted October 11, 2007 Hi try this, i just ran through it quickly, hopefully got it right... function showBasket() { <?php global $db; $basket = $_SESSION['basket']; if ($basket) { $items = explode(',',$basket); $output[] = '<form action="basket.php?action=update" method="post" id="basket">'; $output[] = '<table>'; foreach ($basket as $id) { $sql = 'SELECT * FROM products WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="basket.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$title.' - '.$catalogue.'</td>'; $output[] = '<td>£'.$price.'</td>'; $total += $price; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update basket</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping basket is empty.</p>'; } return join('',$output); } cheers, tdw Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.