Welcome Guest, Not a member yet? Register   Sign In
Blog within Codeigniter
#1

[eluser]James_B[/eluser]
Hi guys im trying to create a blog page in a web-based application using php and CI and when I currently load the page I'm getting this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$Blog

Filename: controllers/blog.php

Line Number: 17


Fatal error: Call to a member function get_last_ten_entries() on a non-object in C:\xampp\htdocs\ci\web-project-jb\controllers\blog.php on line 17

I was thinking this was due to me not loading the model into the controller, but I had

Here's my code so far:
Blog controller

<?php
class Blog extends CI_Controller {

public function _construct()
{
parent::_construct();
$this->load->model('Blogmodel','Blog');

}

public function index()
{

$username = $this->session->userdata('username');

$viewData['username'] = $username;
$viewData['blogs'] = $this->Blog->get_last_ten_entries();

$this->load->view('shared/header');
$this->load->view('blog/blogtitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('blog/blogiview', $viewData);
$this->load->view('shared/footer');
}

public function post()
{
if($this->input->post('act') =='create_post')
{
$this->Blog->insert_entry();
$this->load->view('blog/posted');
}

else
{
$this->load->helper('form');// Load the form helper.

// Lets set the stuff that will be getting pushed forward...
$data = array();
$data['form_open']=form_open();
$data['form_title'] = form_input(array('name' => 'title'));
$data['form_text'] = form_textarea(array('name' => 'text'));
$data['form_hidden'] = form_hidden('act','create_post');
$data['form_submit'] = form_submit('submit','Make Post');

$this->load->view('blog/post', $data);
}
}
}

blogmodel:

<?php
class Blogmodel extends CI_Model
{
public function _construct()
{
parent::__construct();
}

public function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}

public function insert_entry()
{
$this->title = $this->input->post('title');
$this->body = $this->input->post('text');
$this->date = time();

$this->db->insert('entries',$this);
}


}

and my blogview:

<?
foreach($blogs AS $viewData)
{
$id = $viewData->id;
$title = $viewData->title;
$body = $viewData->body;
?>

<h1>&lt;?=$title?&gt;</h1>
&lt;?=$body?&gt;
<hr>

&lt;?
}
?&gt;


Thanks guys, im just so frustrated at the moment...
#2

[eluser]samitrimal[/eluser]
@James_B,
In line no 17 you are calling wrong model name when you are calling the function.The model named Blog is not loaded . Load the blog model
Code:
$this->load->model('Blog');
in before calling it in line 17.
#3

[eluser]James_B[/eluser]
Thanks for getting back to me, that actually worked, which i think is stranger cosidering I had already mentioned the model elsewhere:

$this->load->model('Blogmodel','Blog');

anyway, now when I try to post a blog message it shows up as I wanted it but only i enter the next blog after that

Why do you think this could be?

Here is my controller currently:

&lt;?php
class Blog extends CI_Controller {

public function _construct()
{
parent::_construct();
$this->load->model('Blogmodel','Blog');

}

public function index()
{

$username = $this->session->userdata('username');

$viewData['username'] = $username;

$this->load->model('Blogmodel');
$viewData['blogs'] = $this->Blogmodel->get_last_ten_entries();

$this->load->view('shared/header');
$this->load->view('blog/blogtitle', $viewData);
$this->load->view('shared/nav');
$this->load->helper('form');// Load the form helper.

// Lets set the stuff that will be getting pushed forward...
$data = array();
$data['form_open']=form_open();
$data['form_title'] = form_input(array('name' => 'title'));
$data['form_text'] = form_textarea(array('name' => 'text'));
$data['form_hidden'] = form_hidden('act','create_post');
$data['form_submit'] = form_submit('submit','Make Post');

$this->load->view('blog/blogview', $data);
$this->load->view('blog/post', $data);


if($this->input->post('act') =='create_post')
{
$this->Blogmodel->insert_entry();
}


$this->load->view('shared/footer');
}



}

#4

[eluser]Ewout[/eluser]
Do you mean that the last inserted article doesn't show immediately?
Your insert processing happens after your list fetch, maybe that is the 'problem'?

p.s.: try to use tags
#5

[eluser]Aken[/eluser]
Your constructor is not firing because you've "spelled" it wrong. There should be TWO underscores before it, not one.

Code:
public function __construct()
{
    parent::__construct();
    $this->load->model('Blogmodel', 'Blog');
}
#6

[eluser]James_B[/eluser]
Thanks guys I put the insert post statement before the get last ten entries list function statment and it worked!!

what kind of session parameters could I use if I wanted the same blog to not show up on every users blog page? I know I could use a $username variable for starters but I have tried incorporating a username field into my database but no avail as I keep generating a db related error




Theme © iAndrew 2016 - Forum software by © MyBB