user7047368 user7047368 - 1 month ago 6
MySQL Question

Displaying all the records from the database based on no-of views using codeigniter php

Hi i am trying to fetch the records from database based on no-of-views of the page.But unable to fetch all the records getting only one record.here is my code.

Controller:

function buzzling()
{
$data['records2'] = $this->blogs_model->get_all_buzzling();
$data['mainpage'] = 'buzzling';
$this->load->view('templates/template',$data);
}


Model:

function get_all_buzzling()
{
$this->db->select('B.*');
$this->db->from('blogs AS B');
$this->db->where(array('B.status'=>1));
$this->db->order_by("ne_views", "asc");
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}


View:

<div class="right">
<?php if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<h1 class="headline2"><span class="headline">Buzzing</span></h1>
<div class="clearfix float-my-children">
<img src="<?php echo base_url();?>admin/images/blogimages/thumbs/<?php echo $r->image_path;?>" width=100>
<div class="blogclasstext1"><?php echo $r->description;?></div>
</div>
<?php endforeach ; endif;?>
</div>


Sample database table

blog_id image description status ne_views
1 image.png description 1 3
2 image.png description 1 2
3 image.png description 1 4
4 image.png description 1 1

Answer

You was call $this->blogs_model->get_all_buzzling(); in another method. Please use $this->blogs_model->get_all_buzzling(); in article() method not buzzling() on your blogs controller.

<?php
function article()
{
    $data['records2'] = $this->blogs_model->get_all_buzzling();
    $data['mainpage'] = 'buzzling';
    $this->load->view('templates/template',$data);
}
?>
Comments