• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql database

#1
[eluser]Captain_Fluffy_Pants[/eluser]
trying to load mysql database and its not working
controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
function Site(){
parent::_construct();
  }
    function all(){
    $this->load->model('posts');
    $data['rows'] = $this->posts->load_all();
    $this->load->view('pages/view_all', $data);
   }
}
view
Code:
<ol>
&lt;? foreach($row as $row): ?&gt;
<li>&lt;?= $row->title ?&gt;</li>
&lt;? endforeach; ?&gt;
</ol>
model
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Posts extends CI_Model {
    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function load_all() {
        $query = $this->db->get('posts');
        return $query->result();
    }
}
anyone know what i did wrong?

#2
[eluser]CroNiX[/eluser]
Yes, look at this line in your view:
Code:
&lt;? foreach($row as $row): ?&gt;
should be foreach($rows as $row)

Also, This is a php4 construct:
Code:
function Site(){
parent::_construct();
}

You should use php5 construct:

Code:
function __construct()
{
  parent::__construct();
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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