CodeIgniter Forums
mysql database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: mysql database (/thread-54625.html)



mysql database - El Forum - 09-17-2012

[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?


mysql database - El Forum - 09-17-2012

[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();
}