Show Json + Mysql + PHP Value

Asked

Viewed 164 times

0

I need to do this, but coming from the database.... I don’t know how to do it...

function decodificar($id)
{
if ($id == '1') {
    return json_encode(
        array(
            'id' => '1',
            'desc' => 'descricao do produto',
            'valor' => '49,90',
            'img' => 'img300x300.png',
            )
        );

} else if ($id == '2') {
    return json_encode(
        array(
            'id' => '2',
            'desc' => 'descricao do produto',
            'valor' => '94,90',
            'img' => 'img300x300.png',
            )
        );

}

I’m doing it but it’s not right

$pdo = db_connect();
$listar = $pdo->prepare("SELECT curso_id, curso_descricao, curso_preco, curso_foto FROM cursos WHERE ativo = 1");
$listar->execute();

function decodificar($id)
{
while ($dados = $listar->fetchAll(PDO::FETCH_ASSOC))
{
if ($id == $dados['curso_id']) {
    return json_encode(
        array(
            'id' => $dados['curso_id'],
            'desc' => $dados['curso_descricao'],
            'valor' => $dados['curso_preco'],
            'img' => $dados['curso_foto'],
            )
        );

    }
} 
  • Read the data and return to the structure you want. A great starting point: https://secure.php.net/manual/en/book.mysql.php

  • i know how to pull. I don’t know how to mount the structure, if and if question

  • Gives a var_dump in the list to see what returns

  • To appear in JSON just pass an echo json_encode($array); it has no secret. What is going wrong?

  • @Vitorpresutti he returns nothing

  • @adventistaam - resultado - Object(Pdostatement)#5 (1) { ["queryString"]=> string(85) "SELECT curso_id, curso_descricao, curso_preco, curso_foto FROM cursos WHERE ativo = 1" }

  • Do not give the Return inside the while, first feed the vector and at the end, after the if echo json_encode

  • Instead of fetchAll put fetch

  • @adventistaam the page goes blank... returns nothing

  • What do you see did? Remember that in this case you are just giving a Return and not echo,

Show 6 more comments

2 answers

1

You can first feed the vector and then return or display the vector

Example:

function decodificar($id)
{
     $arrRetorno = array();
     while ($dados = $listar->fetch(PDO::FETCH_ASSOC))
        {
              if ($id == $dados['curso_id']) {
                  $arrRetorno[] = array(
                     'id' => $dados['curso_id'],
                     'desc' => $dados['curso_descricao'],
                     'valor' => $dados['curso_preco'],
                     'img' => $dados['curso_foto'],
                   );
               } //end of if
         } // end of while
      return json_encode($arrRetorno);
 } 

In that case it will just return.

If you want to display the return you can give a echo.

Replace example :

return json_encode($arrRetorno);

for

echo json_encode($arrRetorno);

0

The last item in the array cannot contain comma. I believe this is the error. Try to remove the comma at the end of 'img' => 'img300x300.png' and tell us what happens.

Browser other questions tagged

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