I cannot check whether an array is empty or null

Asked

Viewed 1,728 times

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);
}

  • 1

    This code is like this in PHP? with JS and PHP mixed?

  • 1

    I don’t understand either

  • @Sergio Editei the question to be easier to understand.

  • @anuseranother ok, that makes more sense. And where is the JSONP code?

  • not just put a if (!query) return; no js? or json_encode error?

  • @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...

  • @Maiconcarraro then man, not from direct error but does not work the way it is now.

  • @anuseranother ok so you need to use JSON.parse no? what gives you console.log(typeof query, typeof x);?

  • @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 as appears this line echo "galerias(".$jsonGal.",".$x.");"; //passo o array aqui in HTML?

  • @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 ok, and in those that gives empty what gives echo $countArr; in PHP before of while? I don’t think he’s even getting into while in PHP...

  • @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 then before the while you should have something like if ($countArr == 0) echo "galerias([], 0);"; die(); nay?

  • Sergio no, because there is a QUERY in the middle of WHILE

  • @Felipedinho but this while is never run if the $countArr gives zero

  • No problem Sergio, if it is not run, the JS it doesn’t need changing...but if countArr he may have resulted by query

Show 12 more comments

2 answers

3


As you mentioned in the comments $countArr zero when there are no galleries - "Echo returns to me 0 and a var_dump returns to me int(0)". That means that the while is never run and that

echo "galerias(".$jsonGal.",".$x.");"; //passo o array aqui

is never run.

So I suggest you create an alternative code for these cases... something like:

if ($countArr == 0) echo "galerias([], 0);"; 
else while($x <= $countArr){ // fazer o while

To make the echo empty content.

  • It would only solve the first part of the problem. Inside the while, there may also be the possibility of parametroGal never be filled.

  • @Felipedouradinho as so "inside the while"? that while never happens in the case where $countArr is 0.

  • 1

    @Sergio Thanks Sergio, it was the most effective response and that identified my problem. I thank Sergio and all.

0

Probably his json_encode is returning [], as you can see here: https://ideone.com/BO6SsT

Do a javascript check:

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

If you are using jQuery there is a 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>

  • Still nothing... as if ignoring the if completely

  • @anuseranother if you put a console.log(query) what appears on the console?

  • My json appears where the array has values, where nothing appears.

  • @anuseranother put another alternative

  • 2

    I’ll take Sergio’s answer because it was the one that fit me best, but thank you for your help !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.