Welcome Guest, Not a member yet? Register   Sign In
Undefined Index: cart
#1

[eluser]clintonbeattie[/eluser]
Hi,

I am creating a shopping cart and having errors thrown when I click an "add to cart" button.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: cart

Filename: models/morders.php

Line Number: 10
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\codeigniter\system\libraries\Exceptions.php:164)

Filename: libraries/Session.php

Line Number: 662
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\codeigniter\system\libraries\Exceptions.php:164)

Filename: helpers/url_helper.php

Line Number: 528


Line Number 10 of the morders model is
Code:
$cart = $_SESSION['cart'];
which is a session. When I refresh the error page, the item is added, and all subsequent items can be added, but if I go and delete the cookie from the computer and try to "add to cart" again the same error occurs.

This is my function...
Code:
function updateCart($productid,$fullproduct){
//pull in existing cart first!
$cart = $_SESSION[‘cart’];
$totalprice = 0;
if (count($fullproduct)){
if (isset($cart[$productid])){
$prevct = $cart[$productid][‘count’];
$prevname = $cart[$productid][‘name’];
$prevname = $cart[$productid][‘price’];

It says "pull in existing cart first!". Maybe this is what is causing it because the cart session hasne't been implemented.

Any idea how to fix this?

Thanks,
Clinton
#2

[eluser]m4rw3r[/eluser]
maybe do a simple check:
Code:
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
#3

[eluser]Rick Jolly[/eluser]
There are invalid single quotes in your updateCart function.
#4

[eluser]clintonbeattie[/eluser]
Hi,

In the first reply, where should I put this code check it and what does it do?
In the second reply, what do you mean invalid single quotes? They aren't the same because I copied from dreamweaver to the web. In the real code they are what should be expected...I think.

Thanks for your help so far.

Clinton
#5

[eluser]clintonbeattie[/eluser]
A quick update and one that will help all people using the Wrox book.

The errors will not be shown if you do the following, although they are still being flagged in the logs folder...

Error handling in codeigniter:
To log your errors, but not display them to screen, you set the error_reporting to “0” (error_reporting(0)Wink in the index.php at the root of the code igniter folder,
not the application folder, and your log threshold at 1 or more in the config/config.php ($config['log_threshold'] = 1Wink

All errors will be visible in system/logs/log.php

These are the errors in log.php...
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

ERROR - 2009-01-13 20:08:05 --> Severity: Notice  --> Undefined property: MOrders::$2.58 C:\xampp\htdocs\codeigniter\system\application\models\morders.php 74
ERROR - 2009-01-13 20:08:09 --> Severity: Notice  --> Undefined property: MOrders::$5.16 C:\xampp\htdocs\codeigniter\system\application\models\morders.php 74
ERROR - 2009-01-13 20:08:12 --> Severity: Notice  --> Undefined property: MOrders::$9.03 C:\xampp\htdocs\codeigniter\system\application\models\morders.php 74
ERROR - 2009-01-13 20:08:30 --> Severity: Notice  --> Undefined index:  cart C:\xampp\htdocs\codeigniter\system\application\controllers\welcome.php 75
ERROR - 2009-01-13 20:08:32 --> Severity: Notice  --> Undefined index:  cart C:\xampp\htdocs\codeigniter\system\application\models\morders.php 11

So problems averted for the time being. There could be more issues later on.

Best,

Codeigniter rocks...when I'm not fixing errors lol
#6

[eluser]nunhes[/eluser]
Hello!
I´m use
Code:
$cart = $_SESSION["cart"];

and the error disapear
#7

[eluser]Aea[/eluser]
Let's get down to the core of the problem and not rely on simple hacky fixes (I'm truly surprised that using double quotes actually works, I'd be willing to bet that's an issue with differences in your levels of error reporting and not an actual fix, if it is than I'm once again disappointed with you PHP).

When you delete your cookies you delete the reference to the $_SESSION created for you (actually it's created based on a high entropy string that your cookie knows). This means that $_SESSION['cart'] does not exist. Now the rest of your code is doing some operations on cart, and it looks like it's checking for a product in your cart and possibly doing some operations on them, there is a check that prevents us going further, but no check that checks for an existing cart hence your error.

There are two ways to approach this problem, but both involve checking whether a card exist, as recommended before...

Code:
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();

This creates a new cart array. An alternative would be setting $cart to FALSE. Regardless, in both cases you might want to throw an error saying that you have nothing in your cart. I would personally err on the side of setting it to FALSE or even NULL in your check so you don't end up carrying around an unnecessary array() in your session, although the overhead for that would be entirely minimal.

I always use an isset() when accessing parameters which may not exist so I can explicitly check for them being NULL or FALSE later on (your preference, I prefer NULL). This can also save you problems later on, if you want to be extra careful do an isset($var{0}), this will ensure that our $var isn't set to "".

It's good practice to always explicitly check for NULL or FALSE types instead of pseudonulls like "".
#8

[eluser]TWP Marketing[/eluser]
Rick Jolly has it correct.

The code you copied uses both "left" and "right" single quotes, in every quoted array index:

Code:
function updateCart($productid,$fullproduct){
//pull in existing cart first!
$cart = $_SESSION[‘cart’]; <--- HERE
$totalprice = 0;
if (count($fullproduct)){
if (isset($cart[$productid])){
$prevct = $cart[$productid][‘count’]; <--- HERE
$prevname = $cart[$productid][‘name’]; <--- HERE
$prevname = $cart[$productid][‘price’]; <--- HERE

These should ALL be changed to either "right" hand single quotes (') or double quotes (").
Code:
function updateCart($productid,$fullproduct){
//pull in existing cart first!
$cart = $_SESSION['cart']; <--- HERE
$totalprice = 0;
if (count($fullproduct)){
if (isset($cart[$productid])){
$prevct = $cart[$productid]['count']; <--- HERE
$prevname = $cart[$productid]['name']; <--- HERE
$prevname = $cart[$productid]['price']; <--- HERE

You might check any other code copied from the same source for this problem.
#9

[eluser]nunhes[/eluser]
Sorry, I'm so newbie. And my english is not so good. Thanks for your comments and explanation.
I read the book Professional CodeIgniter and I'm working with the example application to understand something more about this so interesting framework.
Now, following your explanations, I have the application working but my shopping cart doesn´t work correctly:
-the delete buttom doesn´t work;
-if I edit the quantity and click update buttom, nothing happend...




Theme © iAndrew 2016 - Forum software by © MyBB