【问题标题】:Invalid argument supplied for foreach() and Undefined property: stdClass::$title为 foreach() 和未定义属性提供的参数无效:stdClass::$title
【发布时间】:2016-11-22 19:37:55
【问题描述】:

我是 codeigniter 的新手,我正在尝试通过 YouTube 上的一些教程构建 CMS,但在管理区域更新/编辑页面时遇到问题。

错误:

  1. 遇到 PHP 错误

严重性:警告 消息:为 foreach() 提供的参数无效 文件名:helpers/form_helper.php 行号:332

  1. 遇到 PHP 错误

严重性:通知 消息:未定义的属性:stdClass::$title 文件名:models/page_m.php 行号:77


控制器页面.php

class Page extends Admin_Controller
{

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

    //Fetch all pages
    $this->data['pages'] = $this->page_m->get_with_parents();

    // Load view
    $this->data['subview']='admin/page/index';
    $this->load->view('admin/_layout_main', $this->data);
}
public function edit($id = NULL){

    // Fetch a page or set a new one
    if ($id){
        $this->data['page'] = $this->page_m->get($id);
        count($this->data['page']) || $this->data['errors']='Page could not be found';
    }
    else{
        $this->data['page'] = $this->page_m->get_new();
    }

    //Pages for dropdown
    $this->data['pages_no_parents'] = $this->page_m->get_no_parents();
    var_dump($this->data['pages_no_parents']);

    //Set up the form
    $rules = $this->page_m->rules;
    $this->form_validation->set_rules($rules);

    //Proccess the form
    if ($this->form_validation->run() == TRUE){
        $data = $this->page_m->array_from_post(array('title','slug','order','body', 'parent_id'));
        $this->page_m->save($data, $id);
        redirect('admin/page');
    }

    //Load the view
    $this->data['subview'] = 'admin/page/edit';
    $this->load->view('admin/_layout_main', $this->data);
}
public function _unique_slug($str){

    // Do NOT validate if slug already exists
    //UNLESS its the slug for the current page
    $id = $this->uri->segment(5);
    $this->db->where('slug', $this->input->post('slug'));
    !$id || $this->db->where('id !=', $id);
    $page = $this->page_m->get();

    if (count($page)){
        $this->form_validation->set_message('_unique_slug', '%s should be unique');
        return FALSE;
    }
    return TRUE;
}}

模型 page_m.php

class Page_m extends MY_Model
{

protected $_table_name = 'pages';
protected $_order_by = 'order';
public $rules = array(
    'parent_id' => array(
        'field' => 'parent_id',
        'label' => 'Parent',
        'rules' => 'trim|intval'
    ),
    'title' => array(
        'field' => 'title',
        'label' => 'Title',
        'rules' => 'trim|required|max_length[100]'
    ),
    'slug' => array(
        'field' => 'slug',
        'label' => 'Slug',
        'rules' => 'trim|required|max_length[100]|url_title|callback__unique_slug'
    ),
    'order' => array(
        'field' => 'order',
        'label' => 'Order',
        'rules' => 'trim|required'
    ),
    'body' => array(
        'field' => 'body',
        'label' => 'Body',
        'rules' => 'trim|required'
    ),
);
public function get_new()
{
    $page = new stdClass();
    $page->title = "";
    $page->slug = "";
    $page->order = "";
    $page->body = "";
    $page->parent_id = 0;
    return $page;
}
 public function delete($id)
{
    //Delete a page
    parent::delete($id);

    //Reset parent ID for its children
    $this->db->set(array('parent_id' =>0))->where('parent_id', $id)->update($this->_table_name);

}

public function get_with_parents($id = NULL, $single = FALSE){
    $this->db->select('pages.*, p.slug as parent_slug, p.title as parent_title');
    $this->db->join('pages as p', 'pages.parent_id=p.id', 'left');
    return parent::get($id, $single);
}

public function get_no_parents()
{
    // Fetch pages without parents

    $this->db->select('id', 'title');
    $this->db->where('parent_id',0);
    $pages = parent::get();

    // Return key => value pair array
    $array = array(0 => 'No parent');
    if (count($pages)){
        foreach ($pages as $page){
            $array[$page->id]=$page->title;

        }
    }
}}

查看 admin/page/edit.php

<h3><?php echo empty($page->id) ? 'Add a new page' : 'Edit page: '.$page->title; ?></h3>
<div class="modal-body">
    <?php echo validation_errors(); ?>
    <?php echo form_open(); ?>
    <table class="table">
        <tr>
            <td>Parent</td>
            <td><?php echo form_dropdown('parent_id', $pages_no_parents,
                    $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id); ?></td>
        </tr>
        <tr>
            <td>Title</td>
            <td><?php echo form_input('title', set_value('title',$page->title)); ?></td>

        </tr>
        <tr>
            <td>Slug</td>
            <td><?php echo form_input('slug',  set_value('slug', $page->slug)); ?></td>

        </tr>
        <tr>
            <td>Order</td>
            <td><?php echo form_input('order',  set_value('order', $page->order)); ?></td>

        </tr>
        <tr>
            <td>Body</td>
            <td><?php echo form_textarea('body',  set_value('body', $page->body)); ?></td>

        </tr>
        <tr>
            <td></td>
            <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
        </tr>
    </table>
    <?php echo form_close(); ?>
</div>

查看 admin/page/index.php

<section>
    <h2>Pages</h2>
    <?php echo anchor('admin/page/edit' , '<i class="glyphicon glyphicon-plus"></i> Add a page'); ?>
    <table class="table">
        <thead>
            <tr>
                <th>Title</th>
                <th>Parent</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
    <?php if (count($pages)): foreach ($pages as $page): ?>

            <tr>
                <td><?php echo anchor('admin/page/edit/' . $page->id, $page->title); ?></td>
                <td><?php echo $page->parent_slug; ?></td>
                <td><?php echo btn_edit('admin/page/edit/' . $page->id); ?></td>
                <td><?php echo btn_delete('admin/page/delete/' . $page->id); ?></td>
            </tr>
    <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td class="col-sm-3"> We could not find any pages.</td>
            </tr>
        <?php endif; ?>
        </tbody>
    </table>
</section>

感谢您的帮助。

【问题讨论】:

标签: php arrays codeigniter


【解决方案1】:

我不知道你的代码错误原因。

但一般情况是向@Abdulla Nilam 指出的。

例如。 下面的简单代码是error.because $values is null.

<?php

$values=null;
print_r($values);

foreach ($values as $value) {
    print_r($value);
}
?>

PHP 警告:在第 6 行的 /workspace/Main.php 中为 foreach() 提供了无效参数

【讨论】:

  • 我忘了提到转储 $this->data(['pages_no_parents']);给我空数组。代码:$this->data['pages_no_parents'] = $this->page_m->get_no_parents();转储($this->data['pages_no_parents']);
【解决方案2】:

我发现了错误。

显然我的函数没有返回数组。

public function get_no_parents()
{
    // Fetch pages without parents

    $this->db->select('id', 'title');
    $this->db->where('parent_id', 0);
    $pages = parent::get();

    // Return key => value pair array
    $array = array(0 => 'No parent');
    if (count($pages)) {
        foreach ($pages as $page) {
            $array[$page->id] = $page->id;
            //
        }
    }
    return $array;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2016-04-14
    • 1970-01-01
    • 2021-09-08
    • 2020-04-19
    • 2011-05-17
    • 2011-02-07
    相关资源
    最近更新 更多