Welcome Guest, Not a member yet? Register   Sign In
Redirect
#1

[eluser]georgerobbo[/eluser]
Hello,

I have a controller which intercepts the second url segment, passes it along the model which queries the database for the ID present in the url. Everything is then passed to the view.

Works a treat, except for when you type an ID in the url which isn't in the database. For example, http://georgerobbo.dyndns.org/yellowdogy...x.php/id/1 and http://georgerobbo.dyndns.org/yellowdogy...x.php/id/2, however http://georgerobbo.dyndns.org/yellowdogy...x.php/id/3 returns a CodeIgniter error. How can I replace that error with a redirect?


My controller is as follows, where function single() is the controller for each individual post.

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['title'] = "Home";
        
        $this->load->model('Category');
        
        $data['category'] = $this->Category->list_category();
        $data['random'] = $this->Post->get_random_single();
        $data['new'] = $this->Post->latest_submissions();    
        
        foreach ($data['random'] as $a): $id = $a['ID']; endforeach;
        
        $data['tag'] = $this->Post->get_tags($id);

        $this->load->view('meta', $data);
        $this->load->view('header', $data);
        $this->load->view('home', $data);
    }
    
    function single()
    {
        $data['title'] = "Archive";
        
        $permalink = $this->uri->segment(2);
        $data['current'] = $this->Post->the_post($permalink);
        
        foreach ($data['current'] as $a): $id = $a['ID']; endforeach;
        
        $data['tag'] = $this->Post->get_tags($id);
        
        $this->load->view('meta', $data);
        $this->load->view('header', $data);
        $this->load->view('single', $data);
    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
#2

[eluser]rogierb[/eluser]
Check if you have any result from the database:

Code:
if($data['current']->num_rows()>0)
{
  // do the foreach
}
else redirect("somewhere");




Theme © iAndrew 2016 - Forum software by © MyBB