I have lengthy search form and i am trying to keep the form values in session. I use codeigniter pagination, so i need to store the values entered in the search form in the session (i am trying to avoid storing them in the database)
This problem bother me couple of days and i am unable to solve it. Here is the code. For the sake of clarification i provide the comments for each line of my code:
// my submit button named search_button has been clicked
if ($this->input->post('search_button')) {
// all the values from the form are stored in array
$formValues = $this->input->post(NULL, TRUE);
// i assign the values of $formValues array to session
$this->session->set_userdata('formValues', $formValues);
// i store the values in local variable
$mySessData = $this->session->userdata('formValues');
// i count the values of the $formValues to be sure how many fields i get back
// reason for that is that my form is built dynamically
// returns 19 if the form has been submitted.
// when i submit the form, i get 19
echo "Count of form values is now: " . count($formValues);
// i print the $formValues array, to be sure that it return the form values
// and yes, it does if the form has been submitted.
// see the $formValues returns section bellow to see what is returned
echo "<pre>";
print_r($formValues);
echo "</pre>";
// i print my local variable to ensure that variable $mySessData is not empty
// see the section $mySessData retruns (1) to see returned values
echo "Confirm that sess data has been set up";
echo "<pre>";
print_r($mySessData);
echo "</pre>";
} else {
// else, means that form has not been submitted but that controller has been called from the next link
// in the pagination links. This always return empty array, that is nothing...
echo "<pre>";
print_r($mySessData);
echo "</pre>";
}
If the form has been submitted, the $formValues returns the following:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
My variable $mySessData returns the very same dataif the form has been submitted:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
And on the end, if i click on pagination link
$config['base_url'] = site_url('/searchresults/display/'.$limit.'/');
, which point to the same controller and same method, well..i get an empty array.
Any help will be appreciated.
Regards, John
I think It would be better if use method="get" in your form. posting your form with get instead of post would be more preferable in case of search form as u wont need to maintain any session and no need of storing values in database also. you can easily access a feild anytime using
$this ->input->get('fild_name');
You can optimize your code this way and you dont need to maintain session variables as you will always have the search feild valies in your url. Using session another problem is with unsetting the session variable.
I personally dont think that creating a session variable just to maintain pagination is not a good idea.Moreover in a search form there is no need of security so you can send your form using "get" insted of "post"
Related
The manual pagination I found while googling works fine but I was just wondering what does the 'query' => $request->query() in the option parameter does?
$total = count($form_list);
$per_page = 10;
$current_page = $request->input('page') ?? 1;
$starting_point = ($current_page * $per_page) - $per_page;
$form_list = array_slice($form_list, $starting_point, $per_page, true);
$form_list = new Paginator($form_list, $total, $per_page, $current_page, [
'path' => $request->url(),
'query' => $request->query(),
]);
Calling ->query() without any parameters returns all the values from the query string as an associative array.
Suppose you have a query string like this:
https://example.com/path/to/page?name=ferret&color=purple
You can retrieve the value of name by doing something like so:
$request->query('name')
which returns ferret. You can also pass a second parameter for a default value so if you call:
$request->query('size', 'Medium')
which doesn't exist on the query string, you'll get 'Medium' instead of null.
If you omit all parameters, you'll receive an associative array that looks something like this:
query = [
'name' => 'ferret',
'color' => 'purple',
]
The options parameter is not needed by the pagination itself but for your dataset query. If you do not pass the query parameter, when you click one of the pagination urls, you'll get something like this:
https://example.com/path/to/page?page=2&per_page=5
Sometimes, this works fine and will give us something that we want but sometimes, we need those additional query string to get the correct dataset. We pass in all values from our query to get something like this:
https://example.com/path/to/page?page=2&per_page=5&name=ferret&color=purple
Which will filter your dataset for all those purple ferrets. As for the question if you need it, it's up for you to decide if that is essential for your code or if you can get away with just pagination.
Good luck! I hope this helps.
I trying to insert a row in codeigniter and row inserted.
Problem is all row inserted properly but in sql bighint(11) field inserted 0.
I checked properly in array value given.
$data = [
'sku' => $POST['rec1'],
'pruch_price' => $POST['rec2'],
'sell_price' => $POST['rec3']
];
$model->insert ($data);
you should use $_POST[] instead of $POST.
But better yet, don't send $_POST directly to the models, instead use the post request provided by Codeigniter 4.
$data = [
'sku' => $this->request->getPost('rec1'),
'pruch_price' => $this->request->getPost('rec2'),
'sell_price' => $this->request->getPost('rec3')
];
$model->insert($data);
I want to except empty value field from $request->all();
Array ( [first_name] => Dev 1 [password] => [last_name] => [phone] => 123456 [password_confirmation] => )
I got the array like this but I want to except field from above array like last_name, password which has no value.
Any way to do this without for loop.
I mean laravel provide any default method for that ?
Thanks
array_filter will remove empty elements:
$filtered = array_filter($request->all());
I am following nettut+ tutorial for pagination and to store POST inputs as querystrings in db. So far, everything works fine until, suppose if I get an array as POST input, i am unable to loop through it and get all the array values and to store into query_array (i.e., store array within array).
The snippets below:
$query_array = array(
'gender' => $this->input->post('gender'),
'minage' => $this->input->post('minage'),
'maxage' => $this->input->post('maxage'),
'Citizenship' => $this->input->post('citizenship'), // checkboxes with name citizenship[]
);
This returns only last stored array value in Citizenship.
The output array:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 )
makes the query string as:
&gender=1&minage=18&maxage=24&Citizenship=2
But, my requirement is to get all the values of 'Citizenship' array instead of last stored value.
The output required to make query string:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 [Citizenship] => 4 [Citizenship] => 6 )
The query string :
&gender=1&minage=18&maxage=24&Citizenship[]=2&Citizenship[]=4&Citizenship[]=6
Any help appreciated..
Thanks.
Doesn't look like code ignighter supports un-named multidimensional arrays as input without a bit of hacking.
If you can access raw $_POST data try replacing
$this->input->post('citizenship')
with
array_map('intval',$_POST['citizenship'])
Alternativly add keys to your post data:
&gender=1&minage=18&maxage=24&Citizenship[0]=2&Citizenship[1]=4&Citizenship[2]=6
I fixed it myself. I just looped through the POST array and got the individual array key & pair values.
foreach($_POST['Citizenship'] as $k => $v) {
$Citizenship[$v] = $v;
}
Hope this helps someone who face similar problem.
I'm trying to create a bespoke form in drupal, with a node reference field.
I'd like to add a little extra functionality to the node reference auto complete. I've created a view, that contains an argument. I'd like to be able to pass that argument from a drop down as well as the typed text into the autocomplete script.
Does anyone know how I'd start this off.
/* FIELD 1 - the drop down */
$sql = "SELECT nid, title FROM node where type='resourcetype' AND status =1 order by title
";
$result = db_query($sql);
$counter = 0 ;
$options = array();
while ($data = db_fetch_array($result)) {
// krumo ($data);
$options[$data[nid] ] = $data[title] ;
if ($counter ==0 ) {$df = $data[nid]; }
$counter ++;
}
/* FIELD 2 - the node reference field */
$form['sor']['type'] = array(
'#type' => 'select',
'#title' => t('Resource type'),
'#required' =>TRUE,
'#options' => $options,
) ;
$form['sor']['field_asor_sors'] = array(
'#type' => 'textfield',
'#title' => t('Add a SOR item to this job'),
'#autocomplete_path' => 'nodereference/autocomplete/field_asor_sors',
'#element_validate' => array('myelement_validate_is_valid_noderef'),
'#required' =>TRUE,
);
Many Thanks
Matt
AFAIK there is no easy way to do this.
I wanted to do something similar a while ago (using different arguments based on node context), but refrained from doing it, since it would've needed some significant changes of the autocomplete callback logic. You'd need to change several nodereference functions to add support for passing an argument to the initial callback nodereference_autocomplete(), passing it on from there to _nodereference_potential_references(), and finally to _nodereference_potential_references_views(), while ensuring that the changes don't break anything else.
If you want to try nonetheless, you should take a look at the patches in this thread, as they also want to do something like that and might contain some useful hints/examples.
A potentially easier alternative might be to exchange the #autocomplete_path callback of the nodereference field with your own custom version that would generate the result, while adding js logic to your dropdown to add an additional argument to that path when the selection changes.
If you go into the nodereference field configuration form, and scroll all the way to the bottom, there's a fieldset (which may be collapsed) that is titled 'Advanced - Nodes that can be referenced (View)'. You can use this to select a view and have that view be the source of the nodereference choices without writing any new code.