2011-04-11 81 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