1
Guys, I have this code that pulls info from the server
try {
$conecta = new PDO("mysql:host=$servidor;dbname=$banco", $usuario, $senha);
$consulta = $conecta->prepare('SELECT * FROM tb02_cardapiosem WHERE tb02_diasemana=' . $data);
$consulta->execute(array());
$resultadoDaConsulta = $consulta->fetchAll();
$StringJson = "[";
if (count($resultadoDaConsulta)) {
foreach ($resultadoDaConsulta as $registro) {
if ($StringJson != "[") {
$StringJson .= ",";
}
$StringJson .= '{"tb02_lanchedia":"' . $registro[tb02_lanchedia] . '",';
$StringJson .= '"tb02_almoco":"' . $registro[tb02_almoco] . '",';
$StringJson .= '"tb02_cafe":"' . $registro[tb02_cafe] . '",';
$StringJson .= '"tb02_lanchenoite":"' . $registro[tb02_lanchenoite] . '",';
$StringJson .= '"tb02_diasemana":"' . $registro[tb02_diasemana] . '"}';
}
echo $StringJson . "]"; // Exibe o vettor JSON
}
}
catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage(); // opcional, apenas para teste
}
Then I have this code that plays the information found in a div
function ConectaServidor(response) {
var dados = JSON.parse(response); //faz a conversão do texto da WEB para JSON
var i;
var conteudo = "";
var conteudo2 = "";
var conteudo3 = "";
var conteudo4 = "";
for (i = 0; i < dados.length; i++) //dados.length retorna o tamanho do vetor.
{
conteudo += dados[i].tb02_cafe;
conteudo2 += dados[i].tb02_lanchedia;
conteudo3 += dados[i].tb02_almoco;
conteudo4 += dados[i].tb02_lanchenoite;
}
document.getElementById("cafe1").innerHTML = conteudo;
document.getElementById("lanche1").innerHTML = conteudo2;
document.getElementById("almoco1").innerHTML = conteudo3;
document.getElementById("lanchen1").innerHTML = conteudo4;
}
However it is not all the dates that are registered and would like when a date is not found, he put the Div’s innerHTML as "There is no record!". How to do?
Question: Why not use the function
json_encode
to convert your array for JSON?– Woss