Welcome Guest, Not a member yet? Register   Sign In
question regarding default view
#1

[eluser]Unknown[/eluser]
Hi,

I am writing an application that has urls like:

http://domain.com/profiles/view/3/

This would view the profile with id = 3.

I have this working. Here's a snippet of the controller: (yeah, it's dirt simple)

Code:
function view($id)
        {
        $this->load->model('Blogmodel');
        $data['query'] = $this->Blogmodel->get_by_id($id);
        $this->load->view('blogview', $data);
        }


When I load : http://domain.com/profiles/view/, I get an error, because id is missing.

How do I set the behavior when someone does not include an id?

Thanks.
#2

[eluser]xwero[/eluser]
Code:
function view()
  {
        if(!$this->uri->segment(3))
        {
          $this->load->view('someview');
        }
        else
        {
            $this->load->model('Blogmodel');
            $data['query'] = $this->Blogmodel->get_by_id($this->uri->segment(3));
            $this->load->view('blogview', $data);
        }
  }
#3

[eluser]gtech[/eluser]
you can also default id to NULL

Code:
function blog_view($id=NULL)
        {
          if ($id) {
            $this->load->model('Blogmodel');
            $data['query'] = $this->Blogmodel->get_by_id($id);
            $this->load->view('blogview', $data);
          } else {

          }
        }


be careful VIEW is a reserved word in php4 I read a post where someone had a problem with naming the functions view as it worked on php5 their locahost but not php4 where there server was hosted.
#4

[eluser]BravoAlpha[/eluser]
I've been using something like this:
Code:
function view( $id=FALSE )
{
    if (!$id)
        show_404();

    $this->load->model('Blogmodel');
    $data['query'] = $this->Blogmodel->get_by_id($id);

    // Assuming get_by_id returns false when that $id isn't found
    if (!$data['query'])
        show_404();

    // whatever else
}
#5

[eluser]Unknown[/eluser]
excellent, thanks for the suggestions and for not laughing at my question! Smile




Theme © iAndrew 2016 - Forum software by © MyBB