1
I have a page where shows me text fields to be filled (inputs), containing a "+" in it, in case the user needs more forms to fill in..
The input code of the form that receives the values entered by the user is thus:
Nome completo:<input name="nomeCompleto[]" id="nomeCompleto" type="text">
Taking into account that the "nomeCompleto[]"
is referring to an array, so I made my PHP code receive the values through the $_POST
, in such a way:
if(isset($_POST['nomeCompleto'])){
foreach($_POST["nomeCompleto"] as $nameC)
{
echo " => " . $nameC . "<BR>";
}
Thus the above code returns me such values:
=> João
=> Fernando
=> Jacinto Ades Graça Vindo
=> Lucas
Array ( [0] => João [1] => Fernando [2] => Jacinto Ades Graça Vindo [3] => Lucas )
The values with "=>"
are the values that were typed in the inputs by the user, with php I made those values passed to an array, so they are sorted and identified in such a way. And with the "count"
php, I can count how many items my array has, so it prints the value on the screen "4"
, why there are 4 values(from 0 to 3).
The complete code that returns me the values typed above was this way:
if(isset($_POST['nomeCompleto'])){
foreach($_POST["nomeCompleto"] as $nameC)
{
echo " => " . $nameC . "<BR>";
}
?><br /><?php
print_r(($_POST["nomeCompleto"]));// aqui ele me retorna os valores do array e suas respectivas posições( [0] => João [1] => Fernando...)
?><br /><?php
$count = count($_POST["nomeCompleto"]); // aqui ele realiza a contagem do array via $_POST
printf($count); // aqui ele me retorna o valor "4" que é a quantidade de 'itens' que compõe meu array.
?><br /><?php
print_r($nameC); /*coloquei esse print_r pra ver o que ele me retornaria apenas com a variável $nameC.. e aqui ele me retorna apenas o ultimo valor do array, que seria "Lucas", cá esta um erro */
?><br /><?php
}
The first question is:
In "echo " => " . $nameC . "<BR>";
" it returns me the values of the variable $nameC
in such a way:
=> João
=> Fernando
=> Jacinto Ades Graça Vindo
=> Lucas
But why when I give one "print_r($nameC);"
he returns only "Lucas"
? and not all other names. (regardless if it is printf
or print_r
or echo
)
The question is, how can I get such values separately within a loop? Following such logic in the case:
while($i=0;$i<$count;$i++){
printf($nameC[$i]);
}
Obs.: the variable "$nameC"
is the variable I set up to be able to allocate the array values (in which case the array comes from a web page, using the $_POST
i pulled the values and assign such to that variable
Note: It necessarily needs to be while because it governs a whole script repeat..
The problem with such a question is, in the logic of the loop I posted, it would return me something like:
1 = L
2 = u
3 = c
4 = a
And in case I didn’t really want him to read the array this way, why is it just reading the last item of the array, value by value (letter by letter in the case) and showing me on the screen at their respective positions.
In case I wanted it to be printed in such a way:
1 = João
2 = Fernando
3 = Jacinto....
4 = Lucas
Using the loop, what would be the logic for this to be accomplished?
can be done with a for instead of a while?
– Jakson Fischer
Hello Lucas, first welcome to Sopt. As for your question, I know you tried to represent it in the best possible way, but always try to post your piece of code that encompasses the whole solution to facilitate the understanding of everyone. As to need if a
while
, this structure that is using for thewhile
is equivalent to there is afor
orforeach
, then it would make no difference to use one or the other, even by the context afor
orforeach
would be more appropriate.– Vinicius Gabriel
I believe you can yes, I can try to change the script to fit the code using for, what would be the idea?
– Lucas
All right, I’m going to do an edit on the topic itself, adding a little more code and details so I was easier to help myself. Thankful!
– Lucas
@Lucas, I posted an answer, see if this helps you please, if so, please mark the question as correct.
– Jakson Fischer