Having a hard time to iterate over an array of objects

Hello all,

I can’t var_dump and I don’t know how to use netbeans debugger (yet).
I’m wondering if someone could help me out.

I want to list all names that are returned on a query, I can only see 1.

The problem can be either on the server side way of processing the results from the database, OR, on the js part. :frowning: I suppose it’s the first case, but without strong reasons for doing so.

The structure that I have now works for displaying one name.
I want it to work with several names display, when that’s the case.

The js part receives the user input:

function formatar(result) {
    return result.nome;

$(document).ready(function() {

    $('#nomeDominio').autocomplete("searchNome.php", {
        cacheLength: 1,
        minChars: 3,
        parse: function(data)
            return $.map(eval('('+data+')'), function(result) {

                return {

                    data: result,
                    value: result.nome,
                    result: result.nome

        formatItem: function(result) {
            return formatar(result);


And server side part calls a method on the controller, and encodes
the returned array to be consumed by the javascript:

$keyword = addslashes($_GET["q"]);

$comandos = new ComandoController();

$arr = $comandos->recebeNomeDominios($keyword);

    echo json_encode($arr);

recebeNomeDominios looks like this:

public function recebeNomeDominios($keyword)
   $DominioDao = new DominioDao();
   $objecto = $DominioDao->recebeNomeDominios($keyword);
   return $this->jsonArray($objecto);

The object iterated like so:

private function jsonArray($objecto)
    $json = array();
    if(isset($objecto) && !empty($objecto))
       foreach($objecto as $obj)
          $json[]["nome"] = $obj;

  return $json;

I do believe the issue is here, this works for an object returned by fetch, BUT not with an ARRAY of objects returned by fetchAll.

Finally, I do receive the database returned data, like so:

public function recebeNomeDominios($keyword)
     $stmt = $this->_dbh->prepare("SELECT d.nomeDominio FROM dominio d WHERE d.nomeDominio LIKE '%".$keyword."%'");

     // $stmt->bindParam(1, $nome, PDO::PARAM_STR, 255);

     $resultado = $stmt->fetch(PDO::FETCH_OBJ);

     return $resultado;        

This works (the list appear) if we have only 1 returned result, if I change from fetch to fetchAll I got nothing displayed.

Hope I can have your help, at least something to make me moving…


I can’t var_dump, nothings get’s displayed on the browser, because we are calling this using an ajax call. If I load directly the controller, I get a white space, no no var_dump there as well…


With fetch, the $arr returns:

array(1) { [0]=>  array(1) { ["nome"]=>  string(7) "aaaa.pt" } } [{"nome":"aaaa.pt"}]

And it’s well parsed by the js.

With fetchAll, the $arr returns: (an array of objects):

array(65) { [0]=>  array(1) { ["nome"]=>  object(stdClass)#7 (1) { ["nomeDominio"]=>  string(7) "aaaa.pt" } }

And it’s not well parsed by the js.

This is a great help, but I still can’t solve it, due to my lack of knowledge on the js side…

But, that fact that we should var_dump our $arr was a key factor to start solving it. I didn’t even know what to dump. :s

Can I have more help, or are my credits all gone? :smiley:

When you look at the request/response in Firebug, can you see what is being returned to the browser? Also… have you got error’s turned on/off? Blank pages are often usually a result of errors being suppressed. stick

 ini_set('display_errors', 1); error_reporting(E_ALL); 

at the top of your code and see what happens?

I’ve done that. I get nothing outputted into the browser.

BUT :slight_smile:

Ahhh! Console tab! I didn’t know that. Nice!! :smiley:
At least I see that the issue seems to be on the js side. :slight_smile:

So if I use fetch on the DAO all works.

If I switch to fetchAll I get an error on the autocomplete plugin js code:

b.replace is not a function
1/packer/ JS compressor detected /* !eval(new String(function(p,a,c,k,e,r);)) */

I still don’t understand but it must be related with the fact that the Js autocomplete plugin is not receiving a correct format to parse.

Now, the question is where? Maybe the var_dump could lead us a hand, but I really can’t see it. :frowning:

Any clue? :confused:

Install Firebug on Firefox and then try again. In the Console tab, you will see the Ajax request and response.

var_dump should always output something, even if it’s NULL.

Put your var_dump($arr); before you call if(is_array($arr)) line and see what response you get.

Thanks to your “dump guidance”, I found exactly where the issue was. I’ve not yet solved but, that’s another history.

Thanks a lot :smiley: