0

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;

    } 
1

3 Answers 3

2
 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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.