繁体   English   中英

从 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(); 
      
      
    }

报价.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放在get之前,以便它可以在数据库上运行,而不是最终结果。

return $this->db->order_by('offer_id', 'desc')->get('offers')->result();

在您的 Model (viewoffer.php) 中修改您的 function

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM