I can not check if an array is empty or null

3

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
        $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);
}
    
asked by anonymous 27.06.2015 / 21:15

2 answers

3
0

Your json_encode is probably returning [] , as you can see here: link

Check the javascript:

if (query == '[]') 
    imgs.innerHTML = "<h4>Ainda não ha galerias nesta seção.</h4>";

If you are using jQuery there is jQuery.isEmptyObject

window.onload = function () {

var json = {};

if ($.isEmptyObject(json))
  alert('vazio');
  
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
27.06.2015 / 21:40