我正在开发一个关于CodeIgniter的项目,现在我正在尝试创建一个按钮,以便用户可以将产品添加到他的收藏夹中。所以我想要的是当登录用户进入产品详细信息页面并点击“添加到收藏夹”按钮时,它必须复制产品并在新视图页面上显示(cadeaupagina_ontvangen视图页面)。并且下次同一用户登录时,同一产品必须位于该视图页面上。我还在我的产品表中添加了一个名为'ontvangen_cadeaus'的新行,这意味着最喜欢的产品。
所以这是产品详细信息页面中的“添加到收藏夹”按钮:
<a href="<?php echo base_url() . 'Product/cadeaupagina_ontvangen/'.$product_id.'/'. $user_id;?>"> <button type="button" class="btn btn-default">Ik wil dit cadeau!</button></a>
Product.php控制器中的详细信息功能:
public function details($product_id)
{
$this->load->library('session');
//load the Product_model
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
// $this->session->set_userdata("product_id", $product_id);
$data['user_id'] = $this->session->userdata('user_id');
$data['product_id'] = $this->session->userdata('product_id');
//get product details
$data['product'] = $this->Product_model->get_product_details($product_id);
$data['products'] = $this->Product_model->selectProducts();
//laad view
$data['main_content'] = 'details';
$this->load->view('details',$data);
}
Product.php控制器中的cadeaupagina_ontvangen函数:
public function cadeaupagina_ontvangen($product_id='',$user_id='') {
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get ontvangen cadeaus
if(!empty($product_id)&& !empty($user_id)){
$data['products'] = $this->Product_model->get_related_product($product_id,$user_id);
}
//laad view
$data['main_content'] = 'ontvangen_cadeaus';
$this->load->view('cadeaupagina_ontvanger',$data);
}
Product_model.php中的get相关产品函数:
function get_related_product($product_id='',$user_id=''){
if(!empty($product_id)&&!empty($user_id)){
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$this->db->where('ontvangen_cadeaus', $user_id);
$query = $this->db->get();
$result = $query->row_array();
if ($result->num_rows()) {
return $result->row();
} else {
$insert_data = array('ontvangen_cadeaus'=> $user_id);
$this->db->where('product_id', $product_id);
$this->db->update('products ', $data);
}
}
}
Product_model中的getdata函数:
public function getdata($userid='')
{
$this->db->select('*');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
if(!empty($userid)){
$this->db->where('users.user_id', $userid);
}
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
详细信息页面上的按钮链接到的视图页面(cadeaupagina_ontvangen视图页面):
<?php foreach($products as $product) { ?>
<div class="main-gifts">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>">
<img id="fotos" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
</a>
<div class="products_discription">
<h3><?php echo $product["product_naam"]; ?></h3>
<div class="ophaal_plaats">
<p>Plaats: <?php echo $product["ophaal_plaats"]; ?></p>
</div>
<div class="aangeboden_door">
<p> <?php /** Foreach loop om de voornaam van de eigenaar van het product te displayen **/
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?php echo $row['voornaam'];?></td>
</tr>
<?php } ?></p>
</div>
<div class="status">
<p> Status:</p>
</div>
</div>
<a href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product["product_id"]; ?>"><input type="button" class="btn btn-default cb" value="Cadeau bewerken"></input> </a>
</div>
<?php } ?>
因此,当我点击详细信息页面上的按钮到新视图页面(cadeaupagina_ontvangen查看页面)时,产品未添加到该页面,我收到这些错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: products
Filename: views/cadeaupagina_ontvanger.php
Line Number: 12
Backtrace:
File: /home/ubuntu/workspace/application/views/cadeaupagina_ontvanger.php
Line: 12
Function: _error_handler
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 52
Function: view
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/cadeaupagina_ontvanger.php
Line Number: 12
Backtrace:
File: /home/ubuntu/workspace/application/views/cadeaupagina_ontvanger.php
Line: 12
Function: _error_handler
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 52
Function: view
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
任何人都可以注意到我做错了什么和哪个文件? 我发布了属于该函数的所有信息,因为我不知道哪个文件我做错了。
答案 0 :(得分:0)
嗯..请检查在链接上生成的日志/网址是否设置了product_id / user_id,因为在代码中有条件/检查
if(!empty($product_id)&& !empty($user_id)){
在进一步继续并获取产品之前,需要满足这两个条件。一 - 建议,不要将user_id作为参数传递。它可以为任何用户创建易于破解的场景 - 他们也可以指定任何其他用户ID。这将开始创建应用程序的故障。而是从会话中选择user_id。
答案 1 :(得分:0)
我对您希望解决的代码有一些顾虑:
num_rows()
为==0
,您也应始终从模型中返回一些内容,并且如果您希望变量出现在视图中,则不能只是跳过定义它。因此,您当前的错误。$user_id
,就好像它是一块烫手山芋。以下代码假定了一些事情(比如您希望获得所有用户收藏夹而不仅仅是一个),根据您的喜好进行更改。请在CI的自动加载部分中自动加载会话库。为了一致性和可维护性,我更改了一些函数名称。当从控制器函数重定向时,我亲自处理错误和成功消息,该控制器函数的唯一责任是与闪存消息相关的CRUD。我建议你做同样的事情,你会考虑到错误。这意味着您必须向用户显示Flash消息,但我会将其留给您。
请注意评论并了解此代码可能无法开箱即用。我为此付出了很多努力,因此您将更好地理解良好的编码实践! (我也是初学者)。
<?php
// autoload your session! don't keep loading it in every class
class Product_controller extends blahblah
{
public function cadeaupagina_ontvangen()
{
$this->load->model('Product_model');
$data['userdetail_list'] = $this->product_model->get_data();
// if product ID is empty we have an empty array
$data['products'] = $this->product_model->get_favorite_products();
$data['main_content'] = 'ontvangen_cadeaus';
$this->load->view('cadeaupagina_ontvanger', $data);
}
// accessed via: product/add_favorite_project/$id
function add_favorite_project($product_id = '')
{
if (empty($product_id) || $this->product_model->add_favorite_product($product_id)) {
$this->session->set_flashdata('error', 'Could not add favorite product.');
}
redirect('product/cadeaupagina_ontvangen/'); // or wherever...
}
}
class Product_model extends CI_Model
{
public function get_favorite_products()
{
if (empty($product_id)) {
return false;
}
$this->db->where('ontvangen_cadeaus', $this->session->user_id);
$query = $this->db->get('products');
if ($result->num_rows() == 1) {
//return $result->row(); row() is an object not an array!
//in any case we want to get all the results, right? result_array it is
return $result->result_array();
} else {
return false;
}
}
/**
* Logical issue with this code, you essentially overwrite the
* field ontvangen_cadeaus every time a user decided to add a new product
* to their favorites. Thus, no two users can have the same favorite, this
* doesn't seem right, you should have a seperate table for this
* with the user_id, favorite_product, and the corresponding product_id
*/
public function add_favorite_product($product_id)
{
$this->db->where('product_id', $product_id);
return $this->db->update('products', array('ontvangen_cadeaus' => $this->session->user_id));
/* see above comment
$str = array('user_id' => $this->session->user_id, 'product_id' => $product_id);
$this->db->get_where('favorite_products', $str);
if ($query->num_rows() == 0) {
return $this->db->insert('favorite_products', $str)
} else {
return true;
}
*/
}
public function get_data()
{
$this->db->from('users');
$this->db->join('products', 'products.user_id = users.user_id');
$this->db->where('users.user_id', $this->session->user_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
}
?>
<?php if($products !== false): ?>
<?php foreach ($products as $product): ?>
<div class="main-gifts">
<a href="<?php echo base_url() ?>/product/details/<?php echo $product["product_id"]; ?>">
<img id="fotos" src="<?php echo base_url(); ?>/upload/<?php echo $product["product_foto_thumb"]; ?>">
</a>
<div class="products_discription">
<h3><?php echo $product["product_naam"]; ?></h3>
<div class="ophaal_plaats">
<p>Plaats: <?php echo $product["ophaal_plaats"]; ?></p>
</div>
<div class="aangeboden_door">
<p>
<?php if ($userdetail_list !== false): ?>
<?php foreach ($userdetail_list as $row): ?>
<tr>
<td><?php echo $row['voornaam']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</p>
</div>
<div class="status">
<p> Status:</p>
</div>
</div>
<a href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product["product_id"]; ?>"><input type="button" class="btn btn-default cb" value="Cadeau bewerken"></input> </a>
</div>
<?php endforeach; ?>
<?php endif; ?>