[eluser]gbar[/eluser]
Hello. I'm developing a crud, and i'm in the process of saving data from the search field.
I decided to use the session to save two data post, referring to the search term, and the table in which to search.
There are several posts in this forum about this topic, but I've not found something similar to my problem.
Controller code:
Code:
function authors($sortBy = 'id', $sortOrder = 'desc', $offset = 0)
{
$limit = 5;
$data = array(
'search' => $this->input->post('search'),
'field' => $this->input->post('field')
);
$this->session->set_userdata($data);
$results = $this->authors_model->_retrieveAuthors($limit, $offset, $sortBy, $sortOrder);
$data['records'] = $results['my_rows'];
$data['numRows'] = $results['num_rows'];
$config = array();
$config['base_url'] = site_url('admin/sections/authors/' . $sortBy . '/' . $sortOrder);
$config['total_rows'] = $data['numRows'];
$config['per_page'] = $limit;
$config['uri_segment'] = 6;
$config['first_link'] = img(base_url() . 'img/first.png');
$config['last_link'] = img(base_url() . 'img/last.png');
$config['next_link'] = img(base_url() . 'img/next.png');
$config['prev_link'] = img(base_url() . 'img/previous.png');
$config['num_tag_open'] = '<span class="navigation">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="navigation_current">';
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
}
Model code:
Code:
function _retrieveAuthors($limit, $offset, $sortBy, $sortOrder)
{
$search = $this->session->userdata('search');
$field = $this->session->userdata('field');
$key_field = $field == '' ? 'author' : $field;
$this->db->like($key_field, $search);
$this->db->order_by($sortBy, $sortOrder);
$this->db->limit($limit, $offset);
$query = $this->db->get('authors');
if($query->num_rows() > 0)
{
$data['my_rows'] = $query->result();
$this->db->like($key_field, $search);
$query = $this->db->count_all_results('authors');
$data['num_rows'] = $query;
return $data;
}
}
View code:
Code:
<?php echo form_open('admin/sections/authors'); ?>
<?php
$options = array(
'id' => 'Id',
'author' => 'Author',
'biography' => 'Biography',
'site' => 'Site Url'
);
?>
<?php echo form_dropdown('field', $options, $field == '' ? 'author' : $field); ?>
<?php echo form_input('search', $this->session->userdata('search')); ?>
<?php echo form_submit('submit', 'Search'); ?>
<?php echo form_close(); ?>
Everything works fine, in the search field if I select a word, I get a list of records with that search term. But when I click on the second page, I lose the session data back and i see all records in the database.
I also tried to use flashdata, I get a strange result. At first click on the form, nothing happens, the second click I get the list of records containing the term, it works until the second page. from the third page I lose the data.
someone has an idea?
Thanks