我在理解MVC的概念和一次显示多个表单时遇到了根本问题。我尝试了各种各样的方法,但我仍然坚持 - 这是因为我认为我没有正确理解CI和MVC。
我尝试了两种不同形式使用2种不同的视图。没工作。我尝试在控制器中为每个表单使用一个函数。那也行不通。我不知道该怎么做。
我应该这样做吗;
6是我最大的问题。不知道该怎么做。例如,在成功完成表单后,我希望我的用户在所选位置创建了一个目录 - 所以我使用的是mkdir() - 所以我需要在validate()函数中使用if语句或者什么?< / p>
的更新 的
这是我到目前为止创建的代码;
控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//表示CodeIgniter控制器 class Admin扩展了CI_Controller {
// Controller constructor
public function __construct()
{
parent::__construct();
// Load form helper required to validate forms
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
//*************************************************//
// Prepare data for the view to output the forms
public function index()
{
//*****************************************************//
//returns a drop down list of radio buttons, one for each directory
$map_one = $this->recursive_model->iterate_add_folder_names();
$data['folder_list_add'] = $map_one;
//****************************************************//
//*****************************************************//
//also returns a drop down list of radio buttons (slightly different), one for each directory
$map_two = $this->recursive_model->iterate_folder_names();
$data['folder_list_select'] = $map_two;
//****************************************************//
//load the views and the forms
$this->load->view('templates/header.php');
$this->load->view('admin/add_new_folder.php', $data);
$this->load->view('admin/add_new_file.php', $data);
$this->load->view('templates/small_footer.php');
}
//*************************************************//
//function if adding a new directory to the current structure
public function add_folder()
{
//need to select a directory for it to go under
$this->form_validation->set_rules('new_folder', 'New Folder', 'required');
//and name the new directory
$this->form_validation->set_rules('new_folder_name', 'New Folder Name', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->index();
}
else
{
if($this->input->post())
{
$new_folder = $this->input->post('new_folder');
$new_folder_name = $this->input->post('new_folder_name');
$folder_path = "/var/www/html/mike/content".$new_folder."/".$new_folder_name;
mkdir($folder_path, 0777);
$this->index();
}
}
}
//*************************************************//
public function add_file()
{
//folder location and name of file
$folder_name = $this->input->post('folder_name');
$new_folder_name = $this->input->post('file_name');
//validation rules
$this->form_validation->set_rules('folder_name', 'Folder Name', 'required');
$this->form_validation->set_rules('file_name', 'File Name', 'required');
//if there is an error with validation
if ($this->form_validation->run() === FALSE)
{
//gets stuck here every time when trying to upload a new folder :(
$this->index();
}
//if there is not an error with validation
else
{
//$folder_name will be something like "http://www.example.com/publications/people/reports"
$config['upload_path'] = $folder_name;
$config['allowed_types'] = 'gif|jpg|png|html|pdf|xls';
$this->load->library('upload', $config);
//if file cannot be loaded (due to $config perhaps?)
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->index();
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->index();
}
}
}
//*************************************************//
}
这是一个视图(add_new_file.php);
<div id="container">
<h1>Upload A File/Publication</h1>
<div id="body">
<?php //echo $error;?>
<?php echo form_open_multipart('admin/add_file');?>
<?php echo $folder_list_select; ?>
<input type="file" name="file_name" size="20" />
<input type="submit" value="upload" />
</form>
</div>
这是另一个(add_new_folder.php)
div id="container">
<h1>Add A New Folder</h1>
<div id="body">
<?php echo validation_errors(); ?>
<?php echo form_open('admin/add_folder');?>
<?php echo $folder_list_add; ?>
New Folder Name: <input type="text" name="new_folder_name">
<input type="submit" value="upload" />
</form>
</div>
我希望这有助于回答这个问题。
基本上,我可以让第一部分工作 - 添加一个文件夹 - 但我无法添加一个文件来工作。这是因为如果($ this-&gt; form_validation-&gt; run()=== FALSE)总是返回false。我认为它可能正在查看另一种形式的表单元素 - 它不应该这样做。我错过了什么?
答案 0 :(得分:6)
我应该这样做吗;
1。创建一个控制器并在其中有一个index()函数 [让我们,为了对话,请致电此控制器Users
thx -ed]
不确定。这很酷。您还可以在该控制器中使用名为edit
或banana
或其他任何内容的函数;两种方式都有效。仅使用index
方法(函数),网址可能看起来像http://example.com/index.php/users
,而如果您向控制器添加另一个方法,例如banana
,则网址可能看起来像http://example.com/index.php/users/banana
2。为此索引()
中的每个表单构建我的表单元素
嗯,通常不会在控制器中创建表单元素。这就是MVC中的V进入的地方 - 您查看的内容会进入view
。
所以,人们可能会做类似
的事情// Users Controller
class Users extends CI_Controller{
function index(){
//index method
}
function banana(){
$this->load->view('banana_view');
}
}
然后在application/views/banana_view.php
中创建表单。当您访问http://example.com/users/banana
时,您会看到在banana_view.php
中创建的表单。
3。创建一个显示两个表单的视图,并从index()
中调用它
当然,那工作得很好。但请记住,每个<form></form>
都需要自己的<input type="submit" name="Lets GO">
,因此需要在某处发送每个表单数据。这是action=""
。您可以将其删除,但要注意它会将表单发送到您当前所在的任何页面(在我们的案例中为http://example.com/index.php/users/banana
),因此您必须使用banana()
方法中的某些内容处理表单数据。但是,通常会通过form_open()
进行设置。 form_open('index.php/users/eat_banana');
之类的内容会生成<form action="index.php/users/eat_banana"...
4。使用form_open将提交操作定向到另一个函数 - 将其命名为validate()
请勿将其称为late_for_dinner
。但严重的是,validate
有点宽泛 - 验证什么?验证原因?至于验证,https://www.codeigniter.com/user_guide/libraries/form_validation.html。但是,在了解了CodeIgniter的基础知识之后,你应该越过那座桥梁。(不会花很长时间)。
5。验证所有内容,发回错误
见最后一个问题。
6。不知何故,这是我没有得到的主要部分,如果表格已正确填写,请完成一个动作。
很多时候人们会显示成功消息
class Users extends CI_Controller{
function index(){
//index method
}
function banana(){
$this->load->view('banana_view');
}
// assuming form_open('index.php/users/eat_banana'); in banana_view
function eat_banana(){
//make sure that this is a POST
if($this->input->post()){
// do things with the data
// typically it gets saved to a database
// via a model (the M in MVC)
// http://ellislab.com/codeigniter/user-guide/general/models.html
if($saved_to_db){
// set message to send to the view
$data['message'] = "Everything went OK";
}else{
$data['message'] = "but who was database? data didn't save :(";
}
// load the view and send the data
$this->load->view('eat_banana', $data);
}
}
application/views/eat_banana.php
:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
<b>Form submitted.</b><br />
The message is: <?php echo $message; ?>
</div>
</html>
其他时候,人们可能更喜欢重定向
class Users extends CI_Controller{
function index(){
//index method
}
function banana(){
$this->load->view('banana_view');
}
// assuming form_open('index.php/users/eat_banana'); in banana_view
function eat_banana(){
//make sure that this is a POST
if($this->input->post()){
// do things with the data
if($saved_to_db){
// just send them to the homepage
redirect('/');
}else{
// send them back to the form
redirect('index.php/users/banana');
}
}
}
所以,
M 适用于型号。模型用于与数据库通信。
V 适用于 Vend 视图。视图呈现文本,表单,图片,GIF,以及屏幕上的任何内容。无论如何,这是个主意。没有什么可以阻止你echo
从你的控制器中输出一个巨大的未经过最小化的javascript应用程序。那完全不是MVC。
C 适用于控制器。控制器调用并向视图发送数据,接收从视图发送的数据,获取数据并将其发送到模型以保存在数据库中(尽管CodeIgniter也不以任何方式强制执行此操作;如果您想保存,则可以直接来自控制器的数据到数据库,但这显然也击败了MVC主体),从数据库中检索数据并将其发送到视图进行显示。无论如何,这些都是基础。