• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help on how to loop .. displaying all data on database..

#1
[eluser]besingamk[/eluser]
hi im new on codeigniter and i cant loop this one.. please help me.
all i need is to loop the codes as long as it has data on database

heres my controler:
Code:
public function homepage()
{
  $this->load->model("get_db_home");
  $data['result'] = $this->get_db_home->getData();
  $data['title'] = "Home";
  $this->load->view("template/header", $data);
  $this->load->view("sample", $data);
  $this->load->view("template/footer");
}

and heres my model:
Code:
<?php

class Get_db_home extends CI_Model
{
function getData(){
  $query = $this->db->query("SELECT * FROM table WHERE id = id ORDER by id DESC");
  return $query->result();
}
}

heres my view:
Code:
<?php

foreach($result as $row){
$name = $row->name;
$image = $row->image;
$type = $row->type;

}


?>
// the code below is the one i want to loop

<div class="eight columns">
          <div class="row">

            <div class="four mobile-two columns">
              <img src="&lt;?php echo base_url(); ?&gt;/images/templates/&lt;?php echo $image; ?&gt;" alt="Simple and Clean Template">

              <div class="panel">
                <h5>&lt;?php echo $name; ?&gt;</h5>
                <h6 class="subheader">&lt;?php echo $type; ?&gt;<span class="right"><a href="templates/fresh-and-clean.php">Detail</a></span></h6>
              </div>
            </div>

#2
[eluser]Rowan Wilson[/eluser]
Something like this:

Code:
&lt;?php if ($result): ?&gt;

&lt;?php foreach ($result as $row): ?&gt;

<div class="eight columns">
<div class="row">
  <div class="four mobile-two columns">
   <img src="&lt;?php echo base_url(); ?&gt;/images/templates/&lt;?php $row-&gt;image; ?&gt;" alt="Simple and Clean Template">
   <div class="panel">
    <h5>&lt;?php echo $row->name; ?&gt;</h5>
    <h6 class="subheader">&lt;?php $row->type; ?&gt;<span class="right"><a href="templates/fresh-and-clean.php">Detail</a></span></h6>
   </div>
  </div>
</div>
</div>

&lt;?php endforeach; ?&gt;

&lt;?php else: ?&gt;

<p>No result found</p>

&lt;?php endif; ?&gt;

#3
[eluser]besingamk[/eluser]
Thank you sir.. I hope thats its the answer to my problem..

#4
[eluser]besingamk[/eluser]
[quote author="Rowan Wilson" date="1357559740"]Something like this:

Code:
&lt;?php if ($result): ?&gt;

&lt;?php foreach ($result as $row): ?&gt;

<div class="eight columns">
<div class="row">
  <div class="four mobile-two columns">
   <img src="&lt;?php echo base_url(); ?&gt;/images/templates/&lt;?php $row-&gt;image; ?&gt;" alt="Simple and Clean Template">
   <div class="panel">
    <h5>&lt;?php echo $row->name; ?&gt;</h5>
    <h6 class="subheader">&lt;?php $row->type; ?&gt;<span class="right"><a href="templates/fresh-and-clean.php">Detail</a></span></h6>
   </div>
  </div>
</div>
</div>

&lt;?php endforeach; ?&gt;

&lt;?php else: ?&gt;

<p>No result found</p>

&lt;?php endif; ?&gt;
[/quote]

sir how about if i want 5 items only to be display.. ??

#5
[eluser]Rowan Wilson[/eluser]
Quote:sir how about if i want 5 items only to be display.. ??

In your model:

Code:
$query = $this->db->query("SELECT * FROM film WHERE film_id = film_id ORDER by film_id DESC LIMIT 5");

Have a look into Active Record though. Unless your query is massively complicated it will do all that you require:

http://ellislab.com/codeigniter/user-gui...ecord.html

That way you can pass through the limit from your controller/view. If you so wish.

#6
[eluser]besingamk[/eluser]
i am very thankful for your reply sir. it helps me a lot to my project and to understand CI more..


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.