页面显示“无可用数据”
这是我在名为 Supply-list.php 的视图文件中的代码:
<?php
if(!empty($dataLaptop)) {
foreach ($dataLaptop as $row) {
?>
<div class="group relative">
<div class="relative h-80 w-full overflow-hidden rounded-lg bg-white sm:aspect-h-1 sm:aspect-w-2 lg:aspect-h-1 lg:aspect-w-1 group-hover:opacity-75 sm:h-64">
<img src="https://tailwindui.com/img/ecommerce-images/home-page-02-edition-03.jpg" alt="Collection of four insulated travel bottles on wooden shelf." class="h-full w-full object-cover object-center">
</div>
<h3 class="mt-6 text-sm text-gray-500">
<a href="#">
<span class="absolute inset-0"></span>
<?= $row["NAMA_SUPPLY"]; ?>
</a>
</h3>
<p class="text-base font-semibold text-gray-900"><?= $row["HARGA_SUPPLY"]; ?></p>
</div>
<?php
}} else {
echo "No data available";
}
?>
这是我在名为 Controller.php 的控制器文件中的代码:
public function getDataSupplyLaptop() {
$dataLaptop = $this->model->getDataSupplyLaptop();
include("../resources/views/supply-list.php");
}
这是我在名为 Model.php 的模型文件中的代码:
public function getDataSupplyLaptop() {
require "../database/connection.php";
$query = mysqli_query($connection, "SELECT * FROM supply WHERE ID_CATEGORY = 1");
// $rows = array();
while($row = mysqli_fetch_assoc($query)) {
$hasil[] = $row;
}
return $this->hasil;
}
这是我在index.php中的代码:
<?php
include ("../app/Controller.php");
$controller = new Controller();
if (isset($_GET['login'])) {
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
session_start();
$_SESSION['name'] = $username;
$controller->authenticationLogin($username, $password);
} else {
$controller->getDataSupplyLaptop();
$controller->showDataSupply();
}
?>
如果我的源代码有任何奇怪的结构,我很抱歉,因为我仍在学习。谢谢你
while($row = mysqli_fetch_assoc($query)) {
$hasil[] = $row;
}
return $this->hasil;
您将
$row
分配给局部变量 $hasil
,但返回的是属性 $this->hasil
,这应该是一个拼写错误。
顺便说一句,使用
mysqli_fetch_assoc
有点太复杂了,您可以直接使用mysqli_fetch_all
获得整个结果。
return $this->hasil = mysqli_fetch_all($query, MYSQLI_ASSOC);