Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: foreach method or result array?? [Models +View]

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>' ?>
like image 863
Shimsham84 Avatar asked Dec 09 '22 21:12

Shimsham84


1 Answers

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.

like image 51
Sergey Telshevsky Avatar answered Feb 11 '23 06:02

Sergey Telshevsky