Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用函数不需要';我有时不工作_Php_Database_User Defined Functions - Fatal编程技术网

Php 调用函数不需要';我有时不工作

Php 调用函数不需要';我有时不工作,php,database,user-defined-functions,Php,Database,User Defined Functions,在我目前正在进行的一个项目中,我希望从类中的函数中获取数据。因此,我从主索引文件调用该函数。然而,在我最初想打电话的地方,我得到了以下错误: 致命错误:未捕获错误:调用成员函数searchKlant() 在N:\xampp\htdocs\Sylvia project\index.php:66堆栈跟踪中的数组上: 在第66行的N:\xampp\htdocs\Sylvia project\index.php中抛出0{main} 但是,如果我使用同一行代码: $klantfor = $klant-&g

在我目前正在进行的一个项目中,我希望从类中的函数中获取数据。因此,我从主索引文件调用该函数。然而,在我最初想打电话的地方,我得到了以下错误:

致命错误:未捕获错误:调用成员函数searchKlant() 在N:\xampp\htdocs\Sylvia project\index.php:66堆栈跟踪中的数组上:

在第66行的N:\xampp\htdocs\Sylvia project\index.php中抛出0{main} 但是,如果我使用同一行代码:

$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>

在此处覆盖类
klant
的实例
$klant

foreach($allklanten as $klant) {....
所以把它换成别的($klantItem左右)

请参见以下简单示例:

<?php

$array = Array(1,2,3);

$v="a class";
foreach($array as $v) {
    echo $v;
}

echo "outside: ".$v;
// output: 'outside: 3' - so "a class" is overwritten.

因为您覆盖了
$klant
这里:
foreach($klant为allklanten)
也覆盖了
foreach($klantfor为$klant){
老实说,我必须检查自己foreach中的$klant是否是“全局的”——但由于控制结构不是一个单独的作用域,所以它是全局的。
<?php

$array = Array(1,2,3);

$v="a class";
foreach($array as $v) {
    echo $v;
}

echo "outside: ".$v;
// output: 'outside: 3' - so "a class" is overwritten.