Click here to Skip to main content
15,909,466 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I know im wrong somewhere, and i can't see it..im too tired... gotta finish this school project by tomorow ..pls help

PHP
<?php

if(!isset($_SESSION['card'])){
		$_SESSION['card'] = array();
}
if(isset($_POST['mid'])&&isset($_POST['quantity'])){ 
	if(isset($_SESSION['card'][$_POST['mid']])){
		$_SESSION['card'][$_POST['mid']]+=$_POST['quantity'];
	} else {
		$_SESSION['card'][$_POST['mid']]=$_POST['quantity'];
	} 
	if($_SESSION['card'][$_POST['mid']]<=0){
		unset($_SESSION['card'][$_POST['mid']]);
	}
}
if(empty($_SESSION['card'])){
	echo "card is empty";
	return;
}
$sneakers = array_keys($_SESSION['card']);
$sn_string = implode(",",$sneakers);
 
$q = mysqli_query($conn,"select * from items where id in ({$sn_string})");
while($rw=mysqli_fetch_object($q)){ 
?>

<div> 
 <table id="cart" class="table table-hover table-condensed">
    				<thead>
						<tr>
							<th style="width:50%">Product</th>
							<th style="width:10%">Price</th>
							<th style="width:8%">Quantity</th>
							<th style="width:22%" class="text-center">Subtotal</th>
							<th style="width:10%"></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td data-th="Product">
								<div class="row">
									<img src="../img_items/<?php echo $rw->img; ?>" class="img-responsive" height="99" width="99"> 
										<h4 class="nomargin"><?php echo $rw->name; ?></h4>
										<p><?php echo $rw->description; ?> </p>
									</div>
								</div>
							</td>
							<td data-th="Price"><?php echo $rw->price; ?></td>
							<td data-th="Quantity">
								<input type="text" class="form-control text-center" value=<?php echo $_SESSION['card'][$rw->id]; ?>>
							</td>

							<?php $total=$_SESSION['card'][$rw->id] * $rw->price; ?>
							<td data-th="Subtotal" class="text-center"><?php echo $total; ?></td>

							<td class="actions" data-th="">

							<!-- DELETE -->
								  <a href="shopping_cart.php?action=delete&id=<?php echo $rw->id; ?>">X</a> 							
							</td>
						</tr>
					</tbody>
				 
						 
						<tr>
							<td><a href="../home.php" class="btn btn-warning"><?php echo $total; ?></td>
							<td><a href="?check=1.php" class="btn btn-success btn-block">Checkout ^__i class="fa fa-angle-right"></a></td>
						</tr>
				 
				</table>
 </div>

<?php 
}   
 
} ?>


What I have tried:

<?php 
if(isset($_GET["action"]))  
 {  
      if($_GET["action"] = "delete")  
      {  
           foreach($_SESSION["cart"] as $keys => $values)  
           {  
                if($values["id"] == $_GET["id"])  
                {  
                     unset($_SESSION["cart"][$keys]);  
                     echo '<script>alert("Item Removed")</script>';  
                     echo '<script>window.location="index.php"</script>';  
                }  
           }  
      }  
 }   ?>
Posted
Updated 28-Jul-17 0:51am
Comments
Sfjklm 27-Jul-17 17:30pm    
And also if someone knows how to i remove checkout button after every item , and only put one at the bottom i'll appreciate every help

1 solution

Here's some logic to apply to your homework problem that should fix you up - but it's the logic, helping you to know where to look and what to look for.

(1) Look to where you create your checkout button. It should only happen after all of your items are listed and should not be part of the loop that creates the list of items.

(2) To make an element disappear, you have two basic routes. The hard way, putting the information in session variables and redrawing the page. I'd never do that. The proper way is to use the DOM via javascript. You can then set an on-click(), onchange() event in your button (you use links - which is part of the hard way!). The function can then change the visibility of a button, or its value. The best way, which is a more advance use of the second method is to use AJAX so you can redraw the table part of the screen to reflect changes without having to update the entire page. $_SESSION will work - but I rarely use them. Even that route, combined with using the DOM (....innerHTML='new table contents'), could be used to redraw your table without redrawing the page. The item disappears from the table (it no longer exists in it) - so it need not be hidden: it's gone.

A simplified description of all of this, with examples of events, event handlers, and DOM, can be found here: W3Schools Online Web Tutorials[^]

One last piece of advice: don't wait until the last minute to do homework. By now, it may be too late, even if you get extremely detailed help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900