CodeIgniter Forums
session header error!!! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: session header error!!! (/showthread.php?tid=28518)



session header error!!! - El Forum - 03-13-2010

[eluser]bang_dayat[/eluser]
I learn about Thomas myer book,..it pass both of PHP Session and CI Session to develop Shopping cart application,..I create the Model like this:
Quote:<?php
class MOrders extends model{
function MOrders(){
parent::model();
}
function updateCart($productid,$fullproduct){
$cart = $_SESSION['cart'];
$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' => $fullproduct['price'],
'count' => 1
);
}
foreach($cart as $id => $product){
$totalprice += $product['price']*$product['count'];
}
$_SESSION['totalprice']=$totalprice;
$_SESSION['cart']=$cart;
$this->session->set_flashdata('conf msg',"anda telah menambah produk ini ke keranjang belanja");
}
}
}
?>
then in the cart controller like this:
Quote:function cart($productid=0){
//gunakan sebagai keranjang belanja
if($productid > 0){
$fullproduct = $this->MProducts->getProduct($productid);
$this->MOrders->updateCart($productid,$fullproduct);
redirect('welcome/product/'.$productid,'refresh');
}
else{
$data['title']="toko online | keranjang belanja";
if (count($_SESSION['cart']) == true){
$data['main'] = '';
$data['navlist'] = $this->MCats->getCategoriesNav();
$this->load->vars($data);
$this->load->view('template');
}
else{
redirect('welcome/index','refresh');
}
}
}
but in the first time I run that application,,I found the error message like this:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: cart

Filename: models/morders.php

Line Number: 7
Quote:A PHP Error was encountered

Severity: Warning

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

Filename: libraries/Session.php

Line Number: 662
Quote:A PHP Error was encountered

Severity: Warning

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

Filename: helpers/url_helper.php

Line Number: 539
Please Someone Help Me to find the Problem,,..Thanx A LOT


session header error!!! - El Forum - 03-13-2010

[eluser]JanDoToDo[/eluser]
if you are using the native php sessions then you need to call session_start() to start the php session. If youve done that then cart variable doesnt exist and you should do

$cart = isset($_SESSION[‘cart’]) ? $_SESSION[‘cart’] : array();


session header error!!! - El Forum - 03-13-2010

[eluser]bang_dayat[/eluser]
[quote author="JanDoToDo" date="1268525996"]if you are using the native php sessions then you need to call session_start() to start the php session. If youve done that then cart variable doesnt exist and you should do

$cart = isset($_SESSION[‘cart’]) ? $_SESSION[‘cart’] : array();[/quote]

yeaa,..that's work,,..I have allready call session_start() in the parent::controller();
but how do you explain bout that code
Quote:$cart = isset($_SESSION[‘cart’]) ? $_SESSION[‘cart’] : array();
It's Really work!!how the way it can work like that??
Thanx,...


session header error!!! - El Forum - 03-13-2010

[eluser]JanDoToDo[/eluser]
Its called a ternary operator. Its a shorthand IF statement. it tests the first condition, i.e. isset($_SESSION['cart']) and then if that returns true it assigns $cart a value of $_SESSION['cart'] else it assigns it as an empty array fo you to work with Smile


session header error!!! - El Forum - 03-13-2010

[eluser]bang_dayat[/eluser]
[quote author="JanDoToDo" date="1268528416"]Its called a ternary operator. Its a shorthand IF statement. it tests the first condition, i.e. isset($_SESSION['cart']) and then if that returns true it assigns $cart a value of $_SESSION['cart'] else it assigns it as an empty array fo you to work with Smile[/quote]
yeaa,,I got it,..you and wikipedia really help me bout that "ternary operator"...

my last question is,,I call the ci session
Quote:$this->session->set_flashdata('conf msg',"anda telah menambah produk ini ke keranjang belanja");
then in the view I call that session look like this:
Quote:<?php
if($this->session->flashdata('conf_msg')){
echo "<div class='message'>";
echo $this->session->flashdata('conf_msg');
echo "</div>";
}
?&gt;
but that message not appear in the browser,..perhaps you can help bout that,..
really Thanx,..


session header error!!! - El Forum - 03-13-2010

[eluser]JanDoToDo[/eluser]
You're setting "conf msg" but calling "conf_msg" Smile Make sure you include the underscore lol Tongue


session header error!!! - El Forum - 03-13-2010

[eluser]bang_dayat[/eluser]
[quote author="JanDoToDo" date="1268532539"]You're setting "conf msg" but calling "conf_msg" Smile Make sure you include the underscore lol Tongue[/quote]
hahaha,..that's really lost in my eyes,.. :red:
anyway thanx a lot brad,..God Bless U :lol:


session header error!!! - El Forum - 03-13-2010

[eluser]JanDoToDo[/eluser]
NP man Smile glad you got it sorted Smile