开发者

retrieve values from an array

hello coders I have a database in .csv format.To retrive data I used csv parser from where I got the result in array as

 Array
(
    [fieldset_name] => Personal Details
    [field_name] => applicant_name
    [field_label] => Your Name
    [field_type] => text
    [css_开发者_开发技巧classes] => required
    [minlength] => 4
    [maxlength] => 10
    [default_value] => 
    [help_text] => 
)

        Array
(
    [fieldset_name] => Personal Details
    [field_name] => applicant_address
    [field_label] => Address
    [field_type] => textarea
    [css_classes] => required
    [minlength] => 4
    [maxlength] => 10
    [default_value] => 
    [help_text] => 
)

Now I used this code for making this array as like a html form

<?php foreach ( $form_field as $key => $v1 ) {
         $fieldset_name = $v1['fieldset_name'];
          $field_name = $v1['field_name'];
          $field_label = $v1['field_label'];
         $field_type = $v1['field_type'];
          $css_classes = $v1['css_classes'];
          $minlength = $v1['minlength'];
          $maxlength = $v1['maxlength'];
          $default_value = $v1['default_value'];
 <label for "<?php echo $field_name; ?>"></label><?php echo $field_label; ?><input type="<?php echo $field_type;?>" id = "<?php echo $css_classes; ?>"/>

        <?php  } 

          ?>

From where I got the result as

<label for "applicant_name"></label>Your Name<input type="text" id = "required"/> 
<label for "city"></label>City<input type="text" id = "required"/> 

Now I want to set the for both values .For first I want to set the value like

<legend>Your Name</legend>
<label for "applicant_name"></label>Your Name<input type="text" id = "required"/> 
<legend>Address</legend>
<label for "city"></label>City<input type="text" id = "required"/>

So please tell me how to do this in foreach loop or in if..else condition as I have a big form like this so I can't set the value manually.


The id preoperty should have a unique value, as this is what the for attribute of labels is pointing at.

You write "Name" and City" to your HTML code, but they can not be generated from your array.

The closest you can get to your desired result is with:

foreach ($form_field as $f) {
    printf ('<label for="%s">%s</label><input type="text" class="%s" name="%s" value="%s"/>',
        $f['field_name'], 
        $f['field_label'],
        $f['css_classes'],
        $f['field_name'],
        $f['default_value']
    );
}

Which will produce something like:

<label for="applicant_name">Your Name</label><input type="text" class="required" name="applicant_name" value=""/>
<label for="applicant_address">Address</label><input type="text" class="required" name="applicant_address" value=""/>


You could use regex - something like:

$legend = preg_replace('/(<.+label>(.+?)<input)/g',"<legend>$2</legend>\r\n$1",$source)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜