HTML 助手 - 可重用视图 - PHP

发布于 2024-09-10 10:05:30 字数 1382 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕头说它不想醒 2024-09-17 10:05:30

你们在做什么?可重用的视图比这容易得多。只需创建一个视图并将其保存在视图文件夹中即可。添加添加和编辑数据时都会出现的字段,并在 value 参数中使用 if 语句来确定它是否有数据。

例如

控制器:

public function add()
{
    $data['method']    = 'add';
    $data['form_data'] = $this->some_model->get_something();
    $this->load->view('reusable_view', $data);
}

public function edit($id)
{
    $data['method']    = 'edit';
    $data['form_data'] = $this->some_model->get_something($id);
    $this->load->view('reusable_view', $data);
}

视图:

<form method="post" action="my_controller/" . <?php echo $method; ?>>
<input type="text" value="<?php if ( isset($form_data['something']) ) {echo $form_data['something'];} " />
</form>

我认为没有理由在控制器中填充表单,因为这不是 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:

public function add()
{
    $data['method']    = 'add';
    $data['form_data'] = $this->some_model->get_something();
    $this->load->view('reusable_view', $data);
}

public function edit($id)
{
    $data['method']    = 'edit';
    $data['form_data'] = $this->some_model->get_something($id);
    $this->load->view('reusable_view', $data);
}

View:

<form method="post" action="my_controller/" . <?php echo $method; ?>>
<input type="text" value="<?php if ( isset($form_data['something']) ) {echo $form_data['something'];} " />
</form>

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.

呆萌少年 2024-09-17 10:05:30

首先,默认参数应该位于右侧。我会将其默认为“false”或 NULL。

function WorkOrderForm($edit, $array = false)

然后也许检查 $array 是否不为 true,并将所有 $query[0] 设置为 NULL?所以像......

      if(!$array) { 
         $query[0]->name = $query[0]->description = $query[0]->status = null; 
      }

可能有一种更直接的方法,但这是一种方法。

First off, the default argument should be on the right. And I would default it to 'false' or NULL.

function WorkOrderForm($edit, $array = false)

Then maybe check if $array is not true, and set all the $query[0] to NULL? So something like...

      if(!$array) { 
         $query[0]->name = $query[0]->description = $query[0]->status = null; 
      }

There might be a more direct approach, but that's one way.

雨后咖啡店 2024-09-17 10:05:30

首先,如果您将查询作为数组发送到辅助函数,则没有理由将其转换为另一个数组。即 $array = array('$query') 应该只是 $query 这样您就可以访问如下属性: $query->name code> 与 $query[0]->name 相反。其次,如果您不编辑表单条目,您的 $query 将为空,因此您可以使用它作为返回内容的触发器(空白表单或填充表单):

function WorkOrderForm($query)
{            
    if($query!='')
    {
        //$formHTML=populated form
    } else {
        //$formHTML=empty form
    }
    return $formHTML;
}

好的?但是,有一个问题...您的助手中的代码将无法工作。您正在使用算术运算符 += 来(假设)连接表单数据。其作用是尝试将 1 添加到字符串中,该字符串始终等于 0。您正在寻找的是 .= 运算符;这将按预期连接表格。但是,这使您无法控制表单的外观(实际上,它将所有表单元素并排放置 - 不太漂亮)。您可以做的不是将它们全部连接在一起,而是将它们push到一个数组中,然后一一回显表单元素:

if($query!=''){

    $form_array=array();
    array_push($form_array,form_open('order/update',$attributes));
    array_push($form_array,form_input('name',$query->name));
    array_push($form_array,form_textarea('name',$query->description));

    $dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
    array_push($form_array,form_dropdown('status',$dropOptions,$query->status));         
    array_push($form_array,form_input('name',$query->startDate));
    array_push($form_array,form_input('name',$query->endDate));
    array_push($form_array,form_close());

}else{

    $form_array=array();
    array_push($form_array,form_open('order/add',$attributes));
    array_push($form_array,form_open('order/update'));
    array_push($form_array,form_input('name'));
    array_push($form_array,form_textarea('name'));

    $dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
    array_push($form_array,form_dropdown('status',$dropOptions));         
    array_push($form_array,form_input('name'));
    array_push($form_array,form_input('name'));
    array_push($form_array,form_close());

}

return $form_array;

然后要显示表单元素,请迭代 $form_array 数组返回:

$form_data='';//blank for new item, or data to populate form with to edit an item
$form_el = WorkOrderForm($form_data);

foreach($form_el as $key=>$val){
    echo $val.'<br>';//I just added a <br> here so each form element will be on a new line; change to fit your needs
}

希望这有帮助......

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):

function WorkOrderForm($query)
{            
    if($query!='')
    {
        //$formHTML=populated form
    } else {
        //$formHTML=empty form
    }
    return $formHTML;
}

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 add 1 to a string, which will always equal 0. 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:

if($query!=''){

    $form_array=array();
    array_push($form_array,form_open('order/update',$attributes));
    array_push($form_array,form_input('name',$query->name));
    array_push($form_array,form_textarea('name',$query->description));

    $dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
    array_push($form_array,form_dropdown('status',$dropOptions,$query->status));         
    array_push($form_array,form_input('name',$query->startDate));
    array_push($form_array,form_input('name',$query->endDate));
    array_push($form_array,form_close());

}else{

    $form_array=array();
    array_push($form_array,form_open('order/add',$attributes));
    array_push($form_array,form_open('order/update'));
    array_push($form_array,form_input('name'));
    array_push($form_array,form_textarea('name'));

    $dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
    array_push($form_array,form_dropdown('status',$dropOptions));         
    array_push($form_array,form_input('name'));
    array_push($form_array,form_input('name'));
    array_push($form_array,form_close());

}

return $form_array;

Then to present the form elements, iterate through the $form_array array that was returned:

$form_data='';//blank for new item, or data to populate form with to edit an item
$form_el = WorkOrderForm($form_data);

foreach($form_el as $key=>$val){
    echo $val.'<br>';//I just added a <br> here so each form element will be on a new line; change to fit your needs
}

Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文