无法在CodeIgniter中加载产品详细信息页面

时间:2017-07-13 08:24:57

标签: php codeigniter codeigniter-3

我正在尝试制作产品详情页面。因此,当用户在产品页面上时,他们可以点击特定产品以查看产品的详细信息,但我收到了一些错误。

这是我正在加载所有产品的所有产品视图页面:

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);
 }

这是产品详细信息控制器功能:

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

这是产品详细信息视图页面:

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();
    }

这是产品详细模型功能:

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:

当我尝试加载产品的详细信息页面时,我收到了这些错误:

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

$pcname = explode("\r", file_get_contents("PCNAMES2.txt"));
foreach($pcname as $pc) {
    echo "\\".$pc."\c$";
}

2 个答案:

答案 0 :(得分:1)

很简单。问题是你错误地调用了details($productid)方法。要解决此问题,请在所有产品页面中添加<a>标记中的产品ID。见下面的代码。

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

答案 1 :(得分:1)

这应该是您的视图代码(与上面的答案相同): 并且请确保“$ products”包含名为“id”的密钥。

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

然后在控制器中按以下方式更改:

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);
 }

请注意,您必须加载URL帮助程序,以防出现错误:

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