在 codeigniter 中降序从数据库中获取数据
Fetch data from database in decending in codeigniter
我在 codeigniter 中有一个网站。在主页中,我正在从数据库中获取所有数据。我想根据 id 按降序获取我的数据库。我认为这是一件小事,但我在代码中遇到了一些错误。我试过一些类似的问题,但我遇到了这种类型的错误。
我试过的
user.php(控制器)
public function viewmainoffer()
{
$this->load->model('viewoffer');
$employee = $this->viewoffer->veiwoffersmodel();
$this->load->view('user/offer', ['employee'=>$employee]);
}
viewoffer.php(型号)
public function veiwoffersmodel() {
return $this->db->get('offers')->order_by('offer_id', 'desc')->result();
}
offer.php(查看)
<?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; ?>
我遇到的错误
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
谢谢
您需要切换get和order by。得到returns的结果,所以需要把order_by
放在get
之前,这样就可以运行在数据库中,而不是最终的结果。
return $this->db->order_by('offer_id', 'desc')->get('offers')->result();
修改模型中的函数 (viewoffer.php)
public function veiwoffersmodel() {
$this->db->select('*');
$this->db->from('table name');
$this->db->order_by('offer_id', 'desc');
$query = $this->db->get();
return $query->result();
}
我在 codeigniter 中有一个网站。在主页中,我正在从数据库中获取所有数据。我想根据 id 按降序获取我的数据库。我认为这是一件小事,但我在代码中遇到了一些错误。我试过一些类似的问题,但我遇到了这种类型的错误。
我试过的
user.php(控制器)
public function viewmainoffer()
{
$this->load->model('viewoffer');
$employee = $this->viewoffer->veiwoffersmodel();
$this->load->view('user/offer', ['employee'=>$employee]);
}
viewoffer.php(型号)
public function veiwoffersmodel() {
return $this->db->get('offers')->order_by('offer_id', 'desc')->result();
}
offer.php(查看)
<?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; ?>
我遇到的错误
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
谢谢
您需要切换get和order by。得到returns的结果,所以需要把order_by
放在get
之前,这样就可以运行在数据库中,而不是最终的结果。
return $this->db->order_by('offer_id', 'desc')->get('offers')->result();
修改模型中的函数 (viewoffer.php)
public function veiwoffersmodel() {
$this->db->select('*');
$this->db->from('table name');
$this->db->order_by('offer_id', 'desc');
$query = $this->db->get();
return $query->result();
}