About writing variables in PHP

1

I have a form where I ask the amount of inputs they need to add information. This is the form

When generating the code to fill in the inputs, I'm going to notice that I have a programming error that I do not know how to solve, since my variables are fixed and I write them over.

<ul>
                        <?php /*Genera lista de prósitos para cada materia*/
                        if ($no_proposito1 == 1) {
                            printf('<li><input type="text" name="propositos1" placeholder="Descripción del proposito"></li>');
                        } else {
                            for ($i=1; $i <= $no_proposito1; $i++) {
                                printf('<li><input type="text" name="propositos1" placeholder="Descripción del proposito"></li>');
                            }

                        }

                         ?>
                    </ul>

I would like to know if someone can support me in how to dynamically generate variables according to the user input, my limit to generate variables are 5.

PS: I am new programming and surely there is a better solution than mine.

    
asked by Marcos Rugerio 15.10.2018 в 20:58
source

2 answers

0

If you're working with an array, you can build dynamic content like that.

Let's see several examples:

The array has only one element:

$no_proposito=array(1);
$html="<ul>";
$i=1;
foreach ($no_proposito as $item)
{
    $html.="<li><input type=\"text\" name=\"propositos$i\" placeholder=\"Descripción proposito $i\"></li>";
/*Salimos cuando llegue a 5*/
if ($i++ == 5) break;

}

$html.="</ul>";

echo $html;

Exit:

<ul>
  <li><input type="text" name="propositos1" placeholder="Descripción proposito 1"></li>
</ul>

The array has several elements and we need the first five:

$no_proposito=array(1,2,3,4,5,6,7,8);
$html="<ul>";
$i=1;
foreach ($no_proposito as $item)
{
    $html.="<li><input type=\"text\" name=\"propositos$i\" placeholder=\"Descripción proposito $i\"></li>";
    if ($i++ == 5) break;

}

$html.="</ul>";

echo $html;

Exit:

<ul>
  <li><input type="text" name="propositos1" placeholder="Descripción proposito 1"></li>
  <li><input type="text" name="propositos2" placeholder="Descripción proposito 2"></li>
  <li><input type="text" name="propositos3" placeholder="Descripción proposito 3"></li>
  <li><input type="text" name="propositos4" placeholder="Descripción proposito 4"></li>
  <li><input type="text" name="propositos5" placeholder="Descripción proposito 5"></li>
</ul>

You can also use the information inside the array

You only change this line:

$html.="<li><input type=\"text\" name=\"propositos$item\" placeholder=\"Descripción proposito $item\"></li>";

using $item instead of $i . There the data in the array will be used.

    
answered by 15.10.2018 / 21:18
source
0

I would suggest that you use arrays in the html. You should be able to generate this:

<ul>
  <li><input type="text" name="propositos[]" placeholder="Descripción proposito 1"></li>
  <li><input type="text" name="propositos[]" placeholder="Descripción proposito 2"></li>
  <li><input type="text" name="propositos[]" placeholder="Descripción proposito 3"></li>
  <li><input type="text" name="propositos[]" placeholder="Descripción proposito 4"></li>
  <li><input type="text" name="propositos[]" placeholder="Descripción proposito 5"></li>
</ul>

The bracket will allow you to send the "purposes" as an array, so that later in your php you could read them like this:

$propositos = $_POST['propositos'];

and if you do a print_f ($ propositos) you will get an array:

array() [
    0  => string()  ["proposito ejemplo 1"],
    1  => string()  ["proposito ejemplo 2"],
    2  => string()  null,
    3  => string()  ["proposito ejemplo 3"],
    4  => string()  ["proposito ejemplo 4"],

]

The null I give you as an example, because I do not know if you consider the fact of allowing empty proposals. If so, then this case could come to you.

    
answered by 15.10.2018 в 21:36