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
– Andre Mesquita
i know how to pull. I don’t know how to mount the structure, if and if question
– Betinho Silva
Gives a var_dump in the list to see what returns
– adventistaam
To appear in JSON just pass an echo json_encode($array); it has no secret. What is going wrong?
– Vitor Presutti
@Vitorpresutti he returns nothing
– Betinho Silva
@adventistaam - resultado - Object(Pdostatement)#5 (1) { ["queryString"]=> string(85) "SELECT curso_id, curso_descricao, curso_preco, curso_foto FROM cursos WHERE ativo = 1" }
– Betinho Silva
Do not give the Return inside the while, first feed the vector and at the end, after the if
echo json_encode
– adventistaam
Instead of fetchAll put fetch
– adventistaam
@adventistaam the page goes blank... returns nothing
– Betinho Silva
What do you see did? Remember that in this case you are just giving a Return and not echo,
– adventistaam
Let’s go continue this discussion in chat.
– Betinho Silva