dongrenzheng1619 2016-02-17 13:33
浏览 30
已采纳

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

i need to view products from the database each products have their own category this error pops up to my view when i try to view. what i am doing wrong in this one ?

my view:

<?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 } ?>   

controller:

public function view_products() {

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

    }

model:

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

    } 
  • 写回答

3条回答 默认 最新

  • douwuli4512 2016-02-17 19:23
    关注

    In your model you are using result() function and this will return you result in object form.

    $query->result();
    

    Solution 1:

    If you still want to use result() function than you need to change your view as:

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

    Solution 2:

    If you don't want to change your view file than you must need to use result_array() in your model as:

    $query->result_array(); // will return you result in array format.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题