Welcome Guest, Not a member yet? Register   Sign In
Paginating Search Results - 404 Error
#11

[eluser]mpar612[/eluser]
It works both ways, with and without index.php.

Thanks!
#12

[eluser]TheFuzzy0ne[/eluser]
Oh wait... Sorry, it's just occurred to me what's going on since I've taken the time to actually think about it.

CodeIgniter will be looking for a method that matches the page number (which of course you can't have in PHP), so here are my top three solutions:

1) Use _remap() in your controller
Code:
function _remap()
{
    $this->index($this->uri->rsegment(2));
}

2) Add a route to redirect all requests for that controller to the index method - $config['basicidx/?(:numSmile'] = "basicidx/index/$1"; (can't vouch for it as I haven't tried it)

3) Call the index method from your URL, followed by the page number - http://www.domain.com/basicidx/index/1

EDIT: You could also do something like this:

Code:
class Basicidx extends Controller {
    
    function index()
    {
        $this->page(1);
    }
    
    function page($page_num=0)
    {
    
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->database();
        
        $this->form_validation->set_rules('city', 'City', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('minPrice', 'Minimum Price', 'trim|integer|xss_clean');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->library('pagination');
            
            $config['base_url'] = site_url('basicidx');
            $config['total_rows'] = $this->db->count_all_results('residential');
            $config['per_page'] = '5';
            $config['uri_segment'] = 2;
            
            $this->pagination->initialize($config);
            
            //load the model and get results
            $this->load->model('basicidx_model');
            $data['query'] = $this->basicidx_model->getIdx($config['per_page'],$this->uri->segment(2));
            
            $this->load->view('formsuccess', $data);
        }
    }
}
#13

[eluser]richwalkup[/eluser]
It's because your pagination links are wrong. The number 5 can not be the name of your controller method so you should be trying to access /basicidx/index/5. Did you set the base_url property?

Code:
$config['base_url'] = base_url() . 'basicidx/index';
#14

[eluser]mpar612[/eluser]
Thanks TheFuzzy0ne and richwalkup!

It still doesn't work. I tried all of the suggestions and the first page of results returns properly, now rather than getting a 404 error after clicking on the pagination links, those links take me to the myform view.

Could this have something to do with the form validation in my controller maybe?
#15

[eluser]TheFuzzy0ne[/eluser]
_remap() will work for sure. So long as CodeIgniter can find the controller file, the _remap() method will be called on each request. Please make sure you have removed the route I suggested, and then please post the code you have with the remap method in place.
#16

[eluser]mpar612[/eluser]
Here is my code with the _remap() function included.

Thanks!

Code:
<?php

class Basicidx extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->database();
        
        $this->form_validation->set_rules('city', 'City', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('minPrice', 'Minimum Price', 'trim|integer|xss_clean');

                
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            
            $this->load->library('pagination');
            
            $config['base_url'] = site_url('basicidx');
            $config['total_rows'] = $this->db->count_all_results('residential');
            $config['per_page'] = '5';
            $config['uri_segment'] = 2;

            $this->pagination->initialize($config);
            
            //load the model and get results
            $this->load->model('basicidx_model');
            $data['query'] = $this->basicidx_model->getIdx($config['per_page'],$this->uri->segment(2));
            
            $this->load->view('formsuccess', $data);
            
        }
    }
    
    function _remap()
    {
        $this->index($this->uri->rsegment(2));
    }
    
}
?>
#17

[eluser]richwalkup[/eluser]
The problem is a core flaw in your logic. Since you are using a parametrized search in order to execute your query, you also have to carry around a couple extra variables well after the initial form post. I have not tested this code obviously but this is closer to what you're looking for. You need to first check for your search parameters being sent as a GET request and then if those don't exist attempt to do the form validation.

Code:
function index($city='', $price=0) {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation', 'pagination');
    $this->load->database();
    
    if ($city == '' && $price == 0) {
        // nothing passed in URL so attempt to process form
        $this->form_validation->set_rules('city', 'City', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('minPrice', 'Minimum Price', 'trim|integer|xss_clean');
        
        if (! $this->form_validation->run())
        {
            $this->load->view('myform');
            return;
        } else {
            $city = $this->input->post('city');
            $price = $this->input->post('minPrice');
        }
    } else {
        // got here via GET method with URL parameters
        $price = (int) $price;
    }
        
    $config['base_url'] = base_url() . 'basicidx/index/' . $city . '/' . $price . '/';
    $config['total_rows'] = $this->db->count_all_results('residential');
    $config['per_page'] = '5';
    $config['uri_segment'] = 4;
    
    $this->pagination->initialize($config);
        
    //load the model and get results
    $this->load->model('basicidx_model');
    $data['query'] = $this->basicidx_model->getIdx($config['per_page'],$this->uri->segment(2));
        
    $this->load->view('formsuccess', $data);
}
#18

[eluser]mpar612[/eluser]
Does anyone have a good tutorial or working example of this? I worked with richwalkup's suggestions and it is still not working properly.

Thanks for everyone's help!
#19

[eluser]gtech[/eluser]
[url="http://ellislab.com/forums/viewthread/120038/"]http://ellislab.com/forums/viewthread/120038/[/url] only posted a few hours ago.
#20

[eluser]richwalkup[/eluser]
Had a few issues with the original code but I have actually tested this and it works.

Code:
function index($city='', $price=0) {
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('form_validation', 'pagination'));
        $this->load->database();
        
        if ($city == '' && $price == 0) {
            // nothing passed in URL so attempt to process form
            $this->form_validation->set_rules('city', 'City', 'trim|required|min_length[5]|max_length[12]|xss_clean');
            $this->form_validation->set_rules('minPrice', 'Minimum Price', 'trim|integer|xss_clean');
            
            if (! $this->form_validation->run())
            {
                $this->load->view('myform');
                return;
            } else {
                $city = $this->input->post('city');
                $price = $this->input->post('minimum');
            }
        } else {
            // got here via GET method with URL parameters
            $price = (int) $price;
        }
            
        $config['base_url'] = base_url() . 'basicidx/index/' . $city . '/' . $price . '/';
        $config['total_rows'] = $this->db->count_all_results('residential');
        $config['per_page'] = 5;
        $config['uri_segment'] =4;
        
        $this->pagination->initialize($config);
            
        //load the model and get results
        $this->load->model('basicidx_model');
        $data['query'] = $this->basicidx_model->getIdx($config['per_page'],$this->uri->segment(4));
            
        $this->load->view('formsuccess', $data);
    }




Theme © iAndrew 2016 - Forum software by © MyBB