gpt4 book ai didi

php - CodeIgniter:PHP 错误:Table.php 中的 foreach() 中的参数无效:第 198 行

转载 作者:可可西里 更新时间:2023-11-01 01:04:33 25 4
gpt4 key购买 nike

我已经对这个错误进行了非常彻底的搜索,虽然我找到了很多关于“确保您正在传递数组或 SQL 查询”的一般性答案,但我认为我可能需要每个 CodeIgniter 库的更具体的帮助。

我有一个 View ,它只是应该创建一个查询(或数组)并生成一个表。

(最终,查询将从模型中发生,表生成将成为我的 Controller 中的一个函数,因此我可以在其他 View 中重用它,但我只是从基本的屏幕回显开始)

View 是这样设置的:

<div class="span4 well well-small">
<h4>Requests:</h4>
<?php
$this->table->set_heading('ID', 'Request', 'User', 'Date');

$query = array('id'=>'123', 'request'=>'FARTS', 'user'=>'Steve', 'date'=>'Today'); //$this->db->select('user_id', 'password', 'user_name', 'company_id')->from('users');
echo $this->table->generate($query);
?>
<h4>In this div:</h4>
<p>we will display some information about recent requests. It will show a small table with Request ID, Request Name, and Date Requested headers.</p>
</div>

这将打印一个包含 4 个 PHP 错误框的框,内容为:

遇到 PHP 错误

严重性:警告

消息:为 foreach() 提供的参数无效

文件名:libraries/Table.php

行号:198

当我检查 Table.php 库中的第 198 行时,它需要一个数组(关联或非关联),或来自数据库的查询。我试图传递一个非关联数组、一个关联数组和一个数据库查询,所有这些都会产生同样的错误。

而且,您不必翻阅 CI,这里是抛出错误的函数:

function _prep_args($args)
{
// If there is no $args[0], skip this and treat as an associative array
// This can happen if there is only a single key, for example this is passed to table->generate
// array(array('foo'=>'bar'))
if (isset($args[0]) AND (count($args) == 1 && is_array($args[0])))
{
// args sent as indexed array
if ( ! isset($args[0]['data']))
{
foreach ($args[0] as $key => $val)
{
if (is_array($val) && isset($val['data']))
{
$args[$key] = $val;
}
else
{
$args[$key] = array('data' => $val);
}
}
}
}
else
{
foreach ($args as $key => $val)
{
if ( ! is_array($val))
{
$args[$key] = array('data' => $val);
}
}
}

return $args;
}

最佳答案

将您的 $query 变量更改为如下所示:

$query = array(array('id'=>'123', 'request'=>'FARTS', 'user'=>'Steve', 'date'=>'Today'));

该方法需要一个数组数组,即使没有特定的键=>值对也是如此。

$query = array(array('123','FARTS','Steve', 'Today'));

本质上,当您使用 CI 的数据库库时,这也是您的数据库查询应该返回的内容。

就个人而言,除非您打算直接使用来自数据库查询的数据,否则我会使用:

$this->table->add_row('123','FARTS','Steve', 'Today');
-OR-
$this->table->add_row(array('ID'=>'hello','class'=>'Today','data'=>'Displayed Text'));

就您的数据库查询遇到相同的错误而言,我会三次检查您实际从所述查询中收到的输出类型。

希望对你有帮助

关于php - CodeIgniter:PHP 错误:Table.php 中的 foreach() 中的参数无效:第 198 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14241976/

25 4 0