Ryan
Ryan

Reputation: 388

CodeIgniter: How do I correctly run two queries with each other based on the first query result?

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.

Here is my model:

public function fetch_products($limit, $start, $manuid) {
    $this->db->order_by('productname', 'ASC');
    $this->db->limit($limit, $start);

    $query = $this->db->get_where('products', array('manuid' => $manuid, 'active' => 1));
    if($query->num_rows() > 0){
        foreach($query->result() as $row){
            $data[] = $row;
            $pid = $row->id;
            // Check for Price Override
            $squery = $this->db->get_where('price_override', array('id' => $pid));
            if($squery->num_rows() > 0){
                $result = $squery->row();
                $override = $result->storeprice;
                $data['override'] = $override;
            } else {
                $override = 0;
                $data['override'] = $override;
            }
        }
        return $data;
    }
    return false;
}    

The $data['override'] is what is causing me an error. The following error to be precise:

Message: Undefined property: stdClass::$override

In the controller I am using the following:

$data['results'] = $this->store_products_model->fetch_products($config["per_page"], $page, $manuid);

And finally in the view I am calling the results inside of a table to display. All will display except for the override:

foreach ($results as $product){
               echo '<tr>';
               echo '<td>' . $product->productname . '</td>';
               if($product->override != 0){
                echo '<td>$' . $product->override . '</td>';   
               } else {
                echo '<td>$' . $product->storeprice . '</td>';
               }
               echo '<td>$' . $product->saleprice . '</td>';
               echo '<td>' . $product->storepoints . '</td>';
               echo '<td><a href="store_modify_product/' . $product->id . '">Edit</a>';
               echo '</tr>';
            }

Anyone see what I could be doing wrong here to give me the following error?

Message: Undefined property: stdClass::$override

Upvotes: 0

Views: 633

Answers (1)

foxmulder
foxmulder

Reputation: 161

The row is an object, not an array. Try:

$data->override = $override

edit:

Well, actually, $data is an array, but your inserting an object into it, so it would be

$last_index = sizeof($data) - 1;
$data[$last_index]->override = $override;

Upvotes: 1

Related Questions