所以我正在為我的 Web 應用程式創建輪播,我正在使用新的 CodeIgniter 4。到目前為止,我已經成功地上傳了影像,但是我似乎無法獲取影像以在前端顯示它。它給了我以下錯誤-嘗試在 bool 上讀取屬性“bannerimg”。
編輯- 我正在嘗試從資料庫中獲取所有影像(目前存盤了 4 張影像)
我的資料庫表
輪播資料庫表 - 請也檢查此影像!
這是下面的代碼
在 AdminModel 中獲取影像的函式
//This functions gets the images from db
public function getBanner() {
$builder = $this->db->table('tblbanner');
$result = $builder->get();
if(count($result->getResultArray()) == 1) {
return $result->getRow();
} else {
return false;
}
}
管理員控制器
public function banners()
{
if(!session()->has('logged_staff')) {
return redirect()->to(base_url(). "/team");
}
$data = [];
$data['validation'] = null;
$suid = session()->get('logged_staff');
$data['getad'] = $this->adminModel->getBanner(); // This line of fetches the images from database.
$data['staffdata'] = $this->adminModel->getLoggedStaffData($suid);
echo view("team/Templates/header_panel");
echo view("team/navigation", $data);
echo view("team/sidebar", $data);
echo view("team/banners", $data);
echo view("team/Templates/footer_panel");
}
查看檔案
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-striped" id="jsdata">
<thead>
<tr>
<th class="text-center">Edit Image</th>
<th class="text-center">Ad Image</th>
<th class="text-center">Upload Date</th>
<th class="text-center">Delete Image</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center"><a class="btn bg-success"><i class="fas fa-edit"></i></a></td>
<td class="text-center">
**// The image fetched from database is displayed here //**
<?php if($getad->bannerimg != '') : ?>
<img class="profile-user-img img-fluid" src="<?= $getad->bannerimg; ?>" alt="Ad">
<?php else : ?>
<img class="profile-user-img img-fluid" src="<?= base_url(); ?>/public/assets/img/no-image.png" alt="Ad">
<?php endif; ?>
**// The image fetched from database is displayed here //**
</td>
<td class="text-center"><?= $getad->uploaded; ?></td>
<td class="text-center"><a class="btn bg-danger"><i class="fas fa-trash"></i></a></td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- /.card-body -->
</div>
uj5u.com熱心網友回復:
如果該表中有多個影像,則將回傳false:
if(count($result->getResultArray()) == 1) {
return $result->getRow();
} else {
return false;
}
所以$data['getad']和中觀$getad會false。此外,您獲取一個陣列,然后嘗試將其作為物件訪問$getad->bannerimg。
因為你只想要一行,所以去掉if陳述句,只return需要一個物件:
return $result->getRow();
然后在您的模板中,只需:
<?php if(!empty($getad->bannerimg)) : ?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453537.html
標籤:php 代码点火器 网络应用 codeigniter-4
上一篇:codeigniter4mysql查詢distinct或groupby
下一篇:使用count_all_results或get_compiled_select和$this->db->get('table')在查詢中列出表兩次?
