2

I am very new to php and I am trying to learn php by myself. I have an array

<?php 

$age=array("A"=>"test",
    "Arating"=>"37",
    "B"=>"test2",
    "Brating"=>"40",
    "c"=>"test3",
    "crating"=>"41",
    "D"=>"test4",
    "Drating"=>"42");


?>

I would like to create a form from array like Expected output:

 <html>
        <form action="" name="test" method="post"/>
        <input name="A" value="test" id="test"/>
        <textarea rows="4" cols="50" name="Arating" >
    37
    </textarea>
        <br>
      <input name="B" value="test2" id="test2"/>
        <textarea rows="4" cols="50" name="Brating" >
    40
    </textarea>  
      <br>
      <input name="C" value="test3" id="test3"/>
        <textarea rows="4" cols="50" name="Crating" >
   41
    </textarea>  
      <br>
      <input name="D" value="test4" id="test4"/>
        <textarea rows="4" cols="50" name="Drating" >
    42
    </textarea>  
    </form>
    </html>
    <html>

here A B C D will be input type values and textarea should always be Arating,Brating,Crating,Drating

I tried:

    <form action="" name="test" method="post"/>
        <?php
    foreach($age as $key => $value){?>
        <input name="<?php echo $key ?>" value="<?php echo $value ?>" id="test"/>
        <textarea rows="4" cols="50" name="Arating" >
    <?php echo $value ?>
    </textarea>
    <?php } ?>

Input name always:A,B,C,D Textarea:Arating,Brating,Crating,Drating output is totally wrong.I am totally new to php

7
  • 1
    what error you are getting ? Commented Jul 12, 2017 at 12:57
  • @PrafullaKumarSahu:i dint get form what i expected..here all values echo both text area and input field..i need to get A B C D to input field and 37 40 ...textarea please help me Commented Jul 12, 2017 at 13:00
  • Are you able to restructure your array as currently it is not formatted in a way that allows you to do this? Commented Jul 12, 2017 at 13:00
  • @PeterFeatherstone:how can i do that?please help me Commented Jul 12, 2017 at 13:01
  • 1
    Why have to take value for input text as dynamic but for textarea as static? Commented Jul 12, 2017 at 13:01

4 Answers 4

3

try this: As per your array

<?php
$age=array("A"=>"test",
    "Arating"=>"37",
    "B"=>"test2",
    "Brating"=>"40",
    "c"=>"test3",
    "crating"=>"37",
    "D"=>"test4",
    "Drating"=>"40");
?>

 <form action="" name="test" method="post">
    <?php
    $i=0;
    foreach($age as $key => $value)
    {
        if($i%2==0)
        {?>
            <input name="<?php echo $key ?>" value="<?php echo $value ?>" id="test"/>
        <?php   
        }
        else
        {?>
            <textarea rows="4" cols="50" name="<?php echo $key ?>" ><?php echo $value ?> </textarea>
        <?php }    
        $i++;
    } ?>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

what's the sense of using foreach with incrementation? Why don't you use simple for?
Thanks for your reply @mayank
2

As it is your array is not optimised to do this. Technically you could do some checks using the modulos operator % and get the 1st and 2nd options combined with if statements and the like but this gets messy and if you ever have items out of order or want to change or add a new option in future then you will be in a whole word of pain.

It is better to re-arrange your array into a multi-dimensional array and then loop over it and access each item by it's key name. It will make your code much more understandable and maintainable in the long run too.

The code below should work better for you:

<?php

$ages = array(
  "A" => [
    "val" => "test",
    "name" => "Arating",
    "num" => "37"
  ],
  "B" => [
    "val" => "test2",
    "name" => "Brating",
    "num" => "40"
  ],
  "C" => [
    "val" => "test3",
    "name" => "crating",
    "num" => "37"
  ],
  "D" => [
    "val" => "test4",
    "name" => "Drating",
    "num" => "40"
  ]
);

foreach($ages as $key => $age){ ?>
   <input name="<?php echo $key ?>" value="<?php echo $age['val'] ?>" id="<?php echo $age['val'] ?>" />
   <textarea rows="4" cols="50" name="<?php echo $age['name'] ?>"><?php echo $age['num'] ?></textarea>
<?php } ?>

2 Comments

:thankyeww..Without rearrange this array..can i get answer?is my way is wrong?
There are other answers below for doing it that way but they are horrible in my opinion. I'm afraid they are non-maintainable nor scalable and un-necessarily specific and complex... Always improve your data structures first...
2

Try like that.

<form action="" name="test" method="post"/>
<?php
    foreach($age as $key => $value) { 
     if(!is_numeric($value) ) {
?>
 <input name="<?php echo $key ?>" value="<?php echo $value ?>" 
id="test"/>
<?php } else { ?>
    <textarea rows="4" cols="50" name="<?php echo $key ?>" >
<?php echo $value ?>
    </textarea>
<?php } } ?>

5 Comments

What if in future a non numeric value is used such as blank or a string representation for unknown such as NA?
As per current array and requirement this logic is fine if array is change in future then offcource logic will change.
In other words, This approach is not maintainable nor scalable?
If you need it to be more scalable, recreate your array to something like this $age = [ [ 'name' => 'A', 'value' => 'test', 'type' => 'textInput' ], [ 'name' => 'Arating', 'value' => '37', 'type' => 'textarea' ], ];
@PeterFeatherstone As per current array structure only this possibility or you have to pass another $count to check. On these array structure there are no any scalable way.
1
<?php


$age = [
    "A"       => "test",
    "Arating" => "37",
    "B"       => "test2",
    "Brating" => "40",
    "c"       => "test3",
    "crating" => "41",
    "D"       => "test4",
    "Drating" => "42"
];


?>
<form action="" name="test" method="post"/>
<?php
foreach ($age as $key => $value) {
    if (strlen($key) == 1) { ?>

        <input name="<?php echo $key ?>" value="<?php echo $value ?>" id="<?=$value;?>"/>

    <?php } else { ?>

        <textarea rows="4" cols="50" name="<?php echo $key ?>"><?php echo $value ?></textarea>
    <?php } ?>



<?php } ?>

Another option if you have AA for a key

<?php
$count = 1;
foreach ($age as $key => $value) {
    if ($count == 1) { ?>

        <input name="<?php echo $key ?>" value="<?php echo $value ?>" id="<?= $value; ?>"/>

    <?php } elseif($count==2) { ?>

        <textarea rows="4" cols="50" name="<?php echo $key ?>"><?php echo $value ?></textarea>
    <?php } ?>

    <?php
    if ($count == 2) {
        $count = 0;
    }
    $count++;

    ?>
<?php } ?>

A more scalable solution,

<?php


$age = [

    [
        'name'  => 'A',
        'value' => 'test',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'Arating',
        'value' => '37',
        'type'  => 'textarea'
    ],
    [
        'name'  => 'B',
        'value' => 'test2',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'Brating',
        'value' => '40',
        'type'  => 'textarea'
    ],
    [
        'name'  => 'c',
        'value' => 'test3',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'crating',
        'value' => '41',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'D',
        'value' => 'test4',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'Drating',
        'value' => '42',
        'type'  => 'textInput'
    ],

];

?>
<form action="" name="test" method="post"/>
<?php

foreach ($age as $key => $value) {
    if ($value['type'] == 'textInput') { ?>

        <input name="<?php echo $value['name'] ?>" value="<?php echo $value['value'] ?>" id="<?= $value['value']; ?>"/>

    <?php } elseif ($value['type'] == 'textarea') { ?>

        <textarea rows="4" cols="50" name="<?php echo $value['name'] ?>"><?php echo $value['value'] ?></textarea>
    <?php } ?>


<?php } ?>

3 Comments

What happens if he has a key of AA in future?
All you need to do is check the length of the key to determine if you want to display a textarea or input field. This will work as long as your key's stay 1 character for text fields.
If one of your keys has AA, you can always add a $count variable; $count=1; foreach... if(count == 2){ count++; endforeach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.