2011-04-11 41 views
0

我收到以下錯誤,當我嘗試訪問:笨 - >編輯頁面

domain.co.nz/admin/editpage/home/

我得到以下錯誤:

PHP Fatal error: Call to a member function getCMSPage() on a non-object in controllers/home.php on line 22 

這個問題是,我不明白爲什麼它被傳回主控制器主「」。

我所有的車型都默認加載 - http://cl.ly/2U1F3a2B0s2K0i3k3g13

理想的情況

我試圖用這個做的是內容加載到一個文本區域上進行編輯和被點擊時提交我希望它回到同一頁面,並附上更新內容的消息。

管理模板

<li><?php echo anchor('#','Edit Pages');?> 
      <?php if(is_array($cms_pages)): ?> 
        <ul> 
         <?php foreach($cms_pages as $page): ?> 
         <li><a >permalink?>"><?=$page->name?></a></li> 
         <?php endforeach; ?> 
        </ul> 
       <?php endif; ?> 
       </li> 

頁面模型

function updatePage($data){ 
     $data = array('content' => $content); 
     $this ->db->where('id',$id); 
     $this->db->update('pages',$data); 
    } 

查看:

<?php 
//Setting form attributes 
$formpageEdit = array('id' => 'pageEdit', 'name' => 'pageEdit'); 
$formInputTitle = array('id' => 'title', 'name' => 'title'); 
$formTextareaContent = array('id' => 'content', 'name' => 'content'); 
?> 

<section id = "validation"><?php echo validation_errors();?></section> 

<h4><?= $title ?> </h4> 
<?php 
echo form_open('admin/editpage/'.$page->permalink, $formpageEdit); 
echo form_fieldset(); 
echo form_label ('Content', 'content'); 
echo form_textarea("content", $page['content']); 
echo form_submit('submit','Submit'); 
echo form_fieldset_close(); 
echo form_close(); 
?> 

控制器:

function index(){ 
     if($this->session->userdata('logged_in')){ 

     }else{ 
      redirect('admin/home'); 
     } 
     $page = $this->navigation_model->getCMSPage($this->uri->segment(3)); 
     $data['cms_pages'] = $this->navigation_model->getCMSPages(); 
     $data['title'] = $page->name; 
     $data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE)); 

     $this->load->view('admintemplate', $data); 

    } 
+0

需要整個控制器功能 – jondavidjohn 2011-04-12 01:02:47

+0

請從您的navigation_model發佈getCMSPage()方法。 – Catfish 2011-05-12 19:34:24

回答

0
// this line 
$data['content'] = $this->load->view('admin/editpage', $data); 
// needs to be 
$data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE); 
+0

當我訪問:http: //domain.co.nz/admin/editpage/uriseg我在第22行的/House/application/controllers/home.php中的非對象上調用成員函數getCMSPage() – 2011-04-12 23:21:43

1

我還沒有真正的考驗還沒有這個,但它應該給你一個良好的開端。你所要求的是很快完成代碼的工作。

這是page_model模型,它基本上是你發佈的幾個小調整。最好使用id而不是字符串。在將這些數據傳遞給數據庫之前,您可能還需要執行一些htmlspecialchars或mysql劫持驗證。

<?php 
/** 
* This is the Page_model model, this model handles the retrieval and modification of all pages. 
* 
**/ 
class Page_model extends CI_Model { 
/** 
* the getCMSPage function retrieves the data from the db given the ID that was passed via $id. 
**/ 
    function getCMSPage($id = NULL) { 
     $this->db->where('permalink', $permalink); 
     $query = $this->db->get('pages', 1); 

     #check to make sure row's were returned, if so continue, otherwise return false. 
     if ($query->num_rows() > 0){ 
      #set the results into an array and return $row, should return $row['content'], and $row['id']; 
      #if you were editing more than one page this is where you would use a foreach($query) 
      $row = $query->result_array(); 
      return $row; 
     }else{ 
      return false; 
     }// END if ($query->num_rows() > 0) 
    }// END function getCMSPage() 
}// END Page_model class 
?> 

這是現場控制器和時間的緣故,我剛回顯的你form_textarea直接從編輯功能..你不應該這樣做,因爲它違背了MVC的標準。您應該爲該部分的該部分創建一個視圖。

<?php 
/** 
* This is the Site controller, primary controller for your site. 
* 
**/ 
class Site extends CI_Controller { 
/** 
* construct function, in our case we are going to load the Posts_model , you may not want to do this. 
* 
**/ 
    function __construct() 
    { 
     parent::__construct(); 
     #load Page_model, should be located in app/models/page_model.php 
     $this->load->model('Page_model'); 
    }//END function __construct(); 

/** 
* edit function, this function handles retrieval of the page from the URI, and the page's content for editing. 
* 
* This function uses $id which auto retrieves the page's ID from the uri. Your URL should look similiar to: 
* http://yourdomain.com/site/edit/3/yourunecessaryinformationhere 
* everything after the id is not really required but could help with SEO. 
* 
**/ 
    function edit($id){ 
     #retrieve the page's content in array form. 
     $page = $this->Page_model->getCMSPage($id); 
     echo form_textarea("content", $page['content']); 
    } 
}//END Site Class 

請記住我在5-10分鐘內寫了這件事所以它匆忙,並沒有很好的註釋,甚至測試,但ITLL給你一個很好的開端。這隻給你一個如何從數據庫檢索信息並將其回顯到textarea的例子。您仍然需要在您的page_model中創建另一個函數來插入/更新控制器中的信息和代碼片段,以將編輯的內容傳遞給模型。

你可以打我的AIM或Twitter,如果您有更多問題ThirdGenSup,Twitter是@gorelative

+0

更新我的回答,重新閱讀 – gorelative 2011-04-12 01:23:48

+0

Mike,我已經完成了上面所說的,但是每次訪問「固定鏈接」頁面時似乎都認爲它是我的主「家庭控制器」 – 2011-04-12 18:47:05