当我尝试在reports-details.php
上搜索两个日期之间的所有工作显示,但当我尝试在reports-details.php
上重新加载时显示此错误警告:
在C:xampphtdocsparking中为foreach()提供的无效参数预订adminreports-details.php第52行
reports.php
含量为:
<form action="reports-details.php" method="POST">
<label>From Date</label>
<input type="date" name="start_date">
<label>To Date</label>
<input type="date" name="end_date">
<button name="search">Search</button>
</form>
back-end-reports.php
<?php
include 'config.php';
class report extends Connection{
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
}
}
}
$new_vehicle = new report();
$new_vehicle->managereport();
?>
reports-details.php
<?php
include '../back-end/back-end-reports.php';
$result = new report();
$query = $result->managereport();
?>
<?php $id = 1; foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td><a href="view-ingoing.php?id=<?php echo $row['customers_id']; ?>" class="text-dark">View</a> | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } ?>
你的问题是你的函数中的if语句
if (isset($_POST['search'])) {
当您刷新页面时,没有post数据,因此managereport不返回任何内容。因此,$query
的值为空,因此您不能在foreach循环中遍历它。
我的建议是,你的mangerreport()应该在没有post数据的情况下返回一个空数组,即
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
} else {
return array();
}
}
<?php $id = 1; foreach($query as $row) { ?>
使用foreach循环时,必须先判断数组$query
,否则会提示报告类似这样的错误Invalid argument supplied for foreach()
,可以在循环前添加判断,
<?php $id = 1; if (is_array($query) && count($query)) {
foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td><a href="view-ingoing.php?id=<?php echo $row['customers_id']; ?>" class="text-dark">View</a> | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } } ?>