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

时间:2016-11-22 19:37:55

标签: php arrays codeigniter

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

错误:

  1. 遇到PHP错误
  2.   

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

    1. 遇到PHP错误
    2.   

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

      控制器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;
      }}
      

      模型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>
      &#13;
      &#13;
      &#13;

      查看admin / page / index.php

      &#13;
      &#13;
      <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>
      &#13;
      &#13;
      &#13;

      感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

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

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

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

<?php

$values=null;
print_r($values);

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

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

答案 1 :(得分:0)

在此先感谢您的帮助,我发现了错误。

显然我没有在我的函数上获得返回数组。

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