我有一个产品表(tbl_product),其中包含产品数据以及product_id、product_name,
当我可以使用product_id查看产品详细信息时,我想在标题栏中显示产品名称。但我没能做到这一点。请帮我提供详细信息。
Controller :::
public function product_details($product_id)
{
$data=array();
$data['result']=$this->home_model->selectProductByProductId($product_id);
$data['banner']=FALSE;
$data['maincontent']=$this->load->view('single_product', $data, TRUE);
$data['content']=$this->home_model->getContent();
$data['title']= $content;
$this->load->view('home', $data);
}
型号:::
public function getContent()
{
$this->db->select('tbl_product.product_name');
$this->db->from('tbl_product');
$this->db->where('product_name', $product_name);
$product = $this->db->get()->result_array();
}
查看:::
<head>
<title> <?php echo $title; ?> </title>
</head>
当我可以使用product_id查看产品详细信息时,我想在标题栏中显示产品名称。但我没能做到这一点。请帮我提供详细信息。
最佳答案
对于CodeIgniter,你应该记住,。
$data[]
位于数组中。因此,您可以在 Controller 中的 $data
中设置元素,并在 View 中检索 $data
数组。你不能使用
$data['content']=$this->home_model->getContent();
$data['title']= $content;
在你的 Controller 中。
如果您知道“product_id”,那么每个产品都应该有不同的网址。
当您想查看产品时,使用 ulr 帮助程序并编写此代码
redirect('single_product/'.$product_id)
现在使用uri class并检查您的product_id
,例如
if($this->uri->segment(2)){
$data['title']=$this->home_model->get_title($this->uri->segment(2));
.........................
.......
}
并在模型中创建一个 get_title
方法
public function get_title($product_id)
{
$query=$this->db->query("select product_name from tbl_product where product_id='$product_id'");
$result = $query->row();
if(isset($result)){
$title=$result->product_name;
return $title;
}
}
关于php - 如何在 codeigniter 的产品页面中添加动态产品标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762777/