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>' ?>
4
  • Considering query returns only single record, I will prefer the second method. Commented Aug 24, 2012 at 11:10
  • Can't understand why are you looping in your first method's model through result set and not returning it? Commented Aug 24, 2012 at 11:24
  • @Vlakarados,when your saying in my first method i am looping and not returning it- can you describe how i would return this please? Apologies as i am new to the framework and wasnt sure how ti display from the view. Commented Aug 24, 2012 at 11:40
  • I will post it as an answer in a moment then Commented Aug 24, 2012 at 11:47

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.