PHP Shopping Cart Foreach

general-dev
scripts

#1

Hello, I'm probably being thick with this but basically I used to have a shopping cart on my old version so now i'm transferring it across to my mini MVC framework but currently the foreach is only showing one result even tho the session holds more than one! I did a bit of debugging with this <?php var_dump($_SESSION['cart']); echo $data['array']; print_r($data['getItems']); ?> which returned array(2) { [0]=> string(1) "3" [1]=> string(1) "2" } 3,2 and then the print_r returned all the data from only one! I'm really confused but feel like i'm missing something that is relatively small but prevents the whole thing from working. Thanks in advance!

Here is my controller code:

    public function cart()
    {
        $getSettings = $this->setting->getAll();
        $getCategories = $this->setting->getCategories();
        $array = implode(',',$_SESSION['cart']);
        $getCartItems = $this->item->getItemsFromCart($array);  
        
        $data = [
            'getSettings' => $getSettings,
            'getCategories' => $getCategories,
            'title' => 'Cart',
            'getItems' => $getCartItems,
            'array' => $array
        ];
        $this->view('cart', $data);
    }
    
    public function addcart()
    {
        if(!$_GET['id']){ redirect(''); }
        //check if product is already in the cart
	    if(!in_array($_GET['id'], $_SESSION['cart'])){
		    array_push($_SESSION['cart'], $_GET['id']);
	    }
    }
    
        public function deletecart()
    {

	    unset($_SESSION['cart']);
    }

Here is my view code:

			<table class="table table-bordered table-striped">
				<thead>
					<th></th>
					<th>Name</th>
					<th>Price</th>
					<th>Quantity</th>
					<th>Subtotal</th>
				</thead>
				<tbody>
					<?php
						//initialize total
						$total = 0;
 						$index = 0;
 						if(!isset($_SESSION['qty_array'])){
 							$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
 						}
 						
 						          if( empty($_SESSION['cart']) ) { ?>

               <div class="alert alert-primary shadow-1" role="alert">
                  <b><i class="fas fa-info-circle"></i> Empty Cart</b>
               </div>
            <?php } else {
 						
						foreach ($data['getItems'] as $row) :
								?>
								<tr>
									<td>
										<a href="delete_item.php?id=<?php echo $row['id']; ?>&index=<?php echo $index; ?>" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></a>
									</td>
									<td><?php echo $row['name']; ?></td>
									<td><?php echo number_format($row['price'], 2); ?></td>
									<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
									<td><input type="text" class="form-control" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>
									<td><?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></td>
									<?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
								</tr>
								<?php
								//$index ++;
							endforeach; 
						}
 
					?>
					<tr>
						<td colspan="4" align="right"><b>Total</b></td>
						<td><b><?php echo number_format($total, 2); ?></b></td>
					</tr>
				</tbody>
			</table>

Here is the model:

	public function getItemsFromCart($array)
	{
        //$array contains 3,2 id
        $bind = [':in' => $array];
        $sql = 'SELECT * FROM msi_items WHERE id IN (:in)';
        $results = $this->db->selectExtended($sql, $bind);
        return $results; 
	}

Image:

BTW: Before I get moaners, I'm not following the strict MVC guidelines, only part of it!


#2

I don't know what your selectExtended() function does, but if it's going to use a prepared statement with $bind as a parameter, I don't think you can use them in that way. I'm not 100% sure, but I think an "IN" list has to be done differently. If it's not a prepared statement, ignore this.


#3

Correct, each item in the list has to be a parameter of its own. You can't have a whole list as a singular parameter.
This is one way of dealing with it:-