• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blog - error

#1
BLOG
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: blogs
Filename: views/blog.php
Line Number: 8
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\views\blog.php
Line: 8
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\controllers\Blog.php
Line: 17
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\index.php
Line: 315
Function: require_once

Fatal error: Call to a member function result() on null in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\views\blog.php on line 8
A PHP Error was encountered
Severity: Error
Message: Call to a member function result() on null
Filename: views/blog.php
Line Number: 8
Backtrace:

views/blog.php

PHP Code:
<html>


<
h1>BLOG</h1>


<?
php foreach ($blogs->result() as $row) : ?>

<li>

    <?php echo $row->date?><br>
    <?php echo $row->title?><br>
    <?php echo $row->content?><br>
    
</li>

<?php endforeach; ?>


</html> 


controllers/blog.php


PHP Code:
<?php


class Blog extends CI_Controller {

 
       public function index()
 
       {
 
               $this->load->model('blog_model');

 
               //$data['query'] = $this->blog->insert();

                
$data['title'] = $this->blog_model->select_blog();
                
$data['content'] = $this->blog_model->select_blog();
                
$data['date'] = $this->blog_model->select_blog();
                
                
$this->load->view('blog'$data);
                
                
//$this->load->view('blog');
 
       
        
}
}





?>


models/blog_model.php

PHP Code:
<?php



class Blog_model extends CI_Model {

 
       public $title;
 
       public $content;
 
       public $date;

 
       public function __construct()
 
       {
 
               // Call the CI_Model constructor
 
               parent::__construct();
                
                
$this->load->database();
 
       }

        public function 
select_blog()
        {
            
             
   $query $this->db->query('SELECT * FROM blog');

                return 
$query->result();
                
//  return  'masuk loh';
        
}
        
 
       public function insert_blog()
 
       {
 
               $this->title    $_POST['title']; // please read the below note
 
               $this->content  $_POST['content'];
 
               $this->date     time();

 
               $this->db->insert('blog'$this);
 
       }

 
       public function update_blog()
 
       {
 
               $this->title    $_POST['title'];
 
               $this->content  $_POST['content'];
 
               $this->date     time();

 
               $this->db->update('blog'$this, array('id' => $_POST['id']));
 
       }

}



?>


How to fix the above error?

File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\views\blog.php
Line: 8

Line: 8   <?php foreach ($blogs->result() as $row) : ?>


Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\controllers\Blog.php
Line: 17

Line: 17    $this->load->view('blog', $data);
" If I looks more intelligence please increase my reputation."
Reply

#2
Your model returns $query->result();
But in your view, you're trying to run the result() method again.
This won't work.
Simply do this:

PHP Code:
foreach ($blogs as $row
Reply

#3
BLOG
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: blogs
Filename: views/blog.php
Line Number: 8
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\views\blog.php
Line: 8
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\controllers\Blog.php
Line: 17
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\index.php
Line: 315
Function: require_once


A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/blog.php
Line Number: 8
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\views\blog.php
Line: 8
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\controllers\Blog.php
Line: 17
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\index.php
Line: 315
Function: require_once


views/blog.php

PHP Code:
<html>


<
h1>BLOG</h1>


<?
php foreach ($blogs as $row) : ?>

<li>

    <?php echo $row->date?><br>
    <?php echo $row->title?><br>
    <?php echo $row->content?><br>
    
</li>

<?php endforeach; ?>


</html> 

Line 8:  <?php foreach ($blogs as $row) : ?>

How to fix line 8 ?
" If I looks more intelligence please increase my reputation."
Reply

#4
Okay, it only works if define $blog like this:

<?php $blogs = $this->blog_model->select_blog(); ?>

but I wonder why do I have to repeat my controllers code?
" If I looks more intelligence please increase my reputation."
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.