Include comma in results from the database

Asked

Viewed 19 times

1

I have a table where the structure is as follows:

Idestoques | Idprodutos | Tamanho | Cores | Estoques

In it storing the sizes, colors and stocks of each product. However I need to bring from the database the following information:

Tamanhos: P,M, G    
Cores: Azul, Amarelo, Vermelho

I managed to bring only the sizes as follows:

$sqlEstoques = mysqli_query($this->conexao,"SELECT * FROM loja_estoques WHERE IDProdutos = '".$jmBusca->IDProdutos."';");

if(mysqli_num_rows($sqlEstoques) > 0){
   $resultado = array();
   while($jmEstoques = mysqli_fetch_array($sqlEstoques)){
         $resultado[] = $jmEstoques["Tamanho"];
   }
    $visualizar .= "<div class='col-lg-12' style='font-size: 14px; text-align: left'><strong>Tamanhos:</strong>". implode(",",$resultado);
    $visualizar .= "</div>";
}

Saída: Tamanho: P,M,G

How would I bring the colors too?

Cores: Azul, Amarelo, Vermelho
  • The same way you did with the sizes, saving them in an array and then implode. Some difficulty you found and did not put in the question? You can [Dit] to be clearer :)

  • Hello gmsantos. Sorry, I could not understand very well. Could you give me an example?

  • barter $jmEstoques["Tamanho"]; color.

  • Right. I’ve already done this, but I couldn’t figure out how to save in an array and then play to an implode to get the size and colors coming from the comic. I have to create 02 querys and 02 ties?

  • itere a fez, save two variables.

  • Sorry gmsantos, I really couldn’t understand it. It would be like to give me an example based on my code?

Show 1 more comment

1 answer

1


While you iterate, save the colors in another variable:

$sqlEstoques = mysqli_query($this->conexao,"SELECT * FROM loja_estoques WHERE IDProdutos = '".$jmBusca->IDProdutos."';");

if(mysqli_num_rows($sqlEstoques) > 0){
   $tamanhos= array();
   $cores= array();
   while($jmEstoques = mysqli_fetch_array($sqlEstoques)){
       $tamanhos[] = $jmEstoques["Tamanho"];
       $cores[] = $jmEstoques["Cores"];
   }
   $visualizar .= "<div class='col-lg-12' style='font-size: 14px; text-align: left'><strong>Tamanhos:</strong>". implode(",",$tamanhos)."</div>";
   $visualizar .= "<div class='col-lg-12' style='font-size: 14px; text-align: left'><strong>Cores:</strong>". implode(",",$cores)."</div>";
}

I recommend that you study the programming logic part a little, I see that some basic concepts are missing. Read a little also some questions about security, mainly how to avoid SQL Injection in PHP.

  • Perfect gmsantos. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.