2016-11-22 218 views
0

我是codeigniter的新手,我試圖通過YouTube上的一些教程構建CMS,但我遇到了更新/編輯頁面的問題管理員區域。

ERRORS:

遇到
  1. 甲PHP錯誤

嚴重性:警告 消息:提供的foreach無效的參數() 文件名:助手/ form_helper.php 行號: 332

  1. 一個PHP錯誤遇到

嚴重性:注意 消息:未定義的屬性:stdClass的:: $標題 文件名:型號/ 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; 

     } 
    } 
}} 

查看管理員/頁/ 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>

查看管理員/頁/ 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>

感謝您的幫助。

+0

(http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) –

回答

0

我不知道你的代碼錯誤原因。

但一般情況下是指向@Abdulla Nilam。

例如。 下面的簡單代碼是error.because $ values爲null。

<?php 

$values=null; 
print_r($values); 

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

PHP的警告:在/workspace/Main.php提供的foreach()在網上提前幫助我6

+0

[爲的foreach()提供的無效參數]的可能重複我忘了提,傾$這 - >數據([ 'pages_no_parents']);給我空數組。 CODE: $ this-> data ['pages_no_parents'] = $ this-> page_m-> get_no_parents(); 轉儲($ this-> data ['pages_no_parents']); –

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