kan*_*ugl 22 php activerecord codeigniter codeigniter-3
我正在CodeIgniter中编写一个应用程序,我<title>
在每个控制器的每个页面上指定元标记,我已设法将其发送到我的标题模板.但是,现在我已经创建了一个应用程序,通过CodeIgniter模型从数据库中获取信用卡及其标题.我想自动获取并使用信用卡的名称,<title>
这样我就不需要手动更改它了,但我有点不知道如何继续.
这是我现在的代码:
调节器
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');
}
Run Code Online (Sandbox Code Playgroud)
模型
function get_card($card = FALSE)
{
$query = $this->db->get_where('creditcards', array('slug' => $card), 0,1);
return $query->result();
}
Run Code Online (Sandbox Code Playgroud)
我在创建这个应用程序时一直遵循官方的CodeIgniter文档,但到目前为止还没有运气.有解决方案吗
Abd*_*lam 14
试试这个
在模型中
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器中
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');
}
}
Run Code Online (Sandbox Code Playgroud)
在视图中
<?php echo (!empty($page_title)) ? $page_title : ''; ?> # Changed
Run Code Online (Sandbox Code Playgroud)
小智 5
创建一个基本控制器.默认位置是application/core/MY_Controller.php =>
可以通过配置更改.通过使用,$this->site_data
您可以在基类中添加变量,并在每个控件/视图中使用它们
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('your model');
$result = $this->Listing_model->get_card($card);
$this->site_data['query']=$result;
$this->site_data_header["page_title"] = $result['get the property you want'];//this is possible, as get_card returns 1 result
}
}
class YourClass extends MY_Controller
{
function __construct()
{
parent::__construct();
}
public function show($card = NULL)
{
//you don't need to split the variables
//for header and the rest
$this->load->view('includes/header',$this->site_data_header);
$this->load->view('listings/listing_card',$this->site_data);
$this->load->view('includes/footer');
}
}
Run Code Online (Sandbox Code Playgroud)
我认为你的get_where错了:
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
Run Code Online (Sandbox Code Playgroud)
你的限制是0
function get_card($card = FALSE)
{
$query = $this->db->get_where('creditcards', array('slug' => $card), 1,0);//limit 1 offset 0
return $query->result();
}
Run Code Online (Sandbox Code Playgroud)
访问视图中的数据
<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2517 次 |
最近记录: |