0

Sorry I am not very good in object and

I want to do something like that :

$this->countries       =new stdclass;
$this->countries->id   =array();
$this->countries->name =array();

$query = $db
    ->getQuery(true)
    ->select('name,id')
    ->from('#__country');

$rows = $db
    ->setQuery($query)
    ->loadObjectList();

foreach ($rows as $row) {
    $this->countries->id  = $row->id;
    $this->countries->name  = $row->name;
}

foreach ($this->countries as $country):
   echo $country->id;
   echo $country->name;
endforeach;

I receiv this error : Notice: Trying to get property of non-object I don't know why it dont work

Thanks

Yann
  • 23
  • 3
  • possible duplicate of [Notice: Trying to get property of non-object error](http://stackoverflow.com/questions/22636826/notice-trying-to-get-property-of-non-object-error) – Justine Krejcha Apr 07 '15 at 05:03

1 Answers1

2

For the foreach ($this->countries as $country): code to work correctly then $this->countries must be an array. Suggested changes to the code... (untested)

$this->countries       = array();
//    $this->countries       = new stdclass;
//    $this->countries->id   =array();
//    $this->countries->name =array();

$query = $db
    ->getQuery(true)
    ->select('name,id')
    ->from('#__country');

$rows = $db
    ->setQuery($query)
    ->loadObjectList();

foreach ($rows as $row) {
    $country      = new stdclass;
    $country->id  = $row->id;
    $country->name  = $row->name;
    $this->countries[] = $country;   
}

foreach ($this->countries as $country) {
   echo $country->id;
   echo $country->name;
}
Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31