[eluser]shinokada[/eluser]
I am reading a Codeigniter book "Professional Codeigniter".
The welcome.php start session in constructor.
When I click "Add to cart" button, I get error message relating to the session.
Add to cart button is linked welcome/cart/4 etc.
If I refresh or go back and try it, then it does not show errors any more.
However if I delete cookies and repeat the above procedure, I get the same error messages.
If I change index.php error reporting from error_reporting(E_ALL) to error_reporting(E_ALL & ~E_NOTICE);, errors do not appear any more.
Code:
error_reporting(E_ALL & ~E_NOTICE);
Could anyone tell me if there is another way to solve this?
And why changing error reporting solve this problem?
Regards.
welcome.php
Code:
class Welcome extends Controller {
function Welcome(){
parent::Controller();
session_start();
$this->output->enable_profiler(FALSE);
}
function index(){
$this->output->cache(30);
$data['title'] = "Welcome to Claudia's Kids";
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = $data['mainf']['id'];
$data['sidef'] = $this->MProducts->getRandomProducts(3,$skip);
$data['main'] = 'home';
$this->load->vars($data);
$this->load->view('template');
}
...
...
function cart($productid=0){
if ($productid > 0){
//$productid = $this->uri->segment(3);
$fullproduct = $this->MProducts->getProduct($productid);
$this->MOrders->updateCart($productid,$fullproduct);
redirect('welcome/product/'.$productid, 'refresh');
}else{
$data['title'] = "Claudia's Kids | Shopping Cart";
if (count($_SESSION['cart'])){
$data['main'] = 'shoppingcart';
$data['navlist'] = $this->MCats->getCategoriesNav();
$this->load->vars($data);
$this->load->view('template');
}else{
redirect('welcome/index','refresh');
}
}
}
...
...
models/moders.php
Code:
class MOrders extends Model{
function MOrders(){
parent::Model();
}
function updateCart($productid,$fullproduct){
//pull in existing cart first!
$cart = $_SESSION['cart'];//$this->session->userdata('cart');
$productid = id_clean($productid);
$totalprice = 0;
if (count($fullproduct)){
if (isset($cart[$productid])){
$prevct = $cart[$productid]['count'];
$prevname = $cart[$productid]['name'];
$prevprice = $cart[$productid]['price'];
$cart[$productid] = array(
'name' => $prevname,
'price' => $prevprice,
'count' => $prevct + 1
);
}else{
$cart[$productid] = array(
'name' => $fullproduct['name'],
'price' => $this->format_currency($fullproduct['price']),
'count' => 1
);
}
foreach ($cart as $id => $product){
$totalprice += $product['price'] * $product['count'];
}
$_SESSION['totalprice'] = $this->format_currency($totalprice);
//$this->session->set_userdata('totalprice', $totalprice);
$_SESSION['cart'] = $cart;
//$this->session->set_userdata('cart',true);
$this->session->set_flashdata('conf_msg', "We've added this product to your cart.");
}
}
...
...
error messages.
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_shopping_original\system\libraries\Exceptions.php:166)
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_shopping_original\system\libraries\Exceptions.php:166)
Filename: helpers/url_helper.php
Line Number: 539