codeigniter不能使用stdclass类型的对象作为数组

时间:2016-02-17 13:34:00

标签: php database codeigniter

我需要查看数据库中的产品每个产品都有自己的类别当我尝试查看时,此错误会弹出到我的视图中。我在这个问题上做错了什么?

我的观点:

<?php
            foreach ($products as $alls) {
                $id = $alls['product_id'];
                $name = $alls['product_name'];
                $description = $alls['product_description'];
                $price = $alls['product_price'];
                $picture = $alls['img_name'] . $alls['ext'];
                ?>

                <div class="col-md-4"><a data-toggle="modal" data-target="#myModal">
                        <img class = "bread_img" id = "bread_img_<?php echo $id;?>" src="<?php echo base_url() . 'assets/' . $picture; ?>"  onMouseOut="this.src = '<?php echo base_url() . 'assets/' . $picture; ?>'" width="230" height="192"></a>
                        <input type ="hidden" id = "hidden_name_<?php echo $id;?>" value = "<?php echo $name;?>" >
                        <input type ="hidden" id = "hidden_desc_<?php echo $id;?>" value = "<?php echo $description;?>" >
                    <br><br> <h5 class="names" id="pname" src="<?php echo $name; ?>"><?php echo $name; ?></h5>₱&nbsp;<?php echo $price; ?>
                    <br><br><br><br><br>
                    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog modal-l">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                    <h4 class="modal-title" id="myModalLabel"> <?php echo $name; ?></h4>
                                </div>
                                <div class="modal-body">
                                    <img  src="<?php echo base_url() . 'assets/' . $picture; ?>" width="500" height="417" id = "modal_img">
                                    <br><br><h6 class="modal-title" id="myModalLabels"> <?php echo $description; ?></h6><br><br>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            <?php } ?>   

控制器:

public function view_products() {

            $id = $this->uri->segment(3);
            $data['products'] = $this->user->viewprod($id);
            $this->load->view('product_viewpage', $data);

    }

模型:

  public function viewprod($id) {
          $query = $this->db->query("SELECT * from product_table WHERE product_category = '$id'");
        $r = $query->result();
        return $r;

    } 

3 个答案:

答案 0 :(得分:2)

 public function viewprod($id) {
          $query = $this->db->query("SELECT * from product_table WHERE product_category = '$id'");
        $r = $query->result_array();
        return $r;

    } 

这是您正确的模型功能

答案 1 :(得分:1)

在您的模型中,您使用的是result()函数,这将以对象形式返回结果。

$query->result();

解决方案1:

如果您仍想使用result()功能,则需要将视图更改为:

$id = $alls->product_id; 
$name = $alls->product_name; $description = $alls->product_description; 
$price = $alls->product_price; 
$picture = $alls->img_name. $alls->ext;

解决方案2:

如果您不想更改视图文件,则必须在模型中使用result_array()

$query->result_array(); // will return you result in array format.

答案 2 :(得分:0)

<?php
            foreach ($products as $alls) {
                $id = $alls->product_id;
                $name = $alls->product_name;
                $description = $alls->product_description;
                $price = $alls->product_price;
                $picture = $alls->img_name."." . $alls->ext;//assuming $alls->ext = 'jpg' and $alls->img_name = 'abc' so `$alls->img_name."." . $alls->ext` prints abc.jpg
                ?>

            <div class="col-md-4"><a data-toggle="modal" data-target="#myModal">
                    <img class = "bread_img" id = "bread_img_<?php echo $id;?>" src="<?php echo base_url() . 'assets/' . $picture; ?>"  onMouseOut="this.src = '<?php echo base_url() . 'assets/' . $picture; ?>'" width="230" height="192"></a>
                    <input type ="hidden" id = "hidden_name_<?php echo $id;?>" value = "<?php echo $name;?>" >
                    <input type ="hidden" id = "hidden_desc_<?php echo $id;?>" value = "<?php echo $description;?>" >
                <br><br> <h5 class="names" id="pname" src="<?php echo $name; ?>"><?php echo $name; ?></h5>₱&nbsp;<?php echo $price; ?>
                <br><br><br><br><br>
                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog modal-l">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                <h4 class="modal-title" id="myModalLabel"> <?php echo $name; ?></h4>
                            </div>
                            <div class="modal-body">
                                <img  src="<?php echo base_url() . 'assets/' . $picture; ?>" width="500" height="417" id = "modal_img">
                                <br><br><h6 class="modal-title" id="myModalLabels"> <?php echo $description; ?></h6><br><br>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        <?php } ?>