Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter: How do I correctly run two queries with each other based on the first query result?
    primarykey
    data
    text
    <p>I am querying my database for results (which all display correctly) and then while running a foreach statement I am attempting to look in another table for a matching ID to gather an override price which will take the place of the 'storeprice' listed in the first result.</p> <p>Here is my model:</p> <pre><code>public function fetch_products($limit, $start, $manuid) { $this-&gt;db-&gt;order_by('productname', 'ASC'); $this-&gt;db-&gt;limit($limit, $start); $query = $this-&gt;db-&gt;get_where('products', array('manuid' =&gt; $manuid, 'active' =&gt; 1)); if($query-&gt;num_rows() &gt; 0){ foreach($query-&gt;result() as $row){ $data[] = $row; $pid = $row-&gt;id; // Check for Price Override $squery = $this-&gt;db-&gt;get_where('price_override', array('id' =&gt; $pid)); if($squery-&gt;num_rows() &gt; 0){ $result = $squery-&gt;row(); $override = $result-&gt;storeprice; $data['override'] = $override; } else { $override = 0; $data['override'] = $override; } } return $data; } return false; } </code></pre> <p>The $data['override'] is what is causing me an error. The following error to be precise: </p> <pre><code>Message: Undefined property: stdClass::$override </code></pre> <p>In the controller I am using the following:</p> <pre><code>$data['results'] = $this-&gt;store_products_model-&gt;fetch_products($config["per_page"], $page, $manuid); </code></pre> <p>And finally in the view I am calling the results inside of a table to display. All will display except for the override:</p> <pre><code>foreach ($results as $product){ echo '&lt;tr&gt;'; echo '&lt;td&gt;' . $product-&gt;productname . '&lt;/td&gt;'; if($product-&gt;override != 0){ echo '&lt;td&gt;$' . $product-&gt;override . '&lt;/td&gt;'; } else { echo '&lt;td&gt;$' . $product-&gt;storeprice . '&lt;/td&gt;'; } echo '&lt;td&gt;$' . $product-&gt;saleprice . '&lt;/td&gt;'; echo '&lt;td&gt;' . $product-&gt;storepoints . '&lt;/td&gt;'; echo '&lt;td&gt;&lt;a href="store_modify_product/' . $product-&gt;id . '"&gt;Edit&lt;/a&gt;'; echo '&lt;/tr&gt;'; } </code></pre> <p>Anyone see what I could be doing wrong here to give me the following error?</p> <pre><code>Message: Undefined property: stdClass::$override </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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