Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:58:56+00:00 2026-06-10T04:58:56+00:00

I am currently following tutorials on viewing data from the database using the Framework

  • 0

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>' ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:58:57+00:00Added an answer on June 10, 2026 at 4:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking around and following each tutorials,there is one which stands out.
I am using currently the following code to populate a combobox: combobox.DataSource = datatable;
I'm pretty new to Android Development, i'm following some Youtube tutorials currently. I'd like
I am fairly new to XCODE development currently following several tutorials. Bumped into an
I am new to algorithms and am currently studying using you-tube video tutorials/lectures and
I am currently following along with http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model to get a grasp of MVC 4.
I'm currently following a tutorial series for a Tile Engine which uses XML files
I am currently using Eclipse to code a new plugin. I have been following
I'm currently following a tutorial in a book, and it instructs to create a
I'm currently following the third tutorial listed here: here where I'm trying to compile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.