Welcome Guest, Not a member yet? Register   Sign In
What am I doing wrong
#1

[eluser]user666[/eluser]
Hi guys,

I'm developing a website and I'm struggling with a very strange problem.
I can't break out the function and directory structure.

Let me explain:
I call a function like this in my edit view
Code:
echo('<a href="' . base_url() . 'edit/edit_promo/' . $id[$x] . '">Remove</a>');

This is the called function in the edit controller

Code:
function edit_promo($id){
        $this->load->model('promo');
        
        $q = $this->promo->get_promo($id);
        foreach ($q as $row){
            $data['id'] = $row->id;
            $data['title'] = $row->title;
            $data['text'] = $row->text;
        }
        $this->load->view('edit');
    }

Everything works fine except loading the view, it loads the edit view but it remains in the directory of the function www.example.com/edit/edit_promo instead of www.example.com/edit

this is just one example but it happens in the whole site.
Am I looking over a very simple configuration problem or is there a bigger problem.

Many thanks in advance,
Erwin
#2

[eluser]bretticus[/eluser]
This is not a problem. This is how things are expected to work.

Your URL at any given time is NOT indicative of which view your are loading, but which controller you are calling (which is why C stands for Controller in MVC.) Perhaps you have this just backwards. Instead, why not name your controller a noun and any method a verb (this is the recommended manner, and if you think about it for a moment, it makes sense.) In other words, your controller should represent a thing like songs, for example. The method that edits a song should just be edit. So your URL would be songs/edit. The view is COMPLETELY unbound from the URL. Your view naming can be completely arbitrary. You should, however, name it accordingly to match so that you can organize which views you call from your controllers (many people like to append _view to view files for example.)

I don't quite understand your code.

Code:
$q = $this->promo->get_promo($id);
        foreach ($q as $row){
            $data['id'] = $row->id;
            $data['title'] = $row->title;
            $data['text'] = $row->text;
        }
        $this->load->view('edit');

I have to assume that get_promo returns an array. But do you really want to reset your $data associative elements each time (you are just changing the value of $data['id], etc with each pass of your loop?)

You could probably pass the whole array to the view and do the loop there if it spits out a table/list, etc. (as I suspect.) But then you need to pass your array to the view (which you are not doing.)
#3

[eluser]user666[/eluser]
Ok, I can follow you, but apart from naming conventions en the code inside the function.
How come when I click the link www.example.com/edit/edit_promo/12
it executes the code in the edit controller, the edit_promo function with 12 as an argument, but at the end it shows the correct view but for some reason it doesn't executes the constructor or index function for that view. And the url in the browser is still www.example.com/edit/edit_promo/12 in stead of www.example.com/edit what i thought is should be.
#4

[eluser]bretticus[/eluser]
[quote author="user666" date="1266100246"]...it shows the correct view but for some reason it doesn't executes the constructor or index function for that view. And the url in the browser is still www.example.com/edit/edit_promo/12 in stead of www.example.com/edit what i thought is should be.[/quote]

Views are not objects in CI. There are no constructors to be called in views. Calling a view will not redirect your URL either. This is a fundamental of MVC also. This is not a CI-only convention. In other words the controller is accessed through the URL (convention over configuration.)
#5

[eluser]SitesByJoe[/eluser]
I see that your not passing the $data to your view which will bring everything to a grinding halt.

Normally I pass my data object ($this->db->get()) to my view as one of the values in my $data array - sort of like:

Controller:

Code:
$data['query'] = $this->db->get('table_name');
$this->load->view('my_view', $data);

Then in my views, I do my classic:

Code:
if ($query->num_rows() > 0)
{
    foreach ($query->result() as $row)
    {
        echo $row->something;
        echo $row->something_else;
    }
}

What you're doing is:

1. Pulling data object into your controller
2. Putting the data object into an array
3. Sending the array to your view

You can save yourself a step by simply passing the data object. You can use a result_array() if you like working with arrays rather than an object.
#6

[eluser]user666[/eluser]
Ok, I got it to work, seems it was indeed a problem with the way i thought MVC is working.
Thanks a lot guys




Theme © iAndrew 2016 - Forum software by © MyBB