簡體   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