Why am I not able to retrieve data - Codeigniter? - php

I recently added a column called user_cat to a table. I am not able to get this date to display on the webpage. However, I am able to display the data from columns user_id, email_id on the webpage using session variables using below.
$this->session->set_userdata("user_id", $this->session->userdata('loginUser') );
$this->session->set_userdata("user_id", $this->session->userdata('loginEmail') );
I checked the models page to and found the code to be like this.
$this->db->select('user_id,email_id');
&
'loginUser' => $row->user_id,
'loginEmail' => $row->email_id,
which i then changed to:
$this->db->select('user_id,email_id,user_cat');
&
'loginUser' => $row->user_id,
'loginCat' => $row-> user_cat,
'loginEmail' => $row->email_id,
Now i used the session variable like this:
$this->session->set_userdata("user_id", $this->session->userdata('loginCat') );
This did not work. I need help.

I assume that you didn't write the code yourself.Data from the database already set on the sessions. You can directly retrieve data using
this->session->userdata('loginUser')
So look for code like following in the model
$this->session->loginUser = //something;
//add your code
$this->session->loginCat = //similar to above
Take a look at Database reference.
However , following is the answer to your question
You are using same key to all session data. You should use a unique key for each session variable.
$this->session->set_userdata("user_id", $this->session->userdata('loginUser') );
$this->session->set_userdata("user_email", $this->session->userdata('loginEmail') );
$this->session->set_userdata("user_cat", $this->session->userdata('loginCat') );
Take a look at Session library.

first set you session like this
$data = array(
'loginUser' => $row -> user_id ,
'loginEmail' => $row -> email_id,
'loginCat' => "" ,
);
$this -> session -> set_userdata ( $data );
then update it
$data = array('loginCat' => $row -> user_cat );
$this -> session -> set_userdata ( $data );

Related

Codeigniter - input data save in multiple arrays and db table

I've an existing form which is passing the input data to the model in an array format. $postdata has all the data from the view and sending to model.
Controller:
$inquiry_id = $this->input->post('inquiry_id');
$postdata = $this->input->post();
$this->load->model('Design_model');
$this->Design_model->insertdata($postdata,$inquiry_id);
Model:
function insertdata($data = array(), $inquiry_id){
$sql = $this->db->query("select * from design where inquiry_id='".$inquiry_id."'");
if($sql->num_rows() == 0){
$sql_query = $this->db->insert('design', $data);
}
else{
$this->db->where('inquiry_id', $inquiry_id);
$this->db->update('design', $data);
}
}
Above is working fine. Now, I'd like to add few fields in the view and save in a different database table. Need to exclude the new field values from $postdata array getting saved. Need to find the best approach to do this. I can start with some name for all the new fields, so that we can add any filter if available to exclude from the $postdata.
You can use elements() function from Array helper.
$array = array(
'id' => 101,
'title' => 'example',
'desc' => 'something',
'unwanted' => 'bla bla'
);
$filtered_array = elements(array('id','title','desc'),$array); //you can use this directly to the post data
$this->Design_model->insertdata($filtered_array,$inquiry_id);
You can use array_merge() or array_push() functions to add new fields to the array.
Let's say you have following data
$postdata = array("name"=>"xyz",
"email"=>"xyz#gmail.com",
"age"=>"40",
"gender"=>"Male",
"occupation"=>"Engineer"
);
Of which first 3 records are from old fields and last 2 are from new fields as you saying.
You need to find last index of first set i.e. '3' Now you can do this.
$firstDb = array_splice($postdata,0,3); //here 3 is index we are using to get first 3 records from $postdata
$secondDb = array_slice($postdata,0,3); //here 3 is index we are using to get records from position 3 from $postdata
Output:
$firstDb = array("name"=>"xyz","email"=>"xyz#gmail.com","age"=>"40");
$secondDb = array("gender"=>"Male","occupation"=>"Engineer");
Now you can insert you records as you wish to. Happy coding

Codeigniter: Accessing session data from array

So I have stored my session data into the session like below:
$user_data = array(
'user_id' => $user_id,
'email' => $email,
'logged_in' => true
);
$this->session->set_userdata('login_session', $user_data);
In my view, I'm trying to echo the email out onto the page but no matter which way I try to access it, it will not echo out:
echo $this->session->userdata('email');
How can I access the array values?
You are storing the values in an array & then assigning that array to a variable called login_session but while retrieving the data you are just accessing with only key and not with array['key'].
So the correct syntax is:
echo $this->session->userdata('login_session')['email'];
OR
$log_sess=$this->session->userdata('login_session');
echo $log_sess['email'];
Please go through the codeigniter document as well.

How to clear $this to insert more records in CodeIgniter?

With CI, I want to insert one record in User table and one in Post table. Below is a brief of my code (two tables will have multiple columns, and I just use one as example).
$this->username=$user;
$this->db->insert('User',$this);
$this->title='my first post';
$this->db->insert('Post', $this);
However, the second insert will be something like "insert into Post (user, title) values ('$user', 'my first post'). And an error is reported that unknown column user in Post.
How can I clear the members in $this before inserting the next records (in another table)?
This happening becouse of
$this->username=$user;
You probably need to use
$this->db->insert('Post', $this->title);
And before you insert, set in title anything you want, but not
$this->db->insert('Post', $this);
However if you still want to work with an object, more information how to do this properly you can find here, http://ellislab.com/codeigniter/user-guide/database/active_record.html#insert
CI used array as the second argument in the insert method. The index will be the column name and the value referred by the index will be the value to be inserted. What you did was you just keep adding into the $this array.
When you first add username the array will look like this(neglecting the db) inside $this :
array( 'username' => $user );
When you add the title, it will become like this:
array( 'username' => $user, 'title' => 'my first post');
See how the previous entry still in there.
You can just unset($this->username);
or you can use another variable to hold your data instead of $this. Example:
$data = array('username' => $user);
$this->db->insert('User',$data);
$data = array('title' => 'my first post');
$this->db->insert('Post',$data);
And you can insert into two columns like this:
$data = array('username' => $user, 'description' => 'i am sleepy');
$this->db->insert('User',$data);
Hope my answer can help you.

$this->Model->id not working before saveAll in CakePHP

I have the following code in CakePHP 2:
$this->Order->id = 5;
$this->Order->saveAll(array(
'Order' => array(
'person_id' => $this->Session->read('Person.id'),
'amount' => $total,
'currency_id' => $code
),
'Lineitem' => $lineitems /* a correctly-formatted array */
));
I would expect this to update the row with the Primary Key of 5 in the Order table and then insert the Lineitem rows with an order_id of 5.
However, all it does is create a new row in Order and then use the new id from the new Order record to create the Listitem rows.
Note: I'm only setting the ID as above for debugging purposes and to easily demonstrate this question. In my final code, I'll be checking to see if there's already a pending order with the current person_id and doing $this->Order->id = $var; if there is and $this->Order->create(); if there isn't.
In other words, sometimes I will want it to INSERT (in which case I will issue $this->Order->create(); ) and sometimes I will want it to UPDATE (in which case I will issue $this->Order->id = $var; ). The test case above should produce an UPDATE but it's producing an INSERT instead.
Any idea what I am doing wrong here?
The array you pass to Model->saveAll() doesnt't contain the order's id, so Cake creates a new one. If you wanto to update an existing record, either you set the order id in the passed array, or you retrieve it with a find. The documentation explicitly remarks
If you want to update a value, rather than create a new one, make sure
your are passing the primary key field into the data array
$order = $this->Order->findById(5);
// ... modify $order if needed
$this->Order->saveAll(array('Order' => $order, 'LineItem' => $items));
In your case, you may want to use something like the following to be as concise as possible. Model::saveAssociated() is smart enough to create or update depending on the id, but you must provide suitable input. Model::read($fields, $id) initializes the internal $data: for an existing record all fields will be read from the database, but for a nonexistent id, you'll need to supply the correct data for it to succeed. Assuming an order belongsTo a customer, I supply the customer id if the order doesn't exist
// set the internal Model::$data['Order']
$this->Order->read(null, 5);
// You may want to supply needed information to create
// a new order if it doesn't exist, like the customer
if (! $this->Order->exists()) {
$this->Order->set(array("Customer" => array("id" => $customer_id)));
}
$this->Order->set(array('LineItem' => $items));
$this->Order->saveAssociated();
As a final note, it seems you are implementing a shopping cart. If that's the case, maybe it'd be clearer to use a separate ShoppingCart instead of an Order with a finalized flag.
Have you tried following:
$this->Order->saveAll(array(
'Order' => array(
'id' => 5,
'person_id' => $this->Session->read('Person.id'),
'amount' => $total,
'currency_id' => $code
),
'Lineitem' => $lineitems /* a correctly-formatted array */
));
Its pretty much the same what you did with :
$this->Order->id = 5;
Maybe that would fix your problem.
Cake is checking if you set id field and if its there it updates record, if not found it creates new record instead.
update:
Then maybe check before you saveAll if there is id field, then save result of check to some boolean and create array to save determined by this boolean for example:
if($id_exist) $order['Order']['id'] = 5;
$order['Order']['id'] = 5;
$order['Order']['person_id'] = $this->Session->read('Person.id'),
$order['Order']['amount'] = $total;
$order['Order']['currency_id'] = $code;
$this->Order->saveAll(array(
'Order' => $order,
'Lineitem' => $lineitems /* a correctly-formatted array */
));

Codeigniter says session userdata blank, but exists in db

Whenever i try check for a value in my userdata column of my session it thinks it's blank, when it's actually in the ci_sessions table serialised.
here's the db content unserialised:
array (
'user_data' => '',
'edit' =>
array (
'image_id' => 'HF',
'session_id' => '783c15b057bcd9c19d3fd82f367ee55d',
),
)
here's how im checking for the userdata in my view (note sessions are autoloaded)
<?php if ($this->session->userdata('edit')) : ?>
enter code here
and here's how i set the session userdata:
$values = array(
'image_id' => implode(",",$uploaded_image_ids),
'session_id' => $this->session->userdata('session_id')
);
$this->session->set_userdata('edit', $values);
Can anyone see the problem? (whole controller here http://pastebin.com/aXeRn1VN)
Also heres my config.php http://pastebin.com/A31nrC1b
View variables are separate from other variables in CI. I think (am still a bit of a CI newbie) that $this->session is not available in Views. This CI forum post discusses a couple of possible solutions to the problem http://codeigniter.com/forums/viewthread/100587/#508098 - one is to pass the value of 'userdata' from the session into the $data array you pass to the view - which seems the best solution.
You can use like this
<?php $edit = $this->session->userdata('edit'): if (!empty($edit)) : ?>

Categories