Welcome Guest, Not a member yet? Register   Sign In
Forms (html vs arrays)
#1

[eluser]echo sara[/eluser]
Hi,

i was wondering which is better to use. For normal input with forms is it better to use arrays or the regular html method

html
Code:
<input type="text" name="firstname" />

array
Code:
$data = array(
'name' => 'url',
'id' => 'url',
'value' => 'bingo',
'maxlength' => '100',
'size' => '50',
'style' => 'yellow',
);

It seem like the html is alot less work and takes up less space. I understand if i were doing a drop down it is better to use a array since data would be passed back.

HTML
Code:
<select name="type">
<option value="1" selected>www.this.com</option>
<option value="2">www.that.com</option>
<option value="3" >www.theother.com</option>
</select>


Array
Code:
$urlarray = array(
'1' => 'www.this.com',
'2' => 'www.that.com',
'3' => 'www.theother.com',
);


What do you this is the best method for the different situation(forms) how about check box and radio buttons?

Sara
#2

[eluser]danmontgomery[/eluser]
Well, your two examples for the input aren't the same, so of course the html looks cleaner. The equivalent would be:
Code:
echo form_input('firstname');

Which doesn't really seem harder or easier, it's just preference. The value of the helper functions comes when passing values to those inputs.

Code:
&lt;input type="text" name="firstname" value="&lt;?php if(isset($_POST['firstname'])) echo $_POST['firstname']; ?&gt;"/&gt;

// vs

echo form_input('firstname', set_value('firstname'));

This is even more apparent with selects:

Code:
<select name="type">
<option value="1"&lt;?php if(isset($_POST['type']) && $_POST['type'] == 1) echo ' selected="selected"'; ?&gt;>www.this.com</option>
<option value="2"&lt;?php if(isset($_POST['type']) && $_POST['type'] == 2) echo ' selected="selected"'; ?&gt;>www.that.com</option>
<option value="3"&lt;?php if(isset($_POST['type']) && $_POST['type'] == 3) echo ' selected="selected"'; ?&gt;>www.theother.com</option>
</select>

// vs

$urlarray = array(
    '1' => 'www.this.com',
    '2' => 'www.that.com',
    '3' => 'www.theother.com',
);

echo form_dropdown('type', $urlarray, set_value('type', 1));




Theme © iAndrew 2016 - Forum software by © MyBB