13
When I get the names and add them to a variable, I can’t separate them with a comma normally.
- has to be by comma and period at the end
- I can’t put each name in line break using
It’s hard to explain, but here’s an example:
while ($listintegrantes = mysqli_fetch_array($query1)) {
$integrantes = "$integrantes" . $listintegrantes['nome'] . ",";
}
that is, this will show so:
John, Cyrax, john2,
But I want you to stay:
John, Cyrax and john2.
How is it possible to do this?
-SOLUTION
Follow the resolution of the problem posted above, all resolutions are correct, I adapted to my problem to return me my correct objective.
$query1 = mysqli_query($conexao, $sql1);
$integrantes = "";
$i = mysqli_num_rows($query1);
$x = 1;
while ($listintegrantes = mysqli_fetch_array($query1)) {
$integrantes = $integrantes . $listintegrantes['nome'];
if ($x < $i - 1) {//antes do penultimo
$integrantes = $integrantes . ", ";
}elseif($x == $i-1){//penultimo
$integrantes = $integrantes . " e ";
}elseif($x == $i){//ultimo
$integrantes = $integrantes . ".";
}
$x++;
}
follows below the above algorithm feedback: Cyrax, John, Serana and Smigol.
when there is only one member record: Serana.
You can iterate the variable members backwards and when you find the comma, switch to
.
and continue, when the next-to-last comma is found, to be replaced bye
.– ptkato
Can use a
rtrim($integrantes, ',')
at the end of the while and then only concatenate a point at the end. One idea only.– Marcelo Diniz
I removed the solution from your question, their place is in the answers.
– Rafael Almeida
It is better to create an answer with your solution, the question space is only for question :P. The site works different from a forum see the differences in tour is a quick guide :)
– rray
Your solution seems to have been adapted based on the @Jorge B.’s response, you can mark his response as correct. Votes do not influence the choice.
– Papa Charlie
Yes, thank you, I’m adapting on this site yet, I don’t even know what it’s like to choose the answer yet .... rs, but I’ll see here. I believe it’s green.
– Murilo Man
Just choose the answer that suits your case best, there is no mystery rs. Take a look at the help later to get any questions. And problems can be solved in Meta.
– Papa Charlie