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 9122171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:02:05+00:00 2026-06-17T06:02:05+00:00

I’m trying to display query’s results in my view in different divs, I’ve spend

  • 0

I’m trying to display query’s results in my view in different divs, I’ve spend the day reading on ellislab how to display results if I have understand correctly result() function return an object, this is my model:

model

function get_post(){
    $this->db->select(*);
    $this->db->order_by('date_time', 'desc');
    $this->db->limit(2);
    $results = $this->db->get('post');

    if($results->num_rows()<1){
          return false;
    }
    else{
          return $results->result();
    }
}

And this is what my controller does:

controller

foreach($results as $res){
    $data['post_signature']     =  $res->signature;
    $data['post_title']         =  $res->title;
    $data['post_article']       =  $res->article;
    $data['post_date']          =  date('d-M-Y', strtotime($res->date_time));
    $data['post_time']      =  date('H:i', strtotime($res->date_time));
}

What i would like to show in my view is the series of results in a div and the second in another div.

<div id="1">First Result</div>
<div id="2">Second Result</div>

What I tought to do was to pass the whole query result to the view and use a foreach, like this:

<?php foreach ($results as $res)
     <div id="1">First Result</div>
     <div id="2">Second Result</div>
?>

Obviously the problem to this approach is that this foreach just reduplicate both the divs and I just want to put the first result in the first div and the second in the second div. I also tought about changing the foreach into the controller and use a for statement to save all the parameters returned into the data array, but for some reasons this seems very inefficient.

I hope you have better solutions.

  • 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-17T06:02:07+00:00Added an answer on June 17, 2026 at 6:02 am

    You should pass your $results object to the view and use a foreach there:

    // in your view (assuming you passed the $results as-is
    <?php
    foreach ($results as $res)
    {
        echo '<div id="' . $res->id . '">';
        echo $res->title . '<br />';
        echo 'Posted ' . date('d-M-Y', strtotime($res->date_time)) . ' ';
        echo 'at ' . date('H:i', strtotime($res->date_time)) . '<br />';
        echo '<p>' . $res->article . '</p>';
        echo '</div>'; 
    }
    /**
    you'll get something like the following
    <div id="1">
        My Title<br />
        Posted 14 Jan 2012 at 16:40<br />
        <p>Lorem ipsum dolor sit amet.</p>
    </div>
    <div id="2">
        Another Title<br />
        Posted 14 Jan 2012 at 16:45<br />
        <p>Lorem ipsum dolor sit amet also.</p>
    </div>
    */
    

    The idea is that $results is a collection of result objects, so when you loop through them in the controller (like your example in the question) – you’re not getting the “collection” of objects in the view. Looping through the view allows you to manage the collection (and also the display) of those results.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.