Welcome Guest, Not a member yet? Register   Sign In
detail page problem, does not exists
#1

[eluser]Michal1[/eluser]
Hi guys I have a little problem with controllers in my project. I am trying to learn Code Igniter as much as possible so I have decided to create a little cms.

However there is a problem in detail page when than it says it does not exists.

Here is my code:

This is my siteview (which is the mainpage view)
Code:
<?php if (isset($games)): foreach ($games as $row) : ?>

<H3> &lt;?php echo $row->title; ?&gt; </H3>

<p> &lt;?php echo $row->text; ?&gt; </p>

<p> &lt;?php echo anchor('detail/'.$row->id,'Read'); ?&gt;

&lt;?php endforeach; ?&gt;

&lt;?php endif; ?&gt;

Of course I have a controller created for this but there is no problem with that.

The problem is with the line

Code:
&lt;?php echo anchor('detail/'.$row->id,'Run'); ?&gt;

when I click on some I get for example:
Code:
http://127.0.0.1:8888/novy/index.php/detail/27
based on the id of each article of course.

So then I have a "detai" controller and inside of it:

Code:
class Detail extends Controller
    {
        function index()
            {
                $data=array();
                // get certain article
                if ($query=$this->detail_model->get_game_db())
                    {
                    $data['games']=$query;    
                    }
                $this->load->view('detail_view',$data);
            }
    }

And then I have a detail_view and inside:

Code:
&lt;?php if (isset($game)): foreach ($game as $row) : ?&gt;

<H3> &lt;?php echo $row->title; ?&gt; </H3>

<p> &lt;?php echo $row->text; ?&gt; </p>

&lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;

However when I click on some anchor in site_view I get 404 Page Not Found

The page you requested was not found.

Why is that? it seems everything is right.

Thank you

Michael
#2

[eluser]danmontgomery[/eluser]
The index() method is only called when the only parameter CI has is the controller (or it has no parameter, and determines the controller from the default_controller set in routes.php).

If you want to call the index() method and pass it parameters, you have to explicitly tell CI this:

Code:
&lt;?php echo anchor('detail/index/'.$row->id,'Run'); ?&gt;

Naturally, you probably don't want the /index/ in your URL, which is what routes are for. So, open up config/routes.php and add:

Code:
$route['detail/(:num)'] = 'detail/index/$1';

Voila Smile
#3

[eluser]Michal1[/eluser]
Oh thank you it works now. But I still do not get why it has to be like that. Could you please explain it little bit more easier to me? I would like to understand it.

Because I tried to delete detail controller and instead of that I created a function "detail" in the site controller.

so it then was like:

Code:
class Site extends Controller
    {
        
    
        function index()
                {
                $data=array();
                if ($query=$this->site_model->get_games_db())
                {
                    $data['games']=$query;
                }    
                $this->load->view('site_view',$data);
                }

function detail()
            {
                $data=array();
                
                if ($query=$this->detail_model->get_game_db())
                    {
                    $data['games']=$query;    
                    }
                $this->load->view('detail_view',$data);
            }
    }

and then when I had in my site_view this:

Code:
&lt;?php echo anchor('detail/'.$row->id,'Run'); ?&gt;
it worked.


Thank you
#5

[eluser]Michal1[/eluser]
edit: Oh I got it now thanks.




Theme © iAndrew 2016 - Forum software by © MyBB