Hi,
I have some inputs in my CodeIgniter view file that I would like to set from the DB. Here’s an example section that creates the first input in the form:
$user_name = array(
'name' => 'article[name]',
'id' => 'name',
'value' => set_value('article[name]', $article->name),
'maxlength' => '255',
'size' => '50',
'style' => 'width:100%',
);
echo form_label('Name: *', $user_name['name']).br();
echo form_input($user_name).br();
echo form_error($user_name['name']).br();
If I wanted to put the above code in a loop, and change the “$user_name” and “name” in each iteration, how can I achieve this? Variable variables i think?
xMog
March 3, 2014, 2:50am
2
Maybe this would work :
function displayForm($dataArr, $label) {
echo form_label($label . ': *', $dataArr['name']).br();
echo form_input($dataArr).br();
echo form_error($dataArr['name']).br();
}
I think you are asking for something like this:
foreach ($users as $user) {
echo form_label('Name: *', $user['name']).br();
echo form_input($user).br();
echo form_error($user['name']).br();
}
Where $users would be assigned to an array of values similar to what you have assigned to $user_name on your initial post.
xMog
March 3, 2014, 12:52pm
4
parallelist:
I think you are asking for something like this:
foreach ($users as $user) {
echo form_label('Name: *', $user['name']).br();
echo form_input($user).br();
echo form_error($user['name']).br();
}
Where $users would be assigned to an array of values similar to what you have assigned to $user_name on your initial post.
There’s the label “Name:” that will always be the same in your “foreach loop”. I think that he wants to change the array and that label? It’s not clear if the OP wants to change the field completely or just have a bunch of fields named “name”?
Anyway, you could still do it with a method like:
function displayFormField($dataArr) {
if (empty($dataArr['size'])) {
$dataArr['size'] = '50';
}
...
echo form_label($dataArr['label'] . ': *', $dataArr['name']).br();
echo form_input($dataArr).br();
echo form_error($dataArr['name']).br();
}
$allArrData[0] = array(
'name' => 'article[name]',
'label' => 'Name:',
'id' => 'name',
'value' => set_value('article[name]', $article->name),
'maxlength' => '255',
'size' => '50',
'style' => 'width:100%',
);
$allArrData[1] = array(
'name' => 'article[name2]',
'label' => 'Name2:',
'id' => 'name2',
'value' => set_value('article[name2]', $article->name2),
'maxlength' => '255',
'size' => '50',
'style' => 'width:100%',
);
foreach ($allArrdata as $arrData) {
displayFormField($arrData);
}