Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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 为每个读取数据库提供了未定义的变量Codeigniter和无效参数_Php_Codeigniter_Foreach - Fatal编程技术网

Php 为每个读取数据库提供了未定义的变量Codeigniter和无效参数

Php 为每个读取数据库提供了未定义的变量Codeigniter和无效参数,php,codeigniter,foreach,Php,Codeigniter,Foreach,我按照从表中获取整个记录,并将它们加载到html表中。这就是模型 private $namatabel; public function __construct() { parent::__construct(); $namatabel='ms_kategori_material'; } function read() { $sql = $this->db->get($this->namatabel); if($sql-&g

我按照从表中获取整个记录,并将它们加载到html表中。这就是模型

private $namatabel;

public function __construct() {
    parent::__construct();
    $namatabel='ms_kategori_material';
}

function read()
{        
   $sql = $this->db->get($this->namatabel);  

   if($sql->num_rows() > 0)
    {     
        foreach($sql->result() as $row)
        {  
            $data[] = $row;  
        }     
        return $data;  
    } 
    else 
    {  
        return null;  
    }  

}
然后在控制器上使用
read()
功能

public function __construct() {
    parent::__construct();
    $this->load->model('m_kategorimaterial');
}

function index()
{
    $data['c_row'] = $this->m_kategorimaterial->read();
//pass the c_row into the views  
    $this->load->view('v/vkategorimaterial', $data);  
}
在视图上显示它们的步骤

<?php  
$no = 1;   
foreach ($c_row as $row) { ?>  
    <tr id="row">  
        <td id="no"><?php echo $no;?></td>  
        <td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>  
        <td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>                 
    </tr>  
<?php  
    $no++;  
}  
?>  

但随后我得到一个错误,即未定义变量
c_row
和提供的无效参数
foreach()
。我想我已经通过
c_kategorimaterial/index
和复制粘贴
foreach
语句发送了
c_row
变量。出了什么问题

1)使用
print\r($data)
检查数据是否正确。如果出现错误,则可能是查询中遗漏了某些内容

2) 如果您完全获得了db数据,那么只需更改模型中返回数组的名称即可

function read()
{        
   $sql = $this->db->get('ms_kategori_material');  

   if($sql->num_rows() > 0)
    {     
        foreach($sql->result() as $row)
        {  
            $c_row[] = $row;  
        }     
        return $c_row;  //comment this return part while debugging
        // print_r($c_row);
        //exit;
    } 
    else 
    {  
        return null;  
    }  

}

最好打印数据数组,以便准确了解该数组中的内容。尝试以下格式,可能会有所帮助

function getPosts() {
$query = $this->db->get('posts');
$posts = array();

foreach ($query->result() as $row) {
    $posts[] = array(
        'id' => $row->id,
        'title' => $row->title,
        'content' => $row->content
    );
}

return $posts;
}

我认为您模型中的方法返回null!为什么不在模型中打印($data)并检查?只是想知道,为什么要将结果放入该数组中?如果您没有在模型中执行foreach循环,而是返回$sql->result(),则可以在返回结果之前节省在循环结果上花费的时间。因为你只是在制作数组的另一个副本。@JonathanChow我不知道我写了什么,因为我只是复制粘贴了代码,并通过每行代码理解它们。我猜你的模型返回null。更新控制器和模型示例代码,向我们显示
class
关键字。AKA-它们是如何命名的?你能告诉我应该把
打印($data)
放在哪里吗。idk,因为我正在复制粘贴代码,并更改一些变量,以及如何加载它们,因为我们无法从views@Cignitor检查编辑。我对
print\r($c\u row)进行了注释
part.删除注释,测试它并在调试后对其进行注释显示相同的错误,我正在使用您的函数并删除注释,相同的控制器和视图,如顶部所示,@Cignitor您没有退出它..添加此..
print\r($c\u row);退出;
。现在,检查输出并报告。我已将控制器更改为
$data['c_row']=$this->m_kategorimaterial->read();echo$data;
然后加载它们,它会显示一个带有“array”的空白页text返回一个空白屏幕,我在数据库中输入了一些记录,控制器的运行方式如下`function index(){$this->m_kategorimaterial->read();}`