Codeigniter - >编辑页面

时间:2011-04-11 23:31:56

标签: php forms codeigniter

尝试访问时出现以下错误:

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 

这个问题是我无法理解为什么它会被传回主“home”控制器 - 这是主控制器。

我的所有模型都默认加载 - 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);

    } 

2 个答案:

答案 0 :(得分: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分钟内写了这个,所以它很匆忙,评论不太好,甚至没有测试,但它会给你一个很好的开端。这只给出了一个如何从数据库中检索信息并将其回显到textarea的示例。您仍然需要在page_model中创建另一个函数来插入/更新控制器中的信息和另一段代码,以将编辑过的内容传递给模型。

如果你有更多问题,你可以在AIM或推特上打我第三名,推特是@gorelative

答案 1 :(得分: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);