lablanco
lablanco

Reputation: 103

Unable to load product details page in CodeIgniter

I'm trying to make a product detail page. So when users are on the products page they can click on a specific product to see the details of the products but I'm getting some errors.

This is the all products view page where I'm loading all my products:

<?php foreach($products as $product) : ?>
    <div class="col-lg-2">
        <div id="product">
        <a href="<?php echo base_url() ?>/Product/details">
            <?php  echo '<img src="upload/'.$product['product_foto'].'">' ; ?>
        </a>
        <div class="product_naam"><?php echo $product['product_naam']; ?></div>
        <div class="ophaal_plaats">
           <?php  echo $product['ophaal_plaats']; ?>
        </div>
        <div class="aangeboden_door">
            <p>Aangeboden door: Peter</p>
        </div>
        </div>
        </div>
<?php endforeach; ?>

This is the product detail controller function:

public function details($product_id) {
     //vraag product details
     $data['product'] = $this->Product_model->get_product_details($product_id);

     //laad view
     $data['main_content'] = 'details';
     $this->load->view('details',$data);
 }

This is the product detail view page:

<div class="container">
<div class="row">
<div><?php echo $product->product_naam; ?></div>
    <div class="col-md-4">
          <img src="<?php echo base_url().'upload/'.$product['product_foto'] ?>" class="img-responsive">
    </div>
</div>

And this is the product detail model function:

public function get_product_details($product_id) {
        $this->db->select('*');
        $this->db->from('products');
        $this->db->where('id', $product_id);

        $query = $this->db->get();
        return $query->row();
    }

When I try to load a detail page of a product I'm getting these errors:

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Product::details()

Filename: controllers/Product.php

Line Number: 18

Backtrace:

File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 18
Function: _error_handler

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once



A PHP Error was encountered

Severity: Notice

Message: Undefined property: Product::$Product_model

Filename: controllers/Product.php

Line Number: 20

Backtrace:

File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 20
Function: _error_handler

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once





A PHP Error was encountered

Severity: Error

Message: Call to a member function get_cadeau_details() on a non-object

Filename: controllers/Product.php

Line Number: 20

Backtrace:

When I type: <?php echo "<pre>";print_r($products);echo "</pre>";?>

Array
(
    [0] => Array
        (
            [product_id] => 29
            [product_naam] => bfbfbf
            [product_beschrijving] => fdeed
            [user_id] => 0
            [product_categorie] => eetbaar
            [ophaal_plaats] => fbfg
            [product_foto] => 78fa40277b5b961b7b3c3c25e4f5a2af.jpg
            [date_created] => 2017-07-12
            [date_updated] => 2017-07-12
        )

    [1] => Array
        (
            [product_id] => 30
            [product_naam] => dfbfg
            [product_beschrijving] => fdgfgfg
            [user_id] => 0
            [product_categorie] => kleding
            [ophaal_plaats] => gfgdgf
            [product_foto] => fdbf476783f74afef45ecd84a8df0da1.jpg
            [date_created] => 2017-07-12
            [date_updated] => 2017-07-12
        )

    [2] => Array
        (
            [product_id] => 31
            [product_naam] => Call of duty BO III
            [product_beschrijving] => fefef
            [user_id] => 0
            [product_categorie] => kec
            [ophaal_plaats] => Japan
            [product_foto] => 66e57b903b9d297c575259b3dfb04f70.jpg
            [date_created] => 2017-07-12
            [date_updated] => 2017-07-12
        )

    [3] => Array
        (
            [product_id] => 32
            [product_naam] => fefe
            [product_beschrijving] => gwrfef
            [user_id] => 0
            [product_categorie] => spellen
            [ophaal_plaats] => efefef
            [product_foto] => b8c923e48baa25d363baa29da40e23e1.jpg
            [date_created] => 2017-07-12
            [date_updated] => 2017-07-12
        )

)

Upvotes: 0

Views: 482

Answers (2)

Ketan Solanki
Ketan Solanki

Reputation: 697

This should be your view code (Same as the above answer): And Please make sure that the "$products" contains key named as "id" in it.

<?php foreach($products as $product) : ?>
    <div class="col-lg-2">
        <div id="product">
        <a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
            <?php  echo '<img src="upload/'.$product['product_foto'].'">' ; ?>
        </a>
        <div class="product_naam"><?php echo $product['product_naam']; ?></div>
        <div class="ophaal_plaats">
           <?php  echo $product['ophaal_plaats']; ?>
        </div>
        <div class="aangeboden_door">
            <p>Aangeboden door: Peter</p>
        </div>
        </div>
        </div>
<?php endforeach; ?>

And then in the controller change in the following way:

public function details() {  //Remove product id from arguments
     $product_id = $this->uri->segment('3');
     $data['product'] = $this->Product_model->get_product_details($product_id);
     $data['main_content'] = 'details';
     $this->load->view('details',$data);
 }

And please note that you have to load the URL helper,in case you get error:

$this->load->helper('url');

Upvotes: 1

Parag Soni
Parag Soni

Reputation: 713

It is simple. Problem is you are calling your details($productid) methode wrongly. To solve this, in your all product page add product id in <a> tag. see code below .

<?php foreach($products as $product) : ?>
    <div class="col-lg-2">
        <div id="product">
        <a href="<?php echo base_url() ?>/Product/details/<?php echo $product['id']; ?>">
            <?php  echo '<img src="upload/'.$product['product_foto'].'">' ; ?>
        </a>
        <div class="product_naam"><?php echo $product['product_naam']; ?></div>
        <div class="ophaal_plaats">
           <?php  echo $product['ophaal_plaats']; ?>
        </div>
        <div class="aangeboden_door">
            <p>Aangeboden door: Peter</p>
        </div>
        </div>
        </div>
<?php endforeach; ?>

Upvotes: 1

Related Questions