在我目前正在研究的一個項目中,我想從一個類的函數中獲取數據。因此我從我的主索引文件調用該函數。但是在這裏我本來想打這個電話,我得到以下錯誤現貨:調用某個函數在時間不起作用
Fatal error: Uncaught Error: Call to a member function searchKlant() on array in N:\xampp\htdocs\Sylvia project\index.php:66 Stack trace:
0 {main} thrown in N:\xampp\htdocs\Sylvia project\index.php on line 66
不過,如果我使用相同的代碼行:
$klantfor = $klant->searchKlant($_GET['k']);
在不同的點在同一代碼,我確實得到了所需的結果。 如果我調用相同的函數,但是這次它可以工作在不同的類中,這會使它更有趣。
如果有人能夠幫助我解決問題,我會很樂意。 預先感謝您
主要頁面代碼:
<?php
spl_autoload_register(function ($class_name) {
include 'custom/' . $class_name . '.class.php';
});
$klant = new Klant;
$invoice = new Invoice;
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form'])){
if($_POST['form']=="klant") {
$klant->addKlant($_POST);
} else if($_POST['form']=="invoice"){
$invoice->addInvoice($_POST);
}
}
$allklanten = $klant->getKlanten();
$allinvoice = $invoice->getInvoice();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>
<?php include("components/header.php") ?>
<div class="row">
<div class="col-md-2">
<ul class="col-sm-4 list-unstyled">
<?php
foreach($allklanten as $klant){
echo "<li><a href='?page=0&k=" . $klant['kID'] . "'>" . $klant['kVoornaam'] . " " . $klant['kAchter'] . "</a></li><br>";
}
?>
</ul>
</div>
<?php
if(isset($_GET['page'])) {
if ($_GET['page'] == 2) {
include("pages/klantadd.php");
} else if ($_GET['page'] == 3) {
include("pages/invoiceadd.php");
}
}
?>
<div class="col-md-8">
<?php
if(isset($_GET['k'])){
$klantfor = $klant->searchKlant($_GET['k']);
$invoicefor = $invoice->searchInvoice($_GET['k']);
foreach ($klantfor as $klant){
echo "<h3>" . $klant['kVoornaam'] . " " . $klant['kAchter'] . "</h3>";}
echo "<table class=\"table table-hover\">
<thead>
<tr>
<th>ID</th>
<th>Datum</th>
<th>test.class</th>
</tr>
</thead>
<tbody>
";
foreach($invoicefor as $invoice){
echo "<tr>";
echo "<td>" . $invoice['iID'] . "</td>";
echo "<td>" . $invoice['iDate'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
}
?>
</div>
</div>
</body>
,問題中的函數:
public function searchKlant($target){
$db = new Database();
$db = $db->getConn();
$statement = $db->prepare("SELECT * FROM klant WHERE kID = ?");
$statement->bind_param("i",$target);
$statement->execute();
$result = $statement->get_result();
while ($row = $result->fetch_assoc()) {
$klant[] = $row;
}
$statement->close();
$db->close();
return $klant;
}
因爲你覆蓋'$ klant'這裏:'的foreach($ allklanten爲$ klant)' – Jeff
也覆蓋在'的foreach($ klantfor爲$ klant){' – WOUNDEDStevenJones