2016-02-17 72 views
0

我需要查看产品数据库中每个产品都有自己的类此错误弹出我的看法,当我尝试查看。我在做什么错在这一个?笨不能使用类型为stdClass的对象作为数组

我的观点:

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

    } 
+1

的可能的复制[致命错误:无法使用类型stdClass的对象,如数组(http://stackoverflow.com/questions/5412924/fatal-error-cannot-use-对象的型stdClass的-作为阵列式) – ZeroOne

回答

0

在你的模型中,你正在使用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
public function viewprod($id) { 
      $query = $this->db->query("SELECT * from product_table WHERE product_category = '$id'"); 
     $r = $query->result_array(); 
     return $r; 

    } 

这是您正确的模型函数

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 } ?> 
相关问题