views:

75

answers:

3

Hello..

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;
    }
}
?>
A: 

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.

TerryMatula
A: 

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...

stormdrain
+3  A: 

What the fuck 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.

Yorick Peterse
Heh. Well, don't I feel silly...
stormdrain
The approach of using array_push is hideous and makes no sense. Views are ment for these tasks so use them.
Yorick Peterse
This is the correct answer I believe. It's a shame you couldn't answer it without being a douche bag. I not a PHP developer and even less of a CI PHP developer. This would account for the naive question.
Nick