HTML 助手 - 可重用视图 - PHP
我是 PHP 新手,我希望有人可以帮助我确定创建可重用视图的最佳方法是什么。我拥有的是一个用于添加或编辑数据的表单。因此,视图将是相同的,除非在编辑的情况下将填充表单字段。
所以我正在创建一个带有 2 个参数的 HTML 帮助器。第一个是表单数据(如果有的话{edit}),第二个是一个布尔值,标记这是插入还是编辑(更改表单操作)。
我的问题是...如果表单用于添加数据并且因此不包含数据,我应该如何处理第一个参数?可选参数?
编辑--
我使用 CodeIgnitor 作为我的 MVC 框架。这就是那些表单函数被继承的内容..仅供参考,
谢谢..
<?php
if(!defined('BASEPATH') ) exit('No direct script access allowed');
if(!function_exists('WorkOrderForm'))
{
function WorkOrderForm($array = array('$query'),$edit)
{
$formHtml = "";
$attributes = array('class'=>'order','id'=>'orderForm');
if($edit)
{
$formHtml += form_open('order/update',$attributes);
}
else
{
$formHtml += form_open('order/add',$attributes);
}
$formHtml += form_input('name',$query[0]->name);
$formHtml += form_textarea('name',$query[0]->description);
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
$formHtml += form_dropdown('status',$dropOptions,$query[0]->status);
$formHtml += form_input('name',$query[0]->startDate);
$formHtml += form_input('name',$query[0]->endDate);
$formHtml += form_close();
return $formHtml;
}
}
?>
I am new to PHP and I was hoping someone could help me determine what the best way to go about creating a reusable view would be. What I have is a form that will be used to Add or Edit data. Therefore the view will be identical except in the case of an Edit the form fields will be populated.
So I am creating an HTML helper that takes 2 parameters. The first will be form data (if there is any{edit}) and the second will be a bool that flags whether this is an insert or and edit(to change form action).
My question is... how should I handle the first parameter if the form is to used to Add data and therefore does not contain data? Optional parameter?
EDIT--
I am using CodeIgnitor as my MVC framework. That is what those form functions are being inherited.. fyi
Thanks..
<?php
if(!defined('BASEPATH') ) exit('No direct script access allowed');
if(!function_exists('WorkOrderForm'))
{
function WorkOrderForm($array = array('$query'),$edit)
{
$formHtml = "";
$attributes = array('class'=>'order','id'=>'orderForm');
if($edit)
{
$formHtml += form_open('order/update',$attributes);
}
else
{
$formHtml += form_open('order/add',$attributes);
}
$formHtml += form_input('name',$query[0]->name);
$formHtml += form_textarea('name',$query[0]->description);
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
$formHtml += form_dropdown('status',$dropOptions,$query[0]->status);
$formHtml += form_input('name',$query[0]->startDate);
$formHtml += form_input('name',$query[0]->endDate);
$formHtml += form_close();
return $formHtml;
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你们在做什么?可重用的视图比这容易得多。只需创建一个视图并将其保存在视图文件夹中即可。添加添加和编辑数据时都会出现的字段,并在 value 参数中使用 if 语句来确定它是否有数据。
例如
控制器:
视图:
我认为没有理由在控制器中填充表单,因为这不是 MVC 的工作方式。使用助手来填充表单也很奇怪,我认为您稍微错过了 Codeigniter 的工作原理。
What are you guys doing? A reusable view is so much easier than this. Simply create a view and save it in the views folder. Add the fields that will appear both when adding and editing data and use an if statement in the value parameter to determine if it has data.
E.g.
Controller:
View:
I see no reason to populate a form in a controller as that's not the way MVC works. Using a helper in order to populate the form is also weird, I think you've slightly missed the point of how Codeigniter works.
首先,默认参数应该位于右侧。我会将其默认为“false”或 NULL。
然后也许检查 $array 是否不为 true,并将所有 $query[0] 设置为 NULL?所以像......
可能有一种更直接的方法,但这是一种方法。
First off, the default argument should be on the right. And I would default it to 'false' or NULL.
Then maybe check if $array is not true, and set all the $query[0] to NULL? So something like...
There might be a more direct approach, but that's one way.
首先,如果您将查询作为数组发送到辅助函数,则没有理由将其转换为另一个数组。即
$array = array('$query')
应该只是$query
这样您就可以访问如下属性:$query->name
code> 与$query[0]->name
相反。其次,如果您不编辑表单条目,您的$query
将为空,因此您可以使用它作为返回内容的触发器(空白表单或填充表单):好的?但是,有一个问题...您的助手中的代码将无法工作。您正在使用算术运算符 += 来(假设)连接表单数据。其作用是尝试将
1
添加到字符串中,该字符串始终等于0
。您正在寻找的是.=
运算符;这将按预期连接表格。但是,这使您无法控制表单的外观(实际上,它将所有表单元素并排放置 - 不太漂亮)。您可以做的不是将它们全部连接在一起,而是将它们push
到一个数组中,然后一一回显表单元素:然后要显示表单元素,请迭代 $form_array 数组返回:
希望这有帮助......
Firstly, if you're sending your query to the helper function as an array, there is no reason to turn it into another array. ie
$array = array('$query')
should just be$query
this way you can access properties like this:$query->name
as opposed to$query[0]->name
. Secondly, if you're not editing the form entry, your$query
would be empty, so you can use that as the trigger for what to return (either the blank form or the populated form):Okay? But, there's a problem... The code you have in your helper won't work. You're using an arithmetic operator
+=
to (assuming) concatenate the form data. What this does is try to add1
to a string, which will always equal0
. What you're looking for is the.=
operator; this will concatenate the form as would be expected. However, this offers you little control over how the form will look (as is, it will put all the form elements side-by-side -- not too pretty). What you could do is instead of concatenating them all together,push
them into an array, then echo the form elements out one-by-one:Then to present the form elements, iterate through the $form_array array that was returned:
Hope this helps...