Welcome Guest, Not a member yet? Register   Sign In
How to Use Model For Parser?
#2

[eluser]Yorick Peterse[/eluser]
Add the following code (don't forget to change it):

Controller

Code:
<?php
class some_class extends Controller {

  //Constructor function
  function __construct() {
    parent::Controller();
  }

  //Index function
  function index() {
    //Load the comments model
    $this->load->model('comments_model');
        
    //Get the comments
    $data = array (
      'result'    => $this->comments_model->get_comments()            
    );

    //Load the view
    $this->load->view('some_view',$data);
  }

}
?>

The model

Code:
<?php
class Some_model extends Model {

  //Function for the comments
  function get_comments() {
      //Create the query
      $query = $this->db->get('comments');

      //Check to see if there are any comments at all
      if($query->num_rows() > 0) {

        //Return the results
        return $query->result();

      }
      //No comments
      else {

        show_error('No comments have been added yet');

      }
}
?>

The view
Use a foreach loop as shown in the snippet, then use $row->database_column (e.g. $row->id) to output the result.

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

<h3>&lt;?php echo $row->name; ?&gt;</h3>

&lt;?php endforeach; ?&gt;


Messages In This Thread
How to Use Model For Parser? - by El Forum - 05-02-2009, 08:36 PM
How to Use Model For Parser? - by El Forum - 05-03-2009, 03:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB