ballhogjoni Posted July 8, 2007 Share Posted July 8, 2007 what is going on in this line of code? foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } Link to comment https://forums.phpfreaks.com/topic/58978-can-some-one-explain-this-array/ Share on other sites More sharing options...
per1os Posted July 8, 2007 Share Posted July 8, 2007 for every item in array of items create a new index of $item in the $contents array and if it is already set add one, else set it to one. Link to comment https://forums.phpfreaks.com/topic/58978-can-some-one-explain-this-array/#findComment-292677 Share on other sites More sharing options...
ballhogjoni Posted July 8, 2007 Author Share Posted July 8, 2007 Ok, this may be complicated, for me it is over my head. How can you tell php to create a xml structure for the following code? <?php 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 width="500px" style="border:1px solid #EEEEEE;">'; $output[] = '<tr>'; $output[] = '<td>'; $output[] = '<table align="center">'; $output[] = '<tr>'; $output[] = '<td align="center" class="style1">Item</td>'; $output[] = '<td align="center" class="style1">Qty</td>'; $output[] = '<td align="center" class="style1">Price</td>'; $output[] = '<td align="center" class="style1">Sub Total</td>'; $output[] = '</tr>'; 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 class="style4" align="center">'.$title.'</td>'; $output[] = '<td align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td class="style4" align="center">$'.$price.'</td>'; $output[] = '<td class="style4" align="center">$'.($price * $qty).'</td>'; $output[] = '<td align="center"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="images/delete.jpg" alt="delete the diaper cake product" border="0" width="22" height="25"></a></td>'; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<table align="center" width="500px">'; $output[] = '<tr>'; $output[] = '<td>'; $output[] = '</td>'; $output[] = '<td align="right">'; $output[] = '<button type="submit">Update Qty</button>'; $output[] = '</td>'; $output[] = '</tr>'; $output[] = '<tr>'; $output[] = '<td>'; $output[] = '</td>'; $output[] = '<td align="right" style="font-size:large">'; $output[] = 'Total:'; $output[] = '</td>'; $output[] = '<td style="font-size:medium; font-weight:bold; text-align:center;">$'.$total; $output[] = '</td>'; $output[] = '</tr>'; $output[] = '</table>'; $output[] = '</form>'; } else { $output[] = '<p class="style4">You shopping cart is empty.</p>'; } return join('',$output); } ?> Link to comment https://forums.phpfreaks.com/topic/58978-can-some-one-explain-this-array/#findComment-292683 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.