Reference key in foreach loop

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>

Open in new window


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] => ) )

Open in new window

Robert FrancisDirector of Continuous ImprovementAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
If I understand the question correctly... You can change your foreach iterator to get the key.  Example:

foreach ($_SESSION['cart'] as $key => $item)

Then you can unset($_SESSION['cart'][$key]);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peos JohnPHPCommented:
You can also access the array value by adding increment varriable

<?php
	  $i=0;
	foreach ($_SESSION['cart'] as $item) {
 		echo '<pre>'; print_r($_SESSION['cart'][$i]); echo '</pre>';//ACCESS VALUES USING $i
	  ?>
   		
    <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 $i++; } //End of while ?>
	  </table>

Open in new window

0
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
Apparently I am not coding it correctly. It works but always deletes the last key, not the one from the table row.

<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 $key => $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><form action="">
          	<button type="submit" class="btn btn-default" name="btnSubmit2">Delete Line</button>
          </form></td>
    </tr>
    <?php ;} //End of while ?>
	  </table>
	  <div class="row">
	  	<div class="col-sm-6 text-center">	  
	  	<form action="shop-2.php">
	  	<button type="submit" class="btn btn-default" name="btnSubmit" value="save">Continue Quoting</button>
	  </form>
	  </div>
	  	<div class="col-sm-6 text-center">	  
	  	<form action="qoute_cart_close.php">
	  	<button type="submit" class="btn btn-default" name="btnSubmit" value="save">Submit Quote</button>
	  </form>
	  </div>
	  </div>
<hr>
	</div>
	<div id="footer"></div>
</body>

<?php if(isset($_GET['btnSubmit2'])) {
unset($_SESSION['cart'][$key]);
};?>
</html>

Open in new window

0
Julian HansenCommented:
You are not sending the $key value anywhere - you need to put it in your form so that when the button is clicked you can send the value back to the server
Something like this (this is not how I would do it but this is how to do it given the code you have submitted)
foreach ($_SESSION['cart'] as $key => $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><form action="">
			<input type="hidden" name="id" value="<?php echo $key;?>"/>
          	<button type="submit" class="btn btn-default" name="btnSubmit2">Delete Line</button>
          </form></td>
    </tr>
    <?php ;} //End of while ?>

Open in new window

Then you would need this at the top of your code
<?php 
// REMEMBER TO DO A
// session_start();
// BEFORE THIS CODE
if(isset($_GET['btnSubmit2'])) {
  $key = isset($_GET['id']) ? $_GET['id'] : false;
  if ($key) {
       unset($_SESSION['cart'][$key]);
  }
// WHEN YOU GET HERE ITEM HAS BEEN DELETED SO CONTINUE WITH YOUR RENDER
};?>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.