ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

New to CodeIgnitor and Php :Need Help in fetching data in array

October 03, 2013 12:43pm

Subscribe [1]
  • #1 / Oct 03, 2013 12:43pm

    Nidheesh

    4 posts

    Hello am new in php and codeigniter,so i need help in completing my simple blog site.It contains only one table contains three field ( content ,title and date).I want to fetch data from controller to view using foreach loop.But it shows error.

    Model Code

    <?php
    class Blogger_model extends CI_Model{
        
        public $title   = '';
        public $content = '';
        public $date    = '';
        
        function Blogger()
        {
            parent::__construct();
            
            
        }
         function get_last_ten_entries()
         {
            
            $query = $this->db->query('SELECT title,content,date from tbl_Blogger');
            return $query->result();
            
         }
    }
    ?>

    Controller Code

    <?php
    class Blog extends CI_Controller {
    
     function index()
     {
        $this->load->model('Blogger_model');
          // $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
    
    
      //$data['title'] = "My Real Title";
      //$data['heading'] = "My Real Heading";
           
            $data['query'] = $this->Blogger_model->get_last_ten_entries();
           
            
      $this->load->view('Blog', $data);
            
            
     }
    }
    ?>

    View Code

    <!DOCTYPE HTML>
    <head>
     <meta http-equiv="content-type" c />
     <meta name="author" c />
    
     <title>Title</title>
    </head>
    
    <body>
    
    <h1>Content From Controller</h1>
    
    <h3>My Todo List</h3>
    <p><?<br />
    print_r($query);</p>
    
    <p>?><br />
    <div><strong>My Places</strong></div><br />
    <ul><br />
    <? foreach ($query->result() as $row):?><br />
    <li><?=$row->id?></li><br />
    <? endforeach;?><br />
    </ul> <br />
    </body><br />
    </html>

    Output

    Content From Controller
    
    My Todo List
    
    Array ( [0] => stdClass Object ( [title] => A Greate Blogger [content] => The Story of a real Blogger [date] => 2013-10-01 ) [1] => stdClass Object ( [title] => The Bloody Writer [content] => The Writer kills the people without any suggestion [date] => 2013-10-02 ) )
    My Places

    Error

    ( ! ) Fatal error: Call to a member function result() on a non-object in D:\wamp\www\samplesite\application\views\Blog.php on line 20
    Call Stack
    # Time Memory Function Location
    1 0.0000 149960 {main}( ) ..\index.php:0
    2 0.0000 186016 require_once( 'D:\wamp\www\samplesite\system\core\CodeIgniter.php' ) ..\index.php:202
    3 0.0938 1274008 call_user_func_array ( ) ..\CodeIgniter.php:359
    4 0.0938 1274056 Blog->index( ) ..\CodeIgniter.php:359
    5 0.0938 1330120 CI_Loader->view( ) ..\Blog.php:16
    6 0.0938 1330480 CI_Loader->_ci_load( ) ..\Loader.php:419
    7 0.1094 1352648 include( 'D:\wamp\www\samplesite\application\views\Blog.php' ) ..\Loader.php:833


    How to Solve This?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases