I have an array recieved by the code
$logged_in=$this->session->userdata('logged_in');
In this I have an element gid. I want to set value in this session with gid=2. How can I set this ??
use set_userdata() function as follows
$this->session->set_userdata('gid', 1);
if you want to set more than one value in session, use array method as follows
$session_array = array('gid' => 2, 'name' => 'john') ;
$this->session->set_userdata($session_array);
Using session
$this->load->library('session');
$this->session->set_userdata('gid', 2);
To get session value
echo $this->session->userdata('gid'); //Output is 2
$data=array('user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>md5($this->input->post('user_password')),
'product'=>implode(",", $this->input->post('product')),
'compney_name'=>$this->input->post('compney_name'),
'category'=>$this->input->post('category'),
'compney_type'=>$this->input->post('compney_type'),
'city'=>$this->input->post('city'),
'state'=>$this->input->post('state'),
'phone'=>$this->input->post('phone'),
'service_id'=>implode(",", $this->input-
>post('service_id')),
'image'=>$this->session->userdata('images_business')
);
$id = $this->session->userdata('lastid');
$this->db->where('user_id', $id)->update('user', $data);
$this->session->unset_userdata('lastid');
Related
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
I have stored the session varible in one controller and getting the value in another controller but the value is not passing
here is one controller
function control1 {
$this->session->set_userdata(array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
);
echo $this->session->userdata('value1'); //it returns value
}
function control2 {
echo $this->session->userdata('value1'); //it returns empty value
}
What may be the reason for this
You should check if the session has been set in the second controller like this:
function control2 {
if (!isset ($this->session->userdata('value1'))){
redirect('control1');
} else {
echo $this->session->userdata('value1'); //it returns empty value
}
If you haven't run through the control1 first, there won't be any session set yet, you see.
$this->session->set_userdata( 'values', array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
) );
$a = $this->session->userdata('values'); //it returns value
print_r( $a );
Try this code.
How the way you go into control2()? I mean, did you call control1() first to set the sessions value before goes to control2()? If so then the sessions value should be passed.
I am trying to build a month report with Codeigniter.
i have problem in parse value to view,
when i enable profiler, i get 12 month query
Controller
$this->load->model('dash_model');
$data1= $this->dash_model->get_user_all();
$ind = $this->dash_model->monthreport();
$this->output->enable_profiler(TRUE);
$data = array(
'mont' => $ind,
'blok' => $data1
);
print_r($data);
$this->parser->parse('blank', $data);
the output print_r data
Array
(
[mont] => Array
(
[0] => stdClass Object
(
[trans_email] => 0
)
)
and dash_model
for($i=1; $i<=12;)
{
$month=array("","01","2","3","4","5","6","7","8","9","10","11","12");
$m = $month[$i];
$query2=$this->db->query("select count(*) as trans_email from trans_email where lup LIKE '2014-$m%' ");
$i++;
}
return $query2->result();
how i get output select count(*) as trans_email from trans_email where lup LIKE '2014-01%' and next month to view ?
like
month 1 = 356 data
month 2 = 2000 data and next
i'm trying this : Codeigniter - passing multiple values to view
but nothing happens
update
i'm trying to add this code into dash_model
$i++;
$resultarray[$i]=$query2->result();
}
return $resultarray;
and i got some error
* Object of class stdClass could not be converted to string*
okay dude let me try to guest :D
let's assume you use array on your view, i can assume that because you initialize $data with array.
First make sure you read this userguide
on result_array() section.
then change $query->result(); to $query->result_array();
then try to var_dump() it, hope it's work
just pass it as
$data['mont'] = $ind;
$data['blok'] = $data1;
$this->parser->parse('blank', $data);
in the view get the data as of $ind as $mont
and $data1 as $blok.
You could do
$data = array();
$data['mont'] = $ind;
$data['blok'] = $data1;
Instead of declaring it then initializing it at the same time. (It also allow you to add/change data in it whenever you want in your controller).
Then do debug($data);to see if you got everything you want in $data.
I have a multidimensional array $data on the controller side. I populate $data[$group] with any group value, between G1 - G100. I then pass the array to a view via a controller:
$this->load->view('example', $data);
On the view-side I can then access the variable, for instance $G1, $G2. The problem is that I dont know before-hand what will be passed. I can try to access my variable like this in the view:
if (isset($G1)) echo $G1;
if (isset($G2)) echo $G2;
if (isset($G3)) echo $G3;
But this becomes highly unpractical when the group variable in $data[$group] on the controller-side can have many different values.
Is there any way to check from the view beforehand what is being sent?
I don't think it is possible to know what will be passed, but you can put $data itself into an array and pass this array to the view, and in the view go through $data with a foreach:
//controller
$newdata = array(
//maybe other data
'data' => $data
);
$this->load->view('someview', $newdata);
//view
foreach($data as $key => $value){
//do whatever you like
}
Model:
Grabs data from mySQL with the value: array['10','5','2'] and puts it in the session data
Controller:
Puts that session data into an array to pass to the view:
$fubar = array(
'ex1' => $this->session->userdata('ex1')
);
$this->load->view('view', $fubar);
View:
echo $ex1;
The view will spit out the array: array['10','5','2']
How do I parse that array to get the individual values (10, 5, or 2)?
For example, echo $ex1['1']; will spit out the first 1 characters: ar but I want it to return the value 5
I think perhaps I may not be storing the array properly in mySQL, but I'm not sure.
$fubar['ex1'] = $this->session->userdata('ex1');
$this->load->view('view', $fubar);
Now Try
$ex1[0];
$ex1[1];