如何从数据库中获取项目标题并将其发送到CodeIgniter中的标题模板 [英] How to fetch title of an item from a database and send it to the header template in CodeIgniter
问题描述
我正在CodeIgniter中编写一个应用程序,在该应用程序中,我已设法将每个控制器发送到标头模板的每个页面上的每个页面上指定< title>
元标记。但是,现在我创建了一个应用程序,该应用程序通过CodeIgniter模型从数据库中获取信用卡及其标题。我想自动获取并使用< title>
中的信用卡名称,这样我就不需要手动更改它,但是我有点卡住了
I am writing an application in CodeIgniter where I specify the <title>
meta-tag on every page in every controller which I have managed to send to my header template. However, now I have created an application that fetch credit cards and their titles from the database, through an CodeIgniter model. I would like to automatically fetch and use the credit card's name in <title>
so that i don't need to change it manually, but I'm a little stuck on how to proceed.
这是我到目前为止的代码:
This is my code as of now:
控制器
public function show($card = NULL)
{
$data['query'] = $this->Listing_model->get_card($card);
$header["page_title"] = from the model
$this->load->view('includes/header',$header);
$this->load->view('listings/listing_card',$data);
$this->load->view('includes/footer');
}
模型
function get_card($card = FALSE)
{
$query = $this->db->get_where('creditcards', array('slug' => $card), 0,1);
return $query->result();
}
在创建此应用程序时,我一直在遵循官方CodeIgniter文档。没运气。有解决方案吗?
I have been following the official CodeIgniter documentation when creating this application, but so far no luck. Any solutions?
推荐答案
尝试一下
- 模型已更改
- 控制器已更改。
在模型中
function get_card($card)
{
$query = $this->db->query("SELECT * FROM table_name WHERE creditcards = '$card' ");
$result = $query->result_array();
$count = count($result); # New
if(empty($count)){ # New
return FALSE;
}
elseif($count > 1){ # New
return 0;
}
else{
return $result;
}
}
在控制器中
public function show($card)
{
$result = $this->Listing_model->get_card($card); # Changed
if($result == FALSE){ # New
echo "No Data Found";
}
elseif($result == 0){ # New
echo "Multiple Data Found";
}
else{
$data["page_title"] = $result[0]['field_name']; # Changed
$this->load->view('includes/header',$data); # Changed
$this->load->view('listings/listing_card',$data);
$this->load->view('includes/footer');
}
}
视图中
<?php echo (!empty($page_title)) ? $page_title : ''; ?> # Changed
这篇关于如何从数据库中获取项目标题并将其发送到CodeIgniter中的标题模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!