Welcome Guest, Not a member yet? Register   Sign In
noob: trying to build on "blog tutorial"
#11

[eluser]cirox![/eluser]
Managed to get everything working.

The reason why the load helper command was spitting out the helper function as text was because (to my embarassment) I hadn't enclosed the function in <?php ?> tags.

Also, the final function is slightly different:
a) It needed a where clause for it to return what I wanted. Duh!
b) It was giving me Fatal error: Using $this when not in object context which I'm not sure what to make of.
However, I tried to pass the object from the view as a parameter, like so:

Code:
get_comments_no($row->id, $this)

to the comment_helper.php:

Code:
<?php

function get_comments_no($id, $obj)
{
    $obj->db->where('entry_id',$id);
    $q = $obj->db->get('comments');
    
    return $q->num_rows();
}

?>

and this worked fine.

I also managed to get it to work without the helper, which was what I was initially trying to achieve.
I had originally defined get_comments_no() inside the controller, but when I tried viewing the page it was throwing a
Fatal error: Call to undefined function get_comments_no() ...
The reason, I figured out, was because I'd defined the function outside the index() function, and that way, the view didn't know about it.

I've got so much to learn!

This is the final code [for other newbies' benefit] ;-P

blog.php
Code:
<?php

class Blog extends Controller {

    function Blog()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
    }

    function index()
    {

        function get_comments_no($id, $obj)
        {
            $obj->db->where('entry_id',$id);
            $q = $obj->db->get('comments');
    
            return $q->num_rows();
        }

        $data['title'] = "My Blog Title";
        $data['heading'] = "My Blog Heading";
        $data['query'] = $this->db->get('entries');
        
        $this->load->view('blog_view', $data);
    }
    
    function comments()
    {
        $data['title'] = "My Comment Title";
        $data['heading'] = "My Coment Heading";
        $this->db->where('entry_id',$this->uri->segment(3));
        $data['query'] = $this->db->get('comments');
        $this->load->view('comment_view', $data);
    }

    function comment_insert()
    {
        $this->db->insert('comments',$_POST);
        redirect('blog/comments/'.$_POST['entry_id']);
    }

}

?>

blog_view.php
Code:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>&lt;?php echo $title; ?&gt;</h1>

&lt;?php foreach($query->result() as $row): ?&gt;
    <h3>&lt;?php echo $row->title ?&gt;</h3>
    <p>&lt;?php echo $row->body ?&gt;</p>
    <p>&lt;?php echo anchor('blog/comments/'.$row->id,'Comments ('.get_comments_no($row->id, $this).')'); ?&gt;</p>
    <hr />
&lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;


Messages In This Thread
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 12:05 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 12:23 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 12:43 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 12:46 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 01:17 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 01:20 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 01:30 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 01:47 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 01:53 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 01:59 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-27-2008, 04:10 PM
noob: trying to build on "blog tutorial" - by El Forum - 05-28-2008, 03:25 AM
noob: trying to build on "blog tutorial" - by El Forum - 05-29-2008, 03:27 AM
noob: trying to build on "blog tutorial" - by El Forum - 05-29-2008, 09:24 PM
noob: trying to build on "blog tutorial" - by El Forum - 07-23-2008, 04:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB