Welcome Guest, Not a member yet? Register   Sign In
Trouble templating the views
#1

[eluser]kongfjong[/eluser]
I'm trying to creatre some kind of simple template, to get an understanding of how CodeIgniter works. It's very simple so far and uses the info from the video tutorials on the site.

Right now I've got a blog.php file in the controllers folder

Code:
<?php
class Blog extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        $this->load->helper(array('url', 'form'));
    }

    function index()
    {
        $data['title'] = 'My blog title';
        $data['heading'] = 'My blog heading';
        $data['query'] = $this->db->get('entries');
        $data['content_view'] = 'blog_view';
        
        $this->load->view('template', $data);
    }
    
    function comments()
    {
        $data['title'] = 'My comment title';
        $data['heading'] = 'My comment heading';
        
        $this->db->where('entry_id', $this->uri->segment(3));
        $data['query'] = $this->db->get('comments');
        
        $data['content_view'] = 'comment_view';
        
        $this->load->view('template', $data);
    }
    
    function comment_insert()
    {
        $this->db->insert('comments', $_POST);
        
        redirect('blog/comments/'.$_POST['entry_id']);
    }
}
?>

I've then got a blog_view.php and a comment_view.php file in the view folder

blog_view.php
Code:
<h1>&lt;?php echo $heading; ?&gt;</h1>

&lt;?php
    foreach($query->result() as $row) {
        echo '<h3>'.$row->title.'</h3>';
        echo '<p>'.$row->body.'</p>';
        
        echo '<p>'.anchor('blog/comments/'.$row->id, 'Kommentar').'</p>';
        
        echo '<hr />';
    }
?&gt;

comment_view.php
Code:
<h1>&lt;?php echo $heading; ?&gt;</h1>

&lt;?php
    if($query->num_rows() > 0)
    {
        foreach($query->result() as $row) {
            echo '<p>'.$row->body.'</p>';
            echo '<p>'.$row->author.'</p>';
            
            echo '<hr />';
        }
    }
    
    echo '<p>'.anchor('blog', 'Back to blog').'</p>';
        
    echo form_open('blog/comment_insert');
    
    echo form_hidden('entry_id', $this->uri->segment(3));
?&gt;

<p>
    &lt;textarea name="body" rows="10"&gt;&lt;/textarea>
</p>

<p>
    &lt;input type="text" name="author" /&gt;
</p>

<p>
    &lt;input type="submit" value="Submit comment" /&gt;
</p>

&lt;/form&gt;

Then I've created a subfolder in views, named common, which contains these files

footer_view.php
Code:
&lt;/body&gt;
&lt;/html&gt;

header_view.php
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

And last but not least, I've created a template.php file in the view folder, which contains this code
Code:
&lt;?php
$this->load->view('common/header_view');

$this->load->view($content_view);

$this->load->view('common/footer_view');
?&gt;

Now I've got no trouble accessing http://www.footballground.dk/blog which of course uses the index function of the blog controller. But when I access http://www.footballground.dk/blog/comments/1 which should use the comments function of the blog controller, but instead actually uses the index function also.

I might be blind, but can't find the error, so hope you can help me Smile
#2

[eluser]Jaketoolson[/eluser]
something is messing up with your $config['uri_protocol'].

Navigating using index.php works:
http://www.footballground.dk/index.php/blog/comments/1
#3

[eluser]kongfjong[/eluser]
Wow thanks.

It works with both QUERY_STRING and REQUEST_URI respectively Smile Do you think, there's any downside to using one of them instead of AUTO?
#4

[eluser]Jaketoolson[/eluser]
This same problem happened to me actually, at least when I switched to CI 2.0.

Previously, using .htaccess, I 'hid' index.php from the URL and set the $config['uri_protocol'] to AUTO as well and everything worked fine.

Then when I upgraded to CI 2.0, I didn't change a thing and had the same problem as you above. Then I changed it $config['uri_protocol'] to QUERY_STRING and it worked just fine. I haven't really researched the difference between QUERY_STRING and REQUEST_URI.
#5

[eluser]kongfjong[/eluser]
Great, thanks for helpingSmile




Theme © iAndrew 2016 - Forum software by © MyBB