flyersun Posted January 23, 2007 Share Posted January 23, 2007 As part of my assignment I have to explain what all the code I've used does... thats all well and good but I have this code which my tutor wrote and I'm a bit at at loss to work how it works.This is part of a shoping cart script I know what it does but I'm not to sure how.. can anyone help please?function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM cds WHERE cdID = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$title.' by '.$artist.'</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 cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); Link to comment https://forums.phpfreaks.com/topic/35300-explain-help-please/ Share on other sites More sharing options...
trq Posted January 23, 2007 Share Posted January 23, 2007 Um.... the whole point of the assignment is to see how much [b]you've[/b] learnt, not how much we know. Link to comment https://forums.phpfreaks.com/topic/35300-explain-help-please/#findComment-166850 Share on other sites More sharing options...
flyersun Posted January 23, 2007 Author Share Posted January 23, 2007 ok you have a point.. how about I tell you what I think it means and you tell me if I'm close or not? Link to comment https://forums.phpfreaks.com/topic/35300-explain-help-please/#findComment-166851 Share on other sites More sharing options...
trq Posted January 23, 2007 Share Posted January 23, 2007 Sounds fair enough. Go through the code bit by bit and add comments just prior to what you think the code is doing. Link to comment https://forums.phpfreaks.com/topic/35300-explain-help-please/#findComment-166853 Share on other sites More sharing options...
flyersun Posted January 23, 2007 Author Share Posted January 23, 2007 function showCart() { //connection to database global $db; // Get info from the session var and stuff it in $cart $cart = $_SESSION['cart']; //check if there is anything in $cart if ($cart) { //If there is something in $cart put it in an array called $items (brake part where there are ,'s) $items = explode(',',$cart); //create an array called $contents $contents = array(); //Loop, go though the $items array untill there are no more items left foreach ($items as $item) { // This is where I start to get confused it's checking if there is anything in $contents[$item] and //then putting what ever's there into putting it back in it's self? I don't have a clue what the question mark does. // but then it seems to be adding 1 to the value and not sure what the last bit does.. hmm $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } Link to comment https://forums.phpfreaks.com/topic/35300-explain-help-please/#findComment-166861 Share on other sites More sharing options...
trq Posted January 23, 2007 Share Posted January 23, 2007 Ok... that last part actually loops through the array of items, creating a new array of unique keys. For each key it adds up the total and stores it as the value.Hope that helps... it is actually a little difficult to explain. Link to comment https://forums.phpfreaks.com/topic/35300-explain-help-please/#findComment-166872 Share on other sites More sharing options...
flyersun Posted January 23, 2007 Author Share Posted January 23, 2007 Thanks! that make it alot clearer!Amy Link to comment https://forums.phpfreaks.com/topic/35300-explain-help-please/#findComment-167049 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.