views:

50

answers:

2

If I have something like this from the server side, from a fetch:

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

The json of the above is:

[{"nome":"aaaa.br"}]

This Works:

parse: function(data) {
 return $.map(eval('('+data+')'), function(result) {
   return {
    data: result,
    value: result.nome,
    result: result.nome
   }
  });
}

The result is parsed successfully.

If, instead of fetch, I change to fetchAll, the dump gets like this (here only the first index as example):

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

The json conversion of the above:

string(2632) "[{"nome":{"nomeDominio":"aaaa.br"}}

Here, the result is not successfully parsed.

So I believe something needs to be changed on the js side. But I'm absolutely clueless.

UPDATE: The nomeDominio is from the fetchObj PDO method, and corresponds to the column name on the database. It's a natural behaviour for fetch with PDO when FETCH::OBJ option is used.

The php part of this js is:

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

$comandos = new ComandoController();

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

if(is_array($arr))
{
    echo json_encode($arr);

}


public function recebeNomeDominios($keyword)
{
   $DominioDao = new DominioDao();

   $objecto = $DominioDao->recebeNomeDominios($keyword);

   return $this->jsonArray($objecto);

}

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

    }
  }
   return $json;
}

Finally:

public function recebeNomeDominios($keyword)
{
  try
  {
     $stmt = $this->_dbh->prepare("SELECT d.nomeDominio FROM dominio d WHERE d.nomeDominio LIKE '%".$keyword."%'");
$stmt->execute();
$resultado = $stmt->fetch(PDO::FETCH_OBJ);
return $resultado;
}
catch (PDOException $ex)
{
  echo "Erro: " . $ex->getMessage();
}
}

Any advice? MEM

A: 

If you return one array (fetch) then you need to collect data from it like this

var name = data.name;
var age = data.age;
var gender = data.gender;
// Do something with values here

If you are using fetchAll, this will presumably return an array of arrays (a multidimensional array) which you will need to iterate over. From looking at $.map it looks like your using jQuery. Iterate over the multidimensional array like this

jQuery.each(data, function() {
    name = this.name;
    age = this.age;
    gender = this.gender;
    // Do something with values here
});
jakenoble
That will help... I'm sure... I've updated my question do you mind to take a look?
MEM
+1  A: 
$comandos = new ComandoController();
$arr = $comandos->recebeNomeDominios($keyword);
echo json_encode($arr);


class ComandoController {
  public function recebeNomeDominios($keyword)
  {
    $stmt = $this->_dbh->prepare('
      SELECT
        d.nomeDominio as nome
      FROM
        dominio
      WHERE nomeDominio LIKE :keyword
    ');
    $stmt->bindParam(':keyword', $keyparam);
    $keyparam = '%'.str_replace('%', '\\%', $keyword) . '%';
    $stmt->execute();
    return $stmt->fetchALL(PDO::FETCH_ASSOC);
  }
...
VolkerK
I cannot wait to give it a try. Thanks a lot. Why fetch_assoc and not fetch_obj ?
MEM
"Why fetch_assoc" - No particular reason, just habit. Should work with FETCH_OBJ as well.
VolkerK
Well... I just found out that, on jsonArray, if we change from $json[] = array('nome' => $obj); to, $json[] = array('nome' => $obj->nomeDominio); it works perfectly. If you add this on your answer... I can mark it. :) It was so simple. Just adding a ->nomeDominio... oh well... I will use your code however, because you have help me solve another issue, the fact that I was unable to use bindParam with LIKE keywords. ;) I will give it a try.
MEM