doukang1962 2016-11-22 19:37
浏览 63
已采纳

为foreach()和Undefined属性提供的参数无效:stdClass :: $ title

I am new to codeigniter and I am trying to build a CMS by some tutorials on YouTube, but I am having a problem with updating/editing pages in admin area.

ERRORS:

  1. A PHP Error was encountered

Severity: Warning Message: Invalid argument supplied for foreach() Filename: helpers/form_helper.php Line Number: 332

  1. A PHP Error was encountered

Severity: Notice Message: Undefined property: stdClass::$title Filename: models/page_m.php Line Number: 77


Controller page.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;
}}

Model 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;

        }
    }
}}

View 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>

View 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>

Thank You for your help.

</div>
  • 写回答

2条回答 默认 最新

  • dragon0023 2016-11-22 20:30
    关注

    I don't know your code error cause.

    But general case is point out to @Abdulla Nilam.

    For example. Below simple code is error.because $values is null.

    <?php
    
    $values=null;
    print_r($values);
    
    foreach ($values as $value) {
        print_r($value);
    }
    ?>
    

    PHP Warning: Invalid argument supplied for foreach() in /workspace/Main.php on line 6

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题