Welcome Guest, Not a member yet? Register   Sign In
Pagination Problem with submitted query (solved)
#1

[eluser]gazza7364[/eluser]
I've a problem with pagination, If I set the $input as a static value eg Dixons Wood the pagination works correctly. If I set the value as the post value from a submitted form, the pagination shows the first page correctly, but when I click on say page2 or whatever link, I start start getting all the records from the database. I ran this with
this with profiler enabled and I get the following results

This is for the first page
SELECT COUNT(*) AS `numrows`
FROM (`photos`)
WHERE `subject` = 'Dixons Wood'

SELECT `photo_id`, `subject`, `photo_album`, `orientation`, `thumbpath`, `imagename`
FROM (`photos`)
WHERE `subject` = 'Dixons Wood'
ORDER BY `orientation`
LIMIT 5

When I click a link on the pagination it shows the following:-
SELECT COUNT(*) AS `numrows`
FROM (`photos`)
WHERE `subject` = 0

SELECT `photo_id`, `subject`, `photo_album`, `orientation`, `thumbpath`, `imagename`
FROM (`photos`)
WHERE `subject` = 0
ORDER BY `orientation`
LIMIT 5, 5

As you can see the pagination loses the subject criteria.

Like I said if I put a static value into the $input variable everything works.

I think I see what's happening, that when you click a pagination link, it runs through the code again, but thist time their is no post value.

How do I fix?

This is my controller.

Code:
function multiedit() {
  
  $input = $this->input->post('subject');
  
  echo $input;//for testing
  $this->load->library('pagination');
  
  $config['base_url'] = 'http://localhost/photos/multiedit';
  //$config['total_rows'] = $this->db->get('photos')->num_rows();
  $config['total_rows'] = $this->photo_model->mycount($input);
  $config['per_page'] = 5;
  $config['num_links'] = 3;
  $config['full_tag_open'] = '<div id="pagination">';
  $config['full_tag_close'] = '</div>';
  
  $this->pagination->initialize($config);
  $data['records'] = $this->photo_model->get_subject($input, $config['per_page'], $this->uri->segment(3));
  $this->load->view('photos/multi-edit',$data);

  

}


Also if I use
Code:
$input = trim($this->input->post('subject'));
I get no results if I click a page link.
#2

[eluser]InsiteFX[/eluser]
You need to check your input to see if the value is set!
Something like this:
Code:
if (isset($this->input->post('subject', TRUE))
{
    $input = $this->input->post('subject', TRUE);
}
else
{
    // assign a default value here to $input!
    $input = 'defalt_value';
}
#3

[eluser]gazza7364[/eluser]
[quote author="InsiteFX" date="1337028323"]You need to check your input to see if the value is set!
Something like this:
Code:
if (isset($this->input->post('subject', TRUE))
{
    $input = $this->input->post('subject', TRUE);
}
else
{
    // assign a default value here to $input!
    $input = 'defalt_value';
}
[/quote]

Hi thanks for your reply, but not sure how this will work. I'm sending an input from a drop-down form which is populated from the database with unique fields from the subject column. When the form is submitted the data is populated correctly on the first page. the problem occurs when click a pagination links, say next or page2. The data is loss at this point. If I set a default input value say "Lakes" this will pull the data for lakes, which is not what I want. Also when I tried to add your code I got a server error. what I need is the $input value somehow to stay global until I use the form to send a different subject. I can tell the data is loss by the profiler, when I click on the links. I also echo the $input which shows correctly on the first page, but not when I click a link.
I also see by your code about checking to see if the $input is holding a value.


Just added the following:-

Code:
if(isset($_POST['subject'])) {
  
  $input = trim($this->input->post('subject'));
  }
  else
  {
  $input = "Lakes-2010";
  }

What happends is I send subject, which is Fun Run, the data for the Fun Run shows on the fisrt page, then every page, when I click a link shows data for "Lakes-2010" which is not what I want. What I'm after is the links to show the other Fun run data or what ever subject is submitted.
Thanks again.
#4

[eluser]cideveloper[/eluser]
You can store the search criteria in a session and then destroy it if another search is done. Something like this.

Code:
$input = $this->input->post('subject');

if($input) {
  $this->session->unset_userdata('search_subject');
  $this->session->set_userdata('search_subject', $input);
} else {
  $input = $this->session->userdata('search_subject');
}
#5

[eluser]CroNiX[/eluser]
Code:
$this->session->unset_userdata('search_subject');
$this->session->set_userdata('search_subject', $input);
would require 2 db calls. You can just use the second method to overwrite the original value, which is really the same thing as destroying it and recreating it except in one step/db call.
#6

[eluser]gazza7364[/eluser]
Thanks CroNix and cideveloper using session as suggested has solve my problem.

Thanks again, nice to see a forum where people want to help.




Theme © iAndrew 2016 - Forum software by © MyBB