我正在建立一个网站,以便用户可以在网站上添加产品。现在我正在处理产品详细信息页面。一切正常,产品的所有细节都得到了回应。但是现在我正在尝试创建一个按钮,当您单击该按钮时,它必须链接到我命名为“cadeaupagina_ontvanger”的新视图页面,并且还必须复制产品并将其粘贴到该视图页面上。 我不知道如何在CodeIgniter中编写这个函数, 有人能帮助我吗?
这是详细信息页面中的按钮:
<a href="<?php echo base_url() . 'Product/cadeaupagina_ontvangen/'.$product['user_id'];?>"> <button type="button" class="btn btn-default">Ik wil dit cadeau!</button></a>
控制器功能详情:
public function details($product_id)
{
//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);
//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);
}
控制器功能cadeaupagina_ontvangen:
public function cadeaupagina_ontvangen($product_id) {
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
// get product details
// if you want to pass data by url last param than use this
if(!empty($product_id)){
$data['product'] = $this->Product_model->get_product_details($product_id);
}
//if you want to pass data in session than use this
//get session in current controller
$product_id = $this->session->userdata("product_id");
if(!empty($product_id)){
$data['product'] = $this->Product_model->get_product_details($product_id);
}
$data['products'] = $this->Product_model->selectProducts();
//laad view
$data['main_content'] = 'ontvangen_cadeaus';
$this->load->view('cadeaupagina_ontvanger',$data);
}
cadeaupagina_ontvanger是我要粘贴相同产品的视图页面。
Product_model中的模型函数:
public function getdata()
{
$this->db->select('*');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function selectProducts()
{
$query= $this->db->query("SELECT * FROM products");
$result = $query->result_array();
return $result;
}
/** Met deze functie kan je de product details ophalen via de $product_id **/
public function get_product_details($product_id)
{
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
答案 0 :(得分:1)
您可以通过以下两种方式执行此操作:
<强> 1。使用控制器URL发送产品ID。请确保在此网址中添加最后一个产品ID。
<a href ='<?php echo base_url("Product/cadeaupagina_ontvangen/$product_id"); ?>'><button type="button" class="btn btn-default">Ik wil dit cadeau!</button></a>
<强> 2。或者您可以在会话中添加产品ID以及此前的控制器
$this->session->set_userdata("product_id", $whatever_product_id);
现在在当前的控制器中:
public function cadeaupagina_ontvangen($product_id='') {
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get product details
//if you want to pass data by url last param than use this
if(!empty($product_id)){
$data['product'] = $this->Product_model->get_product_details($product_id);
}
//if you want to pass data in session than use this
//get session in current controller
$pid = $this->session->userdata("product_id");
if(!empty($pid)){
$data['product'] = $this->Product_model->get_product_details($pid);
}
//laad view
$data['main_content'] = 'ontvangen_cadeaus';
$this->load->view('cadeaupagina_ontvanger',$data);
}