3
I need to display a message whenever my array is empty or null, but I’m not getting it. I call the JS function in PHP, passing an array with json_encode. It works perfectly, but it doesn’t check if the array is empty.
php file:
$nGals = $conexao->consultarDados("select gal from portfolio where theme = '{$tem}' group by gal");
$galArr = array();
foreach($nGals as $rows){
array_push($galArr, $rows);
}
$countArr = count($galArr);
$x = 1;
while($x <= $countArr){
$parametroGal = array();
$galeria = $conexao->consultarDados("select * from portfolio where gal = '{$x}' and theme = '{$tem}' limit 4");
foreach($galeria as $rows){
array_push($parametroGal, $rows['nome']);
}
$jsonGal = json_encode($parametroGal);
echo "galerias(".$jsonGal.",".$x.");"; //passo o array aqui
$x++;
}
The function is in a separate JS file:
function galerias(query,x){
var imgs = document.querySelector("#gallery");
var theme = location.search.split("?tem=")[1];
//verifica se ha imagens na determinada galeria
//porem o if nao funciona, apenas o else funciona quando o array nao e vazio
if(!query || query.length == 0 || query == null){
imgs.innerHTML = "<h4>Ainda não ha galerias nesta seção.</h4>";
}else{
imgs.innerHTML += "<div class='row'>";
imgs.innerHTML += "<div class='eight columns'>";
imgs.innerHTML += "<h4>Galeria "+x+"</h4>"
for(var i = 0; i < query.length; i++){
imgs.innerHTML += '<img src="img/'+query[i]+'" class="imgs-galeria""/>';
}
imgs.innerHTML += "</div>";
imgs.innerHTML += "</div>";
imgs.innerHTML += "<a class='row' href='pics.php?gal="+x+"=&oth="+theme+"'><div class='twelve columns link'><p>Veja mais</p></div>";
}
console.log(query.length);
}
This code is like this in PHP? with JS and PHP mixed?
– Sergio
I don’t understand either
– Felipe Douradinho
@Sergio Editei the question to be easier to understand.
– anuseranother
@anuseranother ok, that makes more sense. And where is the JSONP code?
– Sergio
not just put a
if (!query) return;
no js? or json_encode error?– Maicon Carraro
@Sergio didn’t make a json code, just converted the array in php to json, it was enough for the situation, works perfectly, just checking that not...
– anuseranother
@Maiconcarraro then man, not from direct error but does not work the way it is now.
– anuseranother
@anuseranother ok so you need to use JSON.parse no? what gives you
console.log(typeof query, typeof x);
?– Sergio
@Sergio in the galleries that have images in the array, returns me "Object", "number", in the galleries that have the empty array does not return me anything.
– anuseranother
@anuseranother as appears this line
echo "galerias(".$jsonGal.",".$x.");"; //passo o array aqui
in HTML?– Sergio
@Sergio where I own a gallery:
galerias(["foto1_1.jpg","foto1_2.jpg","foto1_3.jpg","foto1_4.jpg"],1);
, where I don’t own the line or appear. the <script> tag is left without content– anuseranother
@anuseranother ok, and in those that gives empty what gives
echo $countArr;
in PHP before ofwhile
? I don’t think he’s even getting intowhile
in PHP...– Sergio
@Sergio is not, as I use only to count the number of galleries, I need to leave him out to serve as a reference to the while counter. echo returns me 0 and a var_dump returns me int(0);
– anuseranother
@anuseranother then before the while you should have something like
if ($countArr == 0) echo "galerias([], 0);"; die();
nay?– Sergio
Sergio no, because there is a
QUERY
in the middle ofWHILE
– Felipe Douradinho
@Felipedinho but this while is never run if the
$countArr
gives zero– Sergio
No problem Sergio, if it is not run, the
JS
it doesn’t need changing...but ifcountArr
he may have resulted byquery
– Felipe Douradinho