As I give echo in array

Asked

Viewed 120 times

1

   public static function getAllCount(){
    $sql = "SELECT COUNT(Title) from ".self::$tablename." GROUP BY category_id";
    $query = Executor::doit($sql);
    return Model::many($query[0],new EventData());
}
 FOREACH ---

 foreach ($evcount as $c) {      
 }

 print_r($evcount);

?>  

RETORNO -----

  Array
  (
    [0] => EventData Object
    (
        [name] => 
        [lastname] => 
        [email] => 
        [category_id] => NULL
        [password] => 
        [created_at] => NOW()
        [COUNT(Title)] => 4
    )

[1] => EventData Object
    (
        [name] => 
        [lastname] => 
        [email] => 
        [category_id] => NULL
        [password] => 
        [created_at] => NOW()
        [COUNT(Title)] => 62
    )

I want to give echo pulling the COUNT(title)=>

But when I echo he gives that mistake

Call to Undefined method Eventdata::COUNT()

  • From an alias to the count column.

1 answer

3

The query returns an object with the database columns that see properties so treat them properly :COUNT() is not a method.

"SELECT COUNT(Title) from ".self::$tablename." GROUP BY category_id"

To resolve this, give an alias for the column and then access it as a normal sequence.

"SELECT COUNT(Title) as total from ".self::$tablename." GROUP BY category_id"

Inside the foreach do:

echo $c->total;

If the object has no methods, it is simpler to return a stdclass.

  • Returned the value number 2 and not the [COUNT(Title)] => 62 for each loop what should I do ? said I believe the categories without the quantity !

  • @carloscoelho as you make this call?

Browser other questions tagged

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