我正在使用codeigniter和来自here的教程。我已经制作了一个基本的博客工具,工作正常。但是,由于它要添加一个新帖子,您必须转到单独的页面'create.php'才能访问该表单。我想尝试将表单放在与将要更新的页面相同的页面上,即'index.php'。如果我尝试这样做,表单只是刷新并提交数据。
模型
function insert_post($data){
$this->db->insert('posts', $data);
return;
}
当前视图(admin / create.php)
<?php echo validation_errors(); ?>
<h4>Create A New Post Below</h4>
<form action="" method="post" >
<p>Title:</p>
<input type="text" name="title" size="50"/><br/>
<p>Summary:</p>
<textarea name="summary" rows="2" cols="50"></textarea><br/>
<p>Post Content:</p>
<textarea name="content" rows="6" cols="50"></textarea><br/>
<input type="submit" value="Save" />
<?php echo anchor('admin','Cancel'); ?>
</form>
查看我希望表单打开(index.php)
<?php
echo '<p>Welcome '.$username.'! All posts available for edit or deletion is listed below.</p><br/>';
echo anchor('admin/create','Create New Post');
$count = count($post['id']);
for ($i=0;$i<$count;$i++)
{
echo '<div class="postDiv">';
echo '<h4>'.$post['title'][$i];
echo '<p>'.$post['summary'][$i].'</p>';
echo '<p>'.$post['content'][$i].'</p>';
//echo anchor('blog/view/'.$post['id'][$i],' [view]');
echo anchor('admin/edit/'.$post['id'][$i],' [edit]');
echo anchor('admin/delete/'.$post['id'][$i],' [delete]</h4>');
echo '</div>';
}
?>
控制器
function create(){
$data['userId'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('summary','summary','required');
$this->form_validation->set_rules('content','content','required');
if($this->form_validation->run()==FALSE)
{
$this->load->view('template/admin_html_head',$data);
$this->load->view('admin/create',$data);
$this->load->view('template/html_tail',$data);
} else {
$data = $_POST;
$this->posts->insert_post($data);
redirect('admin');
}
}
当我使用普通的php时,这是直截了当的,但是使用codeigniter我迷失了MVC的东西。我知道这可能是一个相当基本的问题,所以请解释你的答案,或者给我一些链接,以解释我需要做什么,因为我想从中学习。我已经阅读了有关验证的codeigniter文档,但我不认为这是我的问题?
答案 0 :(得分:2)
您要做的是称为嵌入视图。我将尝试解释如何,但你也应该检查一些可能更深入的链接:
http://net.tutsplus.com/tutorials/php/an-introduction-to-views-templating-in-codeigniter/
Codeigniter: Best way to structure partial views
您需要做的就是更改index.php上的链接:
echo anchor('admin/create','Create New Post');
到
$this->load->view('admin/create');
现在这应该可行,但是为了帮助你进入MVC前端,它有助于解释为什么这样做是错误的。 MVC的想法是将应用程序中的功能分离为不同的角色。大多数人都不愿意将业务逻辑放入视图中,除非它非常小。我们可以改进代码的方法是在控制器中加载视图,并将其设置为变量。
在视图的codeigniter文档的底部,它显示了如何加载到变量中:
http://ellislab.com/codeigniter/user-guide/general/views.html
如果load-&gt;视图的第三个参数设置为true,那么该函数会将您的视图作为字符串返回,而不是将其输出到浏览器
$data['input_form'] = $this->load->view('admin/create', $data, true);
然后在视图中你要加载那个表单你需要做的就是echo input_form
<?php echo $input_form;?>
这样可以解决您的问题,但您可以在视图文件中执行更多操作,以提高代码的可读性。
不是使用count()和for循环,而是使用foreach,这使得一切变得更加容易
<?php foreach ($post as $post_item):?>
<div>
<h4><?php echo $post_item['title'];?></h4>
</div>
<?php endforeach;?>
它还有助于破坏您的视图文件并拥有更多标签。它看起来似乎是额外膨胀,但是当你有更大的视图文件时,继续使用尽可能多的回声是非常麻烦的
答案 1 :(得分:0)
只需在表单操作中添加一个方法uri_string(),uri_string将采取相同的页面网址,您可以将表单提交到同一页面
<?php echo validation_errors(); ?>
<h4>Create A New Post Below</h4>
<form action="<?=uri_string()?>" method="post" >
<p>Title:</p>
<input type="text" name="title" size="50"/><br/>
<p>Summary:</p>
<textarea name="summary" rows="2" cols="50"></textarea><br/>
<p>Post Content:</p>
<textarea name="content" rows="6" cols="50"></textarea><br/>
<input type="submit" value="Save" />
<?php echo anchor('admin','Cancel'); ?>
</form>
in controller little chagnes
function create(){
$data['userId'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('summary','summary','required');
$this->form_validation->set_rules('content','content','required');
if($this->form_validation->run()==FALSE)
{
$this->load->view('template/admin_html_head',$data);
$this->load->view('admin/create',$data);
$this->load->view('template/html_tail',$data);
} else {
$data = $this->input->post();
$this->posts->insert_post($data);
redirect('admin');
}
}
答案 2 :(得分:0)
使用会话库
check this another stackoverflow thread to know how to use session
要使用会话库,您需要在 config.php
中配置 encryption_key