Welcome Guest, Not a member yet? Register   Sign In
Title after the id in the url gives 404
#1

[eluser]Dennizzz[/eluser]
Sorry for the maybe confusing title, but had to come up with something describing the situation right? :p

So i just started learning CI and like everyone i started making a blog looking at the tutorial and than adjusting it too how i code. I like my urls clean and starting with the id first so thats what im going for, something like http://example.com/blog/3434/this-is-my-...this-blog/ so started using the htacces, got it working, and started using the routes wich is working. Now whenever i call the blog using blog/3434/ it works fine, but whenever the url has the title in it, it gives me a 404 page.

Controller
Code:
class Blog extends CI_Controller {

    function index() {
       $this->load->model('blog_model');
       $this->load->helper('url');
       $this->load->helper('text');
       $data['rows'] = $this->blog_model->showBlogs();
       $this->load->view('blogs/blog_view', $data);
    }
    
    function view() {
        $this->load->model('blog_model');
        $data['rows'] = $this->blog_model->showSingleblog();
        $this->load->view('blogs/blog_singleview', $data);
    }
}

The specific model
Code:
function showSingleblog() {
        $query = 'SELECT title, author, date, content FROM blog WHERE id = ?';
        $blogid = $this->uri->segment(2);
        $q = $this->db->query($query, $blogid);

        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

And the route
Code:
$route['blog/:num'] = "blog/view";

Tried all sorts of different searching terms on the forums and wiki but could not find anything, hope someone can help me with this.

edit: Sorry posted in the wrong forums, tought i was in the right one.
#2

[eluser]InsiteFX[/eluser]
Code:
$route['blog/:num'] = "blog/view/$1";

InsiteFX
#3

[eluser]Dennizzz[/eluser]
Im sorry i should have posted it, but i did actually already tried that, with and without en closing slash, but it does not solve up the problem.
#4

[eluser]InsiteFX[/eluser]
Should be 3 not 2
Code:
// 1=controller - 2=view - 3 $1
$blogid = $this->uri->segment(3);
#5

[eluser]Dennizzz[/eluser]
No its 2, because of the route file there is no view in the url.
#6

[eluser]theprodigy[/eluser]
Quote:
Code:
class Blog extends CI_Controller {

    function index() {
       $this->load->model('blog_model');
       $this->load->helper('url');
       $this->load->helper('text');
       $data['rows'] = $this->blog_model->showBlogs();
       $this->load->view('blogs/blog_view', $data);
    }
    
    function view() {
        $this->load->model('blog_model');
        $data['rows'] = $this->blog_model->showSingleblog();
        $this->load->view('blogs/blog_singleview', $data);
    }
}

Have you tried adding a parameter to the view function (and pass it to the model) rather than trying to use url segments?
Code:
class Blog extends CI_Controller {

    function index() {
       $this->load->model('blog_model');
       $this->load->helper('url');
       $this->load->helper('text');
       $data['rows'] = $this->blog_model->showBlogs();
       $this->load->view('blogs/blog_view', $data);
    }
    
    function view($blog_id) {
        $this->load->model('blog_model');
        $data['rows'] = $this->blog_model->showSingleblog($blog_id);
        $this->load->view('blogs/blog_singleview', $data);
    }
}
#7

[eluser]Dennizzz[/eluser]
No i had not, i have tried it now but it still wont work when the title is in the url, without it works fine.
#8

[eluser]theprodigy[/eluser]
Are you using the title for anything other than 'clean urls'? Do you actually have a stub field in the db that you are querying against?
It doesn't appear to be so, which is why I am asking.

Code:
function view($blog_id, $blog_title) { //add $blog_title as parameter as well
    $this->load->model('blog_model');
    $data['rows'] = $this->blog_model->showSingleblog($blog_id);
    $this->load->view('blogs/blog_singleview', $data);
}

and maybe alter your $route
Code:
$route['blog/:num/:any'] = "blog/view";
#9

[eluser]Dennizzz[/eluser]
The title is used for displaying on the page itself, as for the clean url. I now tried altering the route line as u suggested, which did the trick on his own, much appreciated of thinking allong with me on this one! I also removed the $blog_id from the the view function, because i did not quite understand it, but does a parameter in the view function also get passed allong from the url?




Theme © iAndrew 2016 - Forum software by © MyBB