ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Undefined Index: cart

January 10, 2009 12:40pm

Subscribe [5]
  • #1 / Jan 10, 2009 12:40pm

    clintonbeattie

    43 posts

    Hi,

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

    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

    $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…

    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 / Jan 10, 2009 1:04pm

    m4rw3r's avatar

    m4rw3r

    647 posts

    maybe do a simple check:

    $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
  • #3 / Jan 10, 2009 5:11pm

    Rick Jolly's avatar

    Rick Jolly

    729 posts

    There are invalid single quotes in your updateCart function.

  • #4 / Jan 11, 2009 5:45am

    clintonbeattie

    43 posts

    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 / Jan 13, 2009 4:23pm

    clintonbeattie

    43 posts

    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);) 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’] = 1;)

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

    These are the errors in log.php…

    <?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 / Mar 30, 2009 12:39pm

    nunhes

    2 posts

    Hello!
    I´m use

    $cart = $_SESSION["cart"];

    and the error disapear

  • #7 / Mar 30, 2009 5:08pm

    Aea

    83 posts

    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…

    $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 / Mar 30, 2009 6:37pm

    TWP Marketing

    596 posts

    Rick Jolly has it correct.

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

    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 (”).

    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 / Mar 31, 2009 9:20am

    nunhes

    2 posts

    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…

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases