Christian
Christian

Reputation: 269

codeigniter cannot use object of type stdclass as an array

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;

    } 

Upvotes: 0

Views: 2682

Answers (3)

devpro
devpro

Reputation: 16117

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.

Upvotes: 1

Praveen Kumar
Praveen Kumar

Reputation: 2408

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

Upvotes: 0

safin chacko
safin chacko

Reputation: 1390

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

    } 

This is your correct model function

Upvotes: 2

Related Questions