4

I am currently following tutorials on viewing data from the database using the Framework Codeigniter. There are various ways in which I've learnt. Is there is a more realiable way- either displaying as an array or using 'foreach' in the view file? Any opinions would be helpful.

This is my code using the two methods:

Method 1 Model:

function getArticle(){
    $this->db->select('*');
    $this->db->from('test');
    $this->db->where('author','David');
    $this->db->order_by('id', 'DESC');
    $query=$this->db->get();

    if($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }$query->free_result();
}

}

Method 1 View file:

 <?php foreach($article as $row){ ?>
        <h3><?php echo $row->title;  ?></h3>
        <p><?php echo $row->content;  ?></p>
        <p><?php echo $row->author;  ?></p>
        <p><?php echo $row->date; ?></p>
<?php } ?>

Method 2 Model: class News_model extends CI_Model {

function getArticle(){
    $this->db->select('*');
    $this->db->from('test');
    $this->db->where('author', 'David');
    $this->db->order_by('id', 'DESC');
    $query=$this->db->get();

    if ($query->num_rows()>0) { 
        return $query->row_array();
    }
    $query->free_result();
}

Method 2 View file:

    <?php echo '<h3>' .$article['title'].'</h3>' ?>
    <?php echo '<p>' .$article['content']. '</p>' ?>
    <?php echo '<p>' .$article['author']. '</p>' ?>
    <?php echo '<p>'. $article['date']. '</p>' ?>
Shimsham84
  • 227
  • 2
  • 12
  • 29

1 Answers1

15

I would do it like that:

Model

function getArticle() {
    $this->db->select('*');
    $this->db->from('test');
    $this->db->where('author','David');
    $this->db->order_by('id', 'DESC');
    return $this->db->get()->result();
}

}

Controller

function get_tests() {
    $data = array(
        'tests' => $this->mymodel->getArticle()
    }
    $this->load->view('myview', $data);
}

View

<table>
    <?php foreach($tests as $test) { ?>
    <tr>
        <td><?php echo $test->title;?></td>
        <td><?php echo $test->content;?></td>
        <td><?php echo $test->author;?></td>
        <td><?php echo $test->date;?></td>
    </tr>
</table>

If you wish to work with arrays rather than objects, in your model change line

return $this->db->get()->result();

to

return $this->db->get()->result_array();

and in views echo like

<td><?php echo $test['title'];?></td>

P.S.

In your code you use $query->free_result(); but it doesn't even run because when you use keyword return everything after that is not even parsed. It's not necessary to free results anyway.

P.S.2.

You use if($query->num_rows() > 0) { but don't have the else part, it means that it's not necessary too. If you'll return no lines to your foreach statement in the view, you won't get any errors.

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
  • Thanks so much, this is so much cleaner, understandable and from this will probably use arrays- but handy to know how to accomplish using the object way so will snippet this! Much appreciated. – Shimsham84 Aug 24 '12 at 12:50
  • I would really recommend using objects, especially if you're new to OOP in PHP and PHP frameworks. When I started I used arrays as I thought it is more simple, but now I understand that it was an illusion and objects allow you to do magic. In CodeIgniter you don't call static methods of objects, that allows you to get started more easily, but that's for you to decide! – Sergey Telshevsky Aug 24 '12 at 13:39