douzheng0702 2013-12-05 15:52
浏览 23
已采纳

CodeIgniter:如何根据第一个查询结果正确运行两个查询?

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
  • 写回答

1条回答 默认 最新

  • douzhu5900 2013-12-05 16:47
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号