Home > Software engineering >  Creating Dynamic Input fields with json data
Creating Dynamic Input fields with json data

Time:03-03

I am creating dynamic multi fields with PHP and JSON data. I am facing issues like 1st the foreach is not showing the data with the same keys like "text" field it is showing the one only in foreach loop. it should show.

1: input field with data (type text)
2: Textarea field with data
3: inputfield with data (type text)

but it is only showing 2 fields when I am running the code. Where I am mistaking? And how to show the fields with types if the type is text then show the <input type="text" with data> and if it is textarea then show <textarea with data></textarea> please some one can help me out with this?

$json_test_data = '
{
    "section_1":
    [
        {
            "text":{"class":"mb-20","name":"text","value":"value 1","placeholder":"Section placeholder"}
        },
        {
            "textarea":{"class":"mb-20","name":"section_content","value":"Section content 1","placeholder":"Section Content"}
        },
        {
             "text":{"class":"mb-20","name":"value_2","value":"value 2 value","placeholder":"value 2 placeholder"}
        }
    ]
}';
$json_test = json_decode($json_test_data, true);
$input = '';
if(is_array($json_test))
{
    foreach ($json_test as $section => $section_data)
    {
        echo '<div >';
        echo '<div ><h4>'.$section.' Data</h4></div>';
        foreach($section_data as $section_fields => $fields_data)
        {
            foreach($fields_data as $field_key => $field_value)
            {
                if($field_key == 'text')
                {
                    foreach($field_value as $field_keyy => $field_valuee)
                    {   
                        $input .= $field_keyy.'="'.$field_valuee.'" ';
                    }
                    echo '<input '.$input.'>';
                }
                
            }
        }
        echo '</div>';
    }
}

CodePudding user response:

As brombeer stated you can not use the same index names twice.

$json_test_data = '{
    "section_1":[
        {
            "text":{"class":"mb-20","name":"text","value":"value 1","placeholder":"Section placeholder"}
        },
        {
            "textarea":{"class":"mb-20","name":"section_content","value":"Section content 1","placeholder":"Section Content"}
        },
        {
            "text":{"class":"mb-20","name":"value_2","value":"value 2 value","placeholder":"value 2 placeholder"}
        }
    ]
}';

$json_test = json_decode($json_test_data, true);

foreach ($json_test as $section => $section_data) 
{
    echo '<div >';
    echo '<div ><h4>'.$section.' Data</h4></div>';

    foreach ($section_data as $section_fields)
    {    
        foreach ($section_fields as $field_type => $field_data) {

            $input = ''; // resetting $input here so it doesnt contain any information from previous row 
            foreach ($field_data as $field_key => $field_value) {
                $input .= $field_key.'="'.$field_value.'" ';
            }

            if ($field_type == 'textarea') {
                echo '<textarea '.$input.'>';
                echo $field_data['value'] ?? '';
                echo '</textarea>';
            } else { 
                echo '<input '.$input.'>';
            }
        }
    }

    echo '</div>';
}

since HTML textarea tags are a little different I wrapped them in a if condition.

CodePudding user response:

Having two keys "text" will leave you with only the last one entered in your decoded array. Make your "section_1" an array of objects:

<?php
$json_test_data = '{
    "section_1":[
        {
            "text":{"class":"mb-20","name":"text","value":"value 1","placeholder":"Section placeholder"}
        },
        {
            "textarea":{"class":"mb-20","name":"section_content","value":"Section content 1","placeholder":"Section Content"}
        },
        {
            "text":{"class":"mb-20","name":"value_2","value":"value 2 value","placeholder":"value 2 placeholder"}
        }
    ]
}';

Adjust your foreachs accordingly.

CodePudding user response:

Try this code, it will works

<?php $json_test_data = '
            {
                "section_1":
                [
                    {
                        "text":{"class":"mb-20","name":"text","value":"value 1","placeholder":"Section placeholder"}
                    },
                    {
                        "textarea":{"class":"mb-20","name":"section_content","value":"Section content 1","placeholder":"Section Content"}
                    },
                    {
                        "text":{"class":"mb-20","name":"value_2","value":"value 2 value","placeholder":"value 2 placeholder"}
                    }
                ]
            }';
            $json_test = json_decode($json_test_data, true);
            $input = '';
            if(is_array($json_test))
            {
                foreach ($json_test as $section => $section_data)
                {
                    echo '<div >';
                    echo '<div ><h4>'.$section.' Data</h4></div>';
                    foreach($section_data as $section_fields => $fields_data)
                    {
                        foreach($fields_data as $field_key => $field_value)
                        {
                            if($field_key == 'text')
                            {
                                foreach($field_value as $field_keyy => $field_valuee)
                                {   
                                    $input .= $field_keyy.'="'.$field_valuee.'" ';
                                }
                                echo '<input '.$input.'>';
                            } else {

                              foreach($field_value as $field_keyy => $field_valuee)
                              {
                                if($field_keyy == 'value') {
                                  $value = $field_valuee;
                                } else 
                                {   
                                    $input .= $field_keyy.'="'.$field_valuee.'" ';
                                }
                                
                              }
                              echo '<textarea '.$input.'>'.$value.'</textarea>';
                            }
                            
                        }
                    }
                    echo '</div>';
                }
            }
  • Related