Note that there are some explanatory texts on larger screens.

plurals
  1. POaccessing a object variable in PHP codeigniter
    primarykey
    data
    text
    <p>I have the following piece of code in my Model:</p> <pre><code>public function getSite() { $this-&gt;load-&gt;library('site'); $data = array(); $str = "SELECT * FROM sitematrix_sites"; $query = $this-&gt;getQuery($str); if($query){ foreach($query-&gt;result() as $row){ $site = new Site(); array_push($data, $site-&gt;load($row)); } foreach($data as $site){ $site-&gt;data['database'] = $this-&gt;getDatabase($site-&gt;data['site_id']); $site-&gt;data['baseline'] = $this-&gt;getBaseline($site-&gt;data['site_id']); } return $data; } else{ return false; } } </code></pre> <p>And I created a class called site:</p> <pre><code>class Site{ public $data; public function __contruct(){ $this-&gt;data = ArrayObject(array(), ArrayObject::STD_PROP_LIST); $this-&gt;data['site_id'] = ''; $this-&gt;data['site'] = ''; $this-&gt;data['site_code'] = ''; $this-&gt;data['tc_10'] = array(); $this-&gt;data['tc9x'] = array(); $this-&gt;data['tc8x'] = array(); $this-&gt;data['tc2008'] = array(); $this-&gt;data['eng_2005'] = array(); $this-&gt;data['database'] = array(); $this-&gt;data['baseline'] = array(); </code></pre> <p>However, I'm getting the following error:</p> <pre><code>&lt;p&gt;Severity: Notice&lt;/p&gt; &lt;p&gt;Message: Trying to get property of non-object&lt;/p&gt; &lt;p&gt;Filename: models/sitematrix_model.php&lt;/p&gt; &lt;p&gt;Line Number: 77&lt;/p&gt; </code></pre> <p>Any thoughts? I have tried to access those values either as <code>$site-&gt;data-&gt;database</code> and as <code>$site-&gt;data['database']</code> but both don't work.</p> <pre><code>line 77: $site-&gt;data['database'] = $this-&gt;getDatabase($site-&gt;data['site_id']); </code></pre> <p>The function getDatabase:</p> <pre><code>public function getDatabase($id){ $database = array(); $str = "SELECT * FROM site_database where site_id='$id'"; $query = $this-&gt;getQuery($str); if($query){ foreach($query-&gt;result() as $row){ array_push($database, $row-&gt;name); } } return $database; } </code></pre> <p>Function getQuery:</p> <pre><code>public function getQuery($str){ $data = array(); $query = $this-&gt;db-&gt;query($str); if(!$query || $query-&gt;num_rows() == 0){ return false; } return $query; } </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload