property name textarea name="q1" to name="q20" + php

2

I need the property name="q1" to generate a string, like this:

<textarea name="q1"></textarea>
<textarea name="q2"></textarea>
<textarea name="q3"></textarea> 
.
.
. <!-- // Até -->
<textarea name="q20"></textarea>  

My code below:

forEach($resultado as $y){ ?>
    <label class="fs-field-label fs-anim-upper" for="q1"><?php echo $y['ipco_descr_item']; ?></label> 

    <textarea rows="3" cols="30" class="fs-anim-lower" id="q1" **name="q1"** placeholder="Resposta"> </textarea>
    <div id="resposta"></div>

    <?php 
    }
    
asked by anonymous 03.10.2014 / 18:58

2 answers

4

If you want to repeat this line you can create a loop there and concatenate the number that is being iterated.

forEach($resultado as $y){ ?>
    <label class="fs-field-label fs-anim-upper" for="q1"><?php echo $y['ipco_descr_item']; ?></label> 
    <?php 
    for($i = 1; $ < 21; $i++){
        echo '<textarea rows="3" cols="30" class="fs-anim-lower" id="q1" name="q'.$i.'" placeholder="Resposta"> </textarea>';
    }
    ?>
    <div id="resposta"></div>

    <?php 
    }

The cycle for goes from 1 to 20 and in the property that wants to join the number can make name="q'.$i.'" to join the number to the letter.

    
03.10.2014 / 19:07
4

You'd better do this:

foreach($resultado as $y){ ?>
   <label class="fs-field-label fs-anim-upper" for="q[]"><?php echo $y['ipco_descr_item']; ?></label> 
   <textarea rows="3" cols="30" class="fs-anim-lower" id="q[]" name="q[]" placeholder="Resposta"> </textarea>
  <div id="resposta"></div>
<?php 
}

And get the data like this:

if (isset($_POST['q'])){
  foreach ($_POST['q'] as $key => $value) {
     echo 'field: q'.$key.'<br>';
     echo 'value: '.$value.'<br><hr><br>';
  }
}

But if you find it difficult to do this, you can generate your code like this:

$i = 1;
foreach($resultado as $y){ ?>
   <label class="fs-field-label fs-anim-upper" for="q<?=$i?>"><?php echo $y['ipco_descr_item']; ?></label> 
   <textarea rows="3" cols="30" class="fs-anim-lower" id="q<?=$i?>" name="q<?=$i?>" placeholder="Resposta"> </textarea>
  <div id="resposta"></div>
<?php 
  $i++;
}
    
03.10.2014 / 19:10