Welcome Guest, Not a member yet? Register   Sign In
Session Class - Add, Edit and Remove
#1

[eluser]webdude[/eluser]
Are you able to update and remove items in a session using the Session Class?

I am building a quoting system which acts like a "shopping cart" where the user adds products on which they want a quote on and will require them to be able to add new items, remove existing or update existing items.

Thanks in advanced.
#2

[eluser]Phil Sturgeon[/eluser]
Indeed you can.

User Guide: Sessions

Add/Edit

Code:
$this->session->set_userdata('some_name', 'some_value');

Delete
Code:
$this->session->set_userdata('some_name', NULL);
#3

[eluser]webdude[/eluser]
Thanks will give that ago a bit later. Should I use the form helper or use a standard html coded form? I have a list of products each with a qty textbox next to them and a single submit button at the bottom of the page.

Thanks once again.
#4

[eluser]Derek Allard[/eluser]
There's also an undocumented (well, its in the SVN now)
Code:
$this->session->unset_userdata('item');
available if you want to remove it from the userdata array. Its essentially the same thing that pyromaniac posted (thanks pyro, you've been prolific lately Wink)
#5

[eluser]webdude[/eluser]
is this correct?
Code:
function add()
        {
            $this->session->set_userdata($array);
            $newdata = array('product' => 'test','code' => '001');
            $this->session->set_userdata($newdata);
        }
#6

[eluser]Derek Allard[/eluser]
Code:
function add()
        {
//            $this->session->set_userdata($array); // don't need this line
            $newdata = array('product' => 'test','code' => '001');
            $this->session->set_userdata($newdata);
        }
#7

[eluser]webdude[/eluser]
thanks for the reply.

below is a cut down version of my page but i am unable to call/show $this->session->userdata('product');
Code:
<?php
    class Product_details  extends Controller {
    
        function productdetails()
        {
            $this->load->library('session');
            echo "session id = ".$this->session->userdata('session_id');
            echo "<br>";
            echo "product = ".$this->session->userdata('product');
            echo "<hr />";
        }
        
        function add()
        {
            $newdata = array('product' => 'test','code' => '001');
            $this->session->set_userdata($newdata);
        }
    
    }
?&gt;
#8

[eluser]Derek Allard[/eluser]
try this. Essentially, I'm loading the session library in the contructor so that its available for both add() and productdetails(), and I'm making sure I get session stuff added BEFORE I try to see the results. (I'm also using the url helper, but that's just for convenience.)


Code:
&lt;?php
class Product_details  extends Controller {

        function Product_details()
        {
            parent::Controller();
            $this->load->library('session');
            $this->load->helper('url');
        }

        function index()
        {
            echo anchor('Product_details/add', 'add product first') . ' and then ';
            echo anchor('Product_details/productdetails', 'see the session details');
        }
    
        function productdetails()
        {

            echo "session id = ".$this->session->userdata('session_id');
            echo "<br>";
            echo "product = ".$this->session->userdata('product');
            echo "<hr />";
        }
        
        function add()
        {
            $newdata = array('product' => 'test','code' => '001');
            $this->session->set_userdata($newdata);
        }
    
    }
?&gt;
#9

[eluser]webdude[/eluser]
Derek, thanks very much for your help and your super fast replies :-)

I have updated my code and it works :-) The only issue I have now (well for the moment) is being able to detect dynamically name form items.

My form
Code:
&lt;?=form_open($_SERVER['REQUEST_URI']."add");?&gt;
  &lt;?php foreach($derivatives->result() as $row):?&gt;
    &lt;?=$row->code?&gt;</strong><br />
    <label for="code">QTY</label>&lt;input name="&lt;?=$row-&gt;code?&gt;" type="text" id="&lt;?=$row->code?&gt;" value="1" size="3" /><br /><br />
  &lt;?php endforeach;?&gt;
  &lt;input type="submit" value="Submit"  /&gt;
&lt;/form&gt;

produces
Code:
&lt;form action="http://bisvalves.orbitalweb.co.uk/products/solenoid-valves/direct-solenoid-valve/add" method="post"&gt;      6B37-A3-S-P</strong><br />
    <label for="code">QTY</label>&lt;input name="6B37-A3-S-P" type="text" id="6B37-A3-S-P" value="1" size="3" /&gt;<br /><br />
      6B37-A3-S-N</strong><br />
    <label for="code">QTY</label>&lt;input name="6B37-A3-S-N" type="text" id="6B37-A3-S-N" value="1" size="3" /&gt;<br /><br />
    &lt;input type="submit" value="Submit"  /&gt;
&lt;/form&gt;
#10

[eluser]Derek Allard[/eluser]
You're welcome. You probably want to start another thread with that, but that said, try to re-word your problem, I'm not sure I understand it. At any rate, a new thread is definately appropriate. Wink




Theme © iAndrew 2016 - Forum software by © MyBB