提供了无效的参数

提供了无效的参数

本文介绍了为Codeigniter中的foreach()提供了无效的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息:视图中foreach()的参数无效.我想显示我的mysql表中的所有条目,但我不断收到错误消息.我是Codeigniter的新手,无法真正解决该问题.代码如下:我的模型(display_branch.php)

I am getting error message: Invalid argument for foreach() in my View. I wanted to display all entries in my mysql table but i kept on getting error message. I am a newbie in Codeigniter and couldn't really figure out how to solve this. The codes are the following:My model (display_branch.php)

<?php if(!defined ('BASEPATH')) exit ('No direct script access allowed');
class Display_Branch extends CI_Model
{
function __construct()
{
    parent::__construct();
}

public function getAll()
{
    $this->db->select('bcode, bname, btel, badd');
    $this->db->from('branches');

    $query = $this->db->get();

    if($query->num_rows() == 1)
    {
        $results = $query->result();
        return $results;
    }
    else
    {
        return FALSE;
    }
}
}?>

我的控制器(link_controller.php)*仅是代码段.

My Controller (link_controller.php) *only the snippet.

public function insert()
{
    $this->load->model('display_branch');

    $data['results'] = $this->display_branch->getAll();

    $this->load->view('insert_branch');
    $this->load->view('navigation');
    $this->load->view('content_bc', $data);
    $this->load->view('footers');
}

我的观点(content_bc.php)

And my view(content_bc.php)

<?php


        foreach($results as $row)
        {
            echo '<tr>';
            echo '<td>'.$row->bcode.'</td>';
            echo '<td>'.$row->bname.'</td>';
            echo '<td>'.$row->btel.'</td>';
            echo '<td>'.$row->badd.'</td>';
            echo '</tr>';
        }

?>

请帮助我该怎么办.谢谢.

Please help me what to do. Thank you.

推荐答案

您需要在开始迭代数据之前进行检查,例如:型号代码:

you need to do a check before starting to iterate for the data, like:model code:

public function getAll() {
    $results = array();
    $this->db->select('bcode, bname, btel, badd');
    $this->db->from('branches');

    $query = $this->db->get();

    if($query->num_rows() > 0) {
        $results = $query->result();
    }
    return $results;
}

查看代码:

if( !empty($results) ) {
    foreach($results as $row) {
        echo '<tr>';
        echo '<td>'.$row->bcode.'</td>';
        echo '<td>'.$row->bname.'</td>';
        echo '<td>'.$row->btel.'</td>';
        echo '<td>'.$row->badd.'</td>';
        echo '</tr>';
    }
}

这篇关于为Codeigniter中的foreach()提供了无效的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 11:32