Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] This MVC works without $this->uri->segment(n), But Why?
#1

[eluser]Juan Velandia[/eluser]
Hello Everyone I have this MVC
Model:
Code:
function get_sections() // I have a table with sections
    {
        $this->db->select('name');
        $this->db->select('idsection');  
        $query = $this->db->get('cms_section');
        return $query->result();
        
    }

    function get_articles($section) // I have a table with articles wich belongs to a certain section
    {
        $this->db->select('name');
        $this->db->select('header');
        $this->db->where('idsection',$section);
        $query = $this->db->get('cms_article');
        return $query->result();
    }

Controller
Code:
function __construct()
{
parent::__construct();
        $this->load->model('content_model');
}

function index()
{
        $data['sections'] = $this->content_model->get_sections();
        $this->load->view('view_section', $data);
       }

function article($section)
{
        $data['articles'] = $this->content_model->get_articles($section); //How does this variable is defined with the right idsection?
        $this->load->view('view_article', $data);      
}



view_section:
Code:
<?
foreach ($sections as $row):
    echo anchor('home/article/'.$row->idsection, $row->name);
    echo '<br>';
endforeach;
?&gt;


view_article:
Code:
&lt;?php foreach ($articles as $row):
    echo $row->name.'<br>';
    echo $row->header.'<br>';
endforeach; ?&gt;


How does the $section variable is defined with the right idsection? usually one should use $this->uri->segment(n) to define it as seen on the videos (http://codeigniter.com/tutorials/), but as you can see it is not necesary in this case.

Someone knows why? thanks!
#2

[eluser]solid9[/eluser]
Are you trying to dig a CMS?
If the codes is not originally written by you.
Then somewhere in the codes it is defined.

Have you checked the SESSION?
Have you checked the config?
Have you checked the library?
How about the MY_Model?
Or the model?
etc...

You should check everything...

#3

[eluser]Juan Velandia[/eluser]
Hello, all the code has been written by me from scratch, from a fresh CI download. It really works and it doesnt use anything fancy.

I just set the DB settings and place the files, and it worked.
#4

[eluser]solid9[/eluser]
you wrote it? then you don't know it?
lol that's funny.
#5

[eluser]CroNiX[/eluser]
Its because extra segments (beyond controller/method) get passed to the method automatically, and you are capturing it and using it in your method.

Code:
function article($section)
Because you are passing $section to that function, if you go to:

Code:
www.yoursite.com/controller/article/section-name
it will pass section-name to article($section) automatically, so it will work.

If you just used
Code:
function article()
and didn't automatically pass the 3rd segment to the function, then you'd have to use uri:Confusedegment(x) to access it.



#6

[eluser]CroNiX[/eluser]
The more parameters you automatically pass, they are taken in order.
Code:
public function some_function($uri_segment_3, $uri_segment_4, $uri_segment_5, etc)

does the same as
Code:
public function some_function()
{
  $uri_segment_3 = $this->uri->segment(3);
  $uri_segment_4 = $this->uri->segment(4);
  $uri_segment_5 = $this->uri->segment(5);
}
#7

[eluser]solid9[/eluser]
@CroNiX
That's make sense, I'm learning too. lol
#8

[eluser]Juan Velandia[/eluser]
Please hold your horses! I will have to read it carefully and english is not my best language...

thanks a lot!
#9

[eluser]solid9[/eluser]
That is a nice trick.
Thanks a lot again CroNix.
Cool!
#10

[eluser]Juan Velandia[/eluser]
Cronix you are very kind, one more thing:

And I have to understand that this

Code:
function article($section, $param1, $param2)
{
        $data['articles'] = $this->content_model->get_articles($section); ..
        $this->load->view('view_article', $data);      
}


is the same as this:

Code:
function article()
{
        $section =  $this->uri->segment(3);
        $param1 =  $this->uri->segment(4);
        $param1 =  $this->uri->segment(5);
    
        $data['articles'] = $this->content_model->get_articles($section);
        $this->load->view('view_article', $data);      
}


thanks a lot, I owe you a beer!




Theme © iAndrew 2016 - Forum software by © MyBB