未定义的变量和无效的参数

时间:2014-10-17 19:00:07

标签: php html arrays

我试图将我的客户数组链接到我的表格,以便在表格中正确输出,但我收到了这些错误。注意:未定义的变量:C:\ wamp \ www \ son第67行中的客户  警告:为第67行的C:\ wp中的foreach()提供的参数无效

我是否认为这完全错了,这似乎是一项简单的任务,但我已经迷失了。

my cu

2 个答案:

答案 0 :(得分:0)

方法readCustomers不正确。尝试这样的事情:

class Customers{
    public function readCustomers()
    {
    $handle = fopen("inputfile.txt", "r") or die("Failed to create file");
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            $pieces_of_line = explode(',', $line);
            //do whatever you like with the pieces
        }
    } else {
        // error opening the file.
    } 
    fclose($handle);

    }
 }

尝试与readOrders

类似的内容

答案 1 :(得分:0)

首先,您需要从班级的阅读功能中返回一些内容。

在这种情况下,它们必须是static

class Customers{

    public static function readCustomers()
    {
        $array_of_lines = explode("\n", file_get_contents('customers.txt'));
        $pieces_of_line = [];
        foreach($array_of_lines as $line) {
            $pieces_of_line[] = explode(',', $line);
        } 
        return $pieces_of_line;
    }
 }

使用Orders类执行相同的操作。

然后你循环通过$customers,你必须先这样做:

$customers= Customers::readCustomers();
foreach ($customers as $cust) {
            echo '<tr>';
            echo '<td><a href="chapter10-project03.php?customer="'.$cust[0].'></a></td>';
            echo '<td>' .$cust[1] . '</td>';
            echo '<td>' .$cust[2] .' ' $cust[3] . '</td>';
            echo '<td>' .$cust[5] . '</td>';
            echo '<td>' .$cust[4] .'</td>';   
            echo '<td>' .$cust[6] .'</td>';  
            echo '<td>' .$cust[7] . '</td>';
            echo '<td>' .$cust[8] . '</td>';
            echo '<td>' .$cust[9] . '</td>';
            echo '</tr>    ';    
        }