troubleshooting Question

Reference key in foreach loop

Avatar of Robert Francis
Robert Francis asked on
PHP
4 Comments2 Solutions154 ViewsLast Modified:
On line 45 I need to put the code in to unset the item in the table list. How do I reference the key for that item?

		<?php 
		$size = $_GET["size"];
		$color = $_GET["color"];
		$style = $_GET["style"];
		$qty = $_GET["qty"];
		$cat = $_GET["cat"];
		$sub = $_GET["sub"];
		$thumb = $_GET["thumb"];
		
		$cart = array(
		'size' => $size,
		'color' => $color,
		'style' => $style,
		'cat' => $cat,
		'sub' => $sub,
		'thumb' => $thumb,
		'qty' => $qty
		);
		
		$_SESSION['cart'][] = $cart;
		print_r($_SESSION['cart']);
	?> 
<br><br>
<h2 class="required">Quote Cart</h2>
  <table class="table table-bordered table-condensed table-striped table-responsive">
    <tr class="table-header">
          <td><b>Image</b></td>
          <td><b>Style</b></td>
          <td><b>Color</b></td>
          <td><b>Size</b></td>
          <td><b>Quantity</b></td>
          <td><b>Order More</b></td>
          <td><b>Delete</b></td>
    </tr>
    
    <?php
	foreach ($_SESSION['cart'] as $item) {;?>
    <tr>
          <td><img src="/SDL/THUMBNAIL_IMAGE/<?php echo $item['thumb'];?>" alt=""></td>
          <td><?php echo $item['style'];?></td>
          <td><?php echo $item['color'];?></td>
          <td><?php echo $item['size'];?></td>
          <td><?php echo $item['qty'];?></td>
          <td><a href="prod_detail2.php?style=<?php echo $item['style'];?>&sub=<?php echo $item['sub'];?>&cat=<?php echo $item['cat'];?>&btnSubmit=save">Add More</td>
          <td>Delete</td>
    </tr>
    <?php ;} //End of while ?>
	  </table>

Here are the results from the print_r on line 21:

Array ( [0] => Array ( [size] => M [color] => Maroon [style] => ST215 [cat] => Activewear [sub] => Baseball [thumb] => ST215TN.jpg [qty] => 10 ) [1] => Array ( [size] => 2XL [color] => Black [style] => ST215 [cat] => Activewear [sub] => Baseball [thumb] => ST215TN.jpg [qty] => 10 ) [2] => Array ( [size] => S/M [color] => Black [style] => A705 [cat] => Accessories [sub] => Aprons [thumb] => A705TN.jpg [qty] => ) [3] => Array ( [size] => S/M [color] => Black [style] => A705 [cat] => Accessories [sub] => Aprons [thumb] => A705TN.jpg [qty] => ) )
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros