0

我有一个产品表(tbl_product),其中包含 product_id、product_name 的产品数据,

当我可以使用 product_id 查看产品详细信息时,我想在标题栏中显示产品名称。但我没有做到这一点。请帮我详细说明。

控制器:::

    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 查看产品详细信息时,我想在标题栏中显示产品名称。但我没有做到这一点。请帮我详细说明。

4

2 回答 2

1

对于 CodeIgniter,你应该记住,。

$data[]在一个数组中。因此,您可以在控制器中设置元素$data并在您的视图中检索您的$data数组。你不能用

$data['content']=$this->home_model->getContent();
$data['title']= $content;

在您的控制器中。

如果您知道您的“product_id”,那么每个产品都应该有不同的 url。

当您想查看产品时,使用 ulr 助手并编写此代码

redirect('single_product/'.$product_id)

现在使用uri 类并检查你的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;
         }
     }
于 2013-11-04T08:21:51.440 回答
0

请在您的视图中进行以下更改。如果您查看您的控制器代码,您将数组(从您的模型)返回到 $data['content'] 所以您的视图应该如下所示

  <head>
        <title> <?php echo $content[0]; ?> </title>
  </head>
于 2013-11-04T06:56:54.960 回答