Updating session vaules in CodeIgniter - codeigniter

Have a little problem, how to update single value in CI session.
I have
$data['jezik'] = $this->uri->segment(1);
$this->session->userdata('jezik',$data['jezik']);
$data['jezik']= $this->session->userdata('jezik');
But it does not change the value. Always the same!

Try:
$this->session->set_userdata('jezik',$data['jezik']);

You need to use set_userdata rather than userdata. There are two ways to do it. You can do it in a key/value type way
$this->session->set_userdata('jezik', $data['jezik']);
Or you can pass an array
$sessionData = array('jezik' => $data['jezik'])
$this->session->set_userdata('jezik',$data['jezik']);
You can read more about CodeIgniter sessions here

Like #Pattle said
$this->session->set_userdata('jezik', $data['jezik']);
Though I think he meant to pass in the array, like so:
$sessionData = array('jezik' => $data['jezik']);
$this->session->set_userdata($sessionData);

Related

Laravel store request all better way

I have following code:
//Create Assignment
$customer['projects_id'] = $Project->id;
$customer['user_id'] = $customeruser['user_id'];
$customer['user_name'] = $SalesRepresentative->user_name;
$customer['user_role'] = "Admin";
ProjectUser::firstOrCreate($customer);
Is there a way to write this code cleaner and don't have 4 lines? Maybe one instead? Thanks. request has a lot more values.
If you are using a validating request class you can simply call ProjectUser::firstOrCreate($request->validated()); in that case, only validated variables are passed to store method.

Codeigniter Route to accept dynamic values

I am trying to create a router which will take a dynamic value and forward it to the actual route. In normal case it would be like
$route['login'] = 'auth/login';
It is possible to catch a parameter before the login in the above parameter and pass it to as the first parameter to the actual route ? like
$route['^(.+)/login$'] = "$1/user/login";
Check out the documentation[docs]. There is a very easy way to do this.
$route['(:any)/login'] = '$1/auth/login';
Starx answer is correct but I wouldn't do that, as your first param will be your controller, and if you don't know what's in the dynamic value that's impossible to manage.
I would do :
$route['(:any)/login'] = 'user/login/$1';
This way, 'anything/login' would be redirected to your User controller, to the Login function with the parameter 'anything'.

Codeigniter problem with routes!

I am trying to do this route trick:
$route['cp/roles/:num'] = "cp/roles/index/:num";
but it doesn't work :(
please help me!!
advanced thanks .
According to the documentation on URI Routing:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
“A URL with "product" as the first segment, and a number in the second will be remapped to the "catalog" class and the "product_lookup_by_id" method passing in the match as a variable to the function.”
So, for your particular instance, you would do the following:
$route['cp/roles/(:num)'] = "cp/roles/index/$1";
You could try
$route['cp/roles/:num'] = "cp/roles";
and then instead of passing a variable in your function you use
$this->uri->segment(3);
or the number that correspond to the segment.

Mutliple URL Segments to Index Function With CodeIgniter

Please excuse me if this is an incredibly stupid question, as I'm new to CodeIgniter.
I have a controller for my verification system called Verify. I'd like to be able to use it something like site.com/verify/123/abcd, but I only want to use the index function, so both URL segments need to go to it.
I'm sure this can be done with URL routing somehow, but I can't figure out how to pass both URL segments into Verify's index function..
Something like this in routes.php should do the job:
$route['verify/(:any)/(:any)'] = "verify/index/$1/$2";
I'm pretty sure you can just pass any controller method in CodeIgniter multiple arguments without modifying routes or .htaccess unless I misunderstood the problem.
function index($arg_one, $arg_two)
{
}
$arg_one representing the 123 and $arg_two representing the abcd in your example URI.
You will either need to edit the routes or write an htaccess rule, however i didn't understand why you want to limit to just the index function.
If you didnt wanna use routes for some reason, then you could add this function to the controller in question.
public function _remap($method_in, $params = array()) {
$method = 'process_'.$method_in;
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $params);
}
array_unshift($params, $method_in);
$this->index($params);
}
Basically it does the same as default behavior in CI, except instead of sending a 404 on 'cant find method', it sends unfound method calls to the index.
You would need to alter your index function to take an array as the first argument.
OR if you know that you only ever want 2 arguments, you could change the last 2 lines to
$this->index($method_in, $params[0]);
Of course both solutions fail in someone uses an argument which is the same as a method in your controller.

codeigniter associative array in post

I normally name my db specific fields in my forms like this "objectname[columnname]", I tseems CI cant access these values using $this->input->post('objectname[columnname]'), what do I do? there is not a chance in hell im renaming 100+ form fields.. I am actually disliking CI, it really is getting in the way of progress by changing the de facto PHP norms...
And were you using $_POST['objectname[columnname]'] or $_POST['objectname']['columnname'] ?
Have you tried the equivalent for the latter
$obj = $this->input->post('objectname');
echo $obj['columnname'];
?
If it works, you can write you own helper to retreive that like post_val('objectname[columnname]').
I saw this post whilst looking for a similar issue, but worked out a CI way to do it, sorry if I'm resurrecting it, but it does appear fairly high on the Google results.
// Load the 'array' helper
$this->load->helper('array');
// Use the 'element' function to return an element from the array
echo element('ColumnName', $this->input->post('ObjectName'));
Hope this helps anyone who comes here in future.
HTML code:
<input type="text" value="" name="myPostArrayName[]">
<input type="text" value="" name="myPostArrayName[]">
Handling form with codeigniter:
$data = $this->input->post('myPostArrayName', TRUE);
You can access data in order like this
echo 'Value of the first element in the form array is '.$data[0];
echo 'Value of the second element in the form array is '.$data[1];
I think someone who has access to codeigniter documentation, had better to add a simple html post array handling example.
I seems I can rely on the $_POST var, but I thought this was reset?
You can cast the post array as an object and use method chaining to return sub-arrays (now properties) using PHP 5.3's method chaining all on one line.
Extend the input class by making a class called MY_Input and put the extended class in the application/core folder. CI 2.0 will automatically use the extended class with the MY_ prefix, and you can add methods to this new class. Extending the input class is cleaner than making helpers.
This method casts the post array, or a nested array (a sub array below the parent), as an object.
/* Cast an array from CI post as an object and return the object */
public function post_obj($key = null){
$post_return = $this->post($key);
if (false === $post_return)
return false;
return (object)$post_return;
}
Now I can retrieve nested values in one line of code using PHP 5.3's method chaining for objects.
$active = $this->input->post_obj('user')->active;
I just went with the $_POST['objectname']['colname'] option as i usually do even though this is probably not the CI way..

Resources