I can not check if an array is empty or null

Posted on

Question :

I need to display a message whenever my array is empty or null, but I can not. I call the JS function in PHP by passing an array with json_encode. It works perfectly, but it does not 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

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>";

    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>";


Answer :

As you mentioned

Leave a Reply

Your email address will not be published. Required fields are marked *