Help with my shopping cart

Hi Everybody,

I am PHP newbie and i am working on a shopping cart. I am trying to work on it myself in order to grasp very well the whole concept and expand it to the way i want. For days i have been trying to sort it out, but God i am hitting a wall, i am posting the whole code to you, so that you can see the logic i am trying to create and help me better.

These are the erros i am getting : Warning: Illegal string offset ‘code’ in C on line 54 and this one W

arning: Illegal string offset ‘qty’ in C on line 56.

Here are my code:

index.php

[php]<?php
session_start();

include_once(‘includes/connect.php’);

try {

$sql =“SELECT * FROM products LIMIT 3”;

$result = $pdo->query($sql);

$items = $result->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {

echo “Problem with code”.$e->getMessage();
}

if (!isset($_SESSION[‘cart’]) || count($_SESSION[‘cart’]) < 1) {

$_SESSION['cart'] = array();

}

if (isset($_POST[‘action’]) && $_POST[‘action’]==‘Add to cart’) {

$item_name[‘code’] = $_POST[‘product_code’];
$item_name[‘qty’]= $_POST[‘prod_qty’];

$_SESSION[‘cart’][] = $item_name;

header(‘Location: .’);
exit();

}

if (isset($_GET[‘cart’])) {

$cart = array();

foreach ($_SESSION[‘cart’] as $prodcode) {

$cart[‘code’] = $prodcode[‘code’];
$cart[‘qty’] = $prodcode[‘qty’];

}

include_once(‘cart.html.php’);
exit();
}

include_once(‘catalog.html.php’);

?>

[/php]

cart.html.php

[php]

View cart
<h4>Your shopping cart</h4>
<?php if (count($cart)>0):?>
<th>ProductCode</th>

<th>Quantity</th> 

    </tr>
 </thead>
<tr>

<td>Total:</td>

<td></td>   

   </tr>

</tfoot>
<?php foreach($cart as $Item):?>
  </tr>
<?php endforeach;?>
 </table>
<?php else:?>

Your cart is empty !

<?php endif;?>

Continue shopping or

<input type="submit" name="action" value="Empty cart">
</body>
[/php]
<?php echo $Item['code'];?> <?php echo $Item['qty'];?>

I’m not a php guru - but try this - I’m sure someone else here will correct me//

Replace

[php]$cart[‘code’] = $prodcode[‘code’];
$cart[‘qty’] = $prodcode[‘qty’];
[/php]

with
[php]$cart = array(“code” => $prodcode[‘code’], “qty” => $prodcode[‘qty’]);[/php]

Topcoder’s version matches the style I would use. Being a beginner, it is the most simplistic version to get all the data you are looking for.

The more advanced version would be to have a product class and then have the cart array hold the product objects.

Example:
[php]<?php
class Product
{
public $qty;
public $prodcode;
public $price;
private $description;

public function __construct( $qty, $prodcode, $description, $price ){
if ( !filter_var( $qty, FILTER_VALIDATE_INT))
throw new Exception(‘qty expects an integer’);
if ( count( $qty ) < 1 )
throw new Exception( ‘qty cannot be 0’ );
$this->qty = $qty;
$this->prodcode = $prodcode;
$this->description = $description;
$this->price = $price;
}

public function __toString(){
$print =<<<PRT
{$this->prodcode}\t{$this->qty}\n\r
{$this->description}\n\r
${$this->price}
PRT;
return $print;
}

}

$cart = array();

$cd = new Product( 1, 101, ‘Pink Floyd, The Wall’, 15.00);
$book = new Product( 1, 205, ‘Oliver Twist First Edition’, 50.00);
array_push( $cart, $cd);
array_push( $cart, $book);

echo $cart[0];

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service