I have a website in codeigniter. In the main page i am fetching all data from database. I want to fetch my database in a decending order according to id. I think its a small thing but i encounterd some error in my code. I have tried some similar questions but i got this type error.
what i have tried
user.php(controller)
public function viewmainoffer()
{
$this->load->model('viewoffer');
$employee = $this->viewoffer->veiwoffersmodel();
$this->load->view('user/offer', ['employee'=>$employee]);
}
viewoffer.php(model)
public function veiwoffersmodel() {
return $this->db->get('offers')->order_by('offer_id', 'desc')->result();
}
offer.php(view)
<?php if(count($employee)): ?>
<?php foreach ($employee as $row): ?>
<div class="container mainofferdiv align-left p-3 mt-4">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4 imagedivision ">
<img style="border-radius: 10px;" height="100" width="150" class="mt-5 offerimage" src="<?= $row->image ?>">
<br>
</div>
<div class="col-lg-8 col-md-8 col-sm-8 d-flex flex-column justify-content-between">
<h3 style="color: red;" class="mt-3"><?= $row->heading ?></h3>
<p style="word-wrap: break-word;"><?php echo nl2br($row->details); ?></p>
<div>
<a class="btn btn-warning p-3 font-weight-bold" href="<?= $row->link ?>">Collect cashback</a>
</button>
</div>
<div>
<br>
<button class="btn btn-success"> Verified <i class="fa fa-check-square" aria-hidden="true"></i></button>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif; ?>
the error i have got
An uncaught Exception was encountered
Type: Error
Message: Call to undefined method CI_DB_mysqli_result::order_by()
Filename: C:\xampp\htdocs\blog\application\models\viewoffer.php
Line Number: 25
Backtrace:
File: C:\xampp\htdocs\blog\application\controllers\user.php
Line: 122
Function: veiwoffersmodel
File: C:\xampp\htdocs\blog\index.php
Line: 315
Function: require_once
Thankyou
You need switch the get and order by. Get returns the results, so you need to place the order_by
before the get
so it can be run on the database, instead of the final results.
return $this->db->order_by('offer_id', 'desc')->get('offers')->result();