如何从数据库中获取项目的标题并将其发送到CodeIgniter中的标题模板

时间:2022-12-04 23:20:47

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.

我正在CodeIgniter中编写一个应用程序,我在每个控制器的每个页面上指定

元标记,我已设法将其发送到我的标题模板。但是,现在我已经创建了一个应用程序,通过CodeIgniter模型从数据库中获取信用卡及其标题。我想在 中自动获取并使用信用卡的名称,这样我就不需要手动更改它了,但我有点不知道如何继续。</p>

This is my code as of now:

这是我现在的代码:

Controller

调节器

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');
}

Model

模型

function get_card($card = FALSE)
{
    $query = $this->db->get_where('creditcards', array('slug' => $card), 0,1);
    return $query->result();
}

I have been following the official CodeIgniter documentation when creating this application, but so far no luck. Any solutions?

我在创建这个应用程序时一直关注官方的CodeIgniter文档,但到目前为止还没有运气。有解决方案?

13 个解决方案

#1


14  

Try this

尝试这个

  1. Model is changed
  2. 模型改变了
  3. Controller is changed.
  4. 控制器已更改。

In Model

在模型中

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;
    }
}

In Controller

在控制器中

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');
    }

}

In View

在视图中

<?php echo (!empty($page_title)) ? $page_title : ''; ?> # Changed 

#2


5  

Create a base controller. The default location for this is application/core/MY_Controller.php => this can be changed via the config. By using $this->site_data you can add variables in your base class and use them in every controlle/view

创建一个基本控制器。这个的默认位置是application / core / MY_Controller.php =>这可以通过config更改。通过使用$ 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');
    }
}

And I think your get_where is wrong:

我认为你的get_where错了:

$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

your limit is 0

你的限制是0

function get_card($card = FALSE)
{
    $query = $this->db->get_where('creditcards', array('slug' => $card), 1,0);//limit 1 offset 0
    return $query->result();
}

access the data in your view

访问视图中的数据

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#3


4  

A simple example:

一个简单的例子:

Controller

调节器

$query = $this->Listing_model->get_card($card);
$query = $query->row();

$header["page_title"] = $query->title;

View

视图

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#4


4  

You can create a Base Controller and Extends all you other controller to that base controller.

您可以创建基本控制器并将所有其他控制器扩展到该基本控制器。

Like this

喜欢这个

<?php
class MY_Controller extends CI_Controller {

public $data = array();
    function __construct() {

        parent::__construct();

        $this->data['errors'] = array();

        $this->data['site_name'] = config_item('site_name');


    }
}       

Then In Your Controller

然后在你的控制器中

class Test extends MY_Controller
{
  function __construct() {

        parent::__construct();
         $this->data['meta_title'] = 'Your Title';

 }

}

And in you views access the page title like this:

在您的视图中访问页面标题,如下所示:

echo("<title>.$site_name.</title>");

#5


3  

Controller

调节器

 $card_data= $this->Listing_model->get_card($card); //Your model returns an array of objects


 $header["page_title"]  = $card_data[0]->title; //grab value of 'title' property of first object returned from model.


 $this->load->view('includes/header',$header);

View

视图

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#6


3  

Try this:

尝试这个:

function get_card($card = FALSE)
{
    $data = $this->db->get_where('creditcards', array('slug' => $card), 0,1)->result();
    $data->title = $data[0]->title;
    return $data;
}

#7


3  

Controller

调节器

$query = $this->Listing_model->get_card($card);
var_dump($query);
//Your $query may be some data got from db;
$card_name = "";
if(!empty($query)){
$card_name = $query[0]->name;  //You must verify the name attribute and it should in the $query result;
}

$header["page_title"] = $card_name;

View

视图

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#8


3  

You may need to create some routes for your show function. Codeigniter URI Routing

您可能需要为show函数创建一些路径。 Codeigniter URI路由

$route['your_controller_name/show/(:any)'] = 'your_controller_name/show/$1';

I am not sure if you have set up a htaccess for your main directory so you could remove the index.php from your url.

我不确定你是否为你的主目录设置了一个htaccess,所以你可以从你的网址中删除index.php。

Try this code below

请尝试以下代码

Model:

模型:

<?php 

class Listing_model extends CI_Model {

function get_card_title($card) {
    $this->db->where('slug', $card);
    $query = $this->db->get($this->db->dbprefix . 'creditcards');
    if ($query->num_rows() > 0) {
        $row = $quer->row();
        return $row->title;
    } else {
        return false;
    }
}

}

Controller: Your_controller_name.php

控制器:Your_controller_name.php

<?php

class Your_controller_name extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('listing_model');
    }

    public function show($card) {
        $data['title'] = $this->listing_model->get_card_title($card);

        $this->load->view('includes/header', $data);
        $this->load->view('listings/listing_card', $data);
        $this->load->view('includes/footer');
    }
}

View:

视图:

<head>
<title><?php echo $title;?></title>
</head>

#9


1  

In your listing card view, do this:

在您的列表卡视图中,执行以下操作:

foreach ($query  as  $rec){
    <title><?php echo $rec->title ?></title>
}

replace 'title' with the name of the column on your database that keeps the title of the credit card...so you are passing the results of the query you ran in your controller to this view, and then using a foreach loop to display data of the specific credit card

将'title'替换为数据库中保留信用卡标题的列的名称...所以您将在控制器中运行的查询结果传递给此视图,然后使用foreach循环显示特定信用卡的数据

#10


1  

You can use template library for robustness and use as follows:

您可以使用模板库来获得稳健性,并使用如下:

Controller

调节器

$this->template->title('Home :: ' . $this->data['metadata']['site_name'])
            ->set_layout('home_layout')
            ->build('listing_card', $this->data);

Views

查看

    <title><?php echo $template['title']; ?></title>
    <?php echo $template['metadata']; ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Reference: https://github.com/philsturgeon/codeigniter-template

参考:https://github.com/philsturgeon/codeigniter-template

#11


1  

Just to add another, there's no reason this shouldn't work:

只是为了添加另一个,没有理由这不起作用:

$data['query'] = $this->Listing_model->get_card($card);
$this->load->view('header', array('page_title' => $data['query'][0]->column_name)); 
//Will there only be one result? Consider returning $query->row(). Multiple,      
//loop through and set one title

In your view:

在你看来:

<title><?=isset($page_title) ? $page_title : "";?></title>

If this doesn't work your query isn't returning what you think it is.

如果这不起作用,则查询不会返回您认为的内容。

#12


1  

Controller :

控制器:

$data["page_title"] = $result[0]['title_field'];

view: and You just need to write in your header file like :

view:你只需要在头文件中写入:

<title><?php echo $page_title; ?></title>

#13


1  

In your model - don't return $query->result(), just return $query:

在你的模型中 - 不要返回$ query-> result(),只返回$ query:

function get_card($card = FALSE)
{
    $query = $this->db->get_where('creditcards', array('slug' => $card), 0,1);
    return $query;
}

Controller:

控制器:

public function show($card = NULL)
{
    // verify that you got something back from the database
    // or show an error 
    if( ! $query = $this->Listing_model->get_card($card) )
    {
        $this->_showNoResultsFor($card) ; 
    } 
    else
    {

        // get one record from the query using row()
        $onecard = $query->row() ; 

        // assign the title using whatever your field name is called 
        $header["page_title"] = $onecard->thetitle ; 

        // Now assign the query result() to data 
        $data['query'] = $query->result() ; 

        $this->load->view('includes/header',$header);
        $this->load->view('listings/listing_card',$data);
        $this->load->view('includes/footer');

    }    

}

#1


14  

Try this

尝试这个

  1. Model is changed
  2. 模型改变了
  3. Controller is changed.
  4. 控制器已更改。

In Model

在模型中

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;
    }
}

In Controller

在控制器中

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');
    }

}

In View

在视图中

<?php echo (!empty($page_title)) ? $page_title : ''; ?> # Changed 

#2


5  

Create a base controller. The default location for this is application/core/MY_Controller.php => this can be changed via the config. By using $this->site_data you can add variables in your base class and use them in every controlle/view

创建一个基本控制器。这个的默认位置是application / core / MY_Controller.php =>这可以通过config更改。通过使用$ 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');
    }
}

And I think your get_where is wrong:

我认为你的get_where错了:

$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

your limit is 0

你的限制是0

function get_card($card = FALSE)
{
    $query = $this->db->get_where('creditcards', array('slug' => $card), 1,0);//limit 1 offset 0
    return $query->result();
}

access the data in your view

访问视图中的数据

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#3


4  

A simple example:

一个简单的例子:

Controller

调节器

$query = $this->Listing_model->get_card($card);
$query = $query->row();

$header["page_title"] = $query->title;

View

视图

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#4


4  

You can create a Base Controller and Extends all you other controller to that base controller.

您可以创建基本控制器并将所有其他控制器扩展到该基本控制器。

Like this

喜欢这个

<?php
class MY_Controller extends CI_Controller {

public $data = array();
    function __construct() {

        parent::__construct();

        $this->data['errors'] = array();

        $this->data['site_name'] = config_item('site_name');


    }
}       

Then In Your Controller

然后在你的控制器中

class Test extends MY_Controller
{
  function __construct() {

        parent::__construct();
         $this->data['meta_title'] = 'Your Title';

 }

}

And in you views access the page title like this:

在您的视图中访问页面标题,如下所示:

echo("<title>.$site_name.</title>");

#5


3  

Controller

调节器

 $card_data= $this->Listing_model->get_card($card); //Your model returns an array of objects


 $header["page_title"]  = $card_data[0]->title; //grab value of 'title' property of first object returned from model.


 $this->load->view('includes/header',$header);

View

视图

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#6


3  

Try this:

尝试这个:

function get_card($card = FALSE)
{
    $data = $this->db->get_where('creditcards', array('slug' => $card), 0,1)->result();
    $data->title = $data[0]->title;
    return $data;
}

#7


3  

Controller

调节器

$query = $this->Listing_model->get_card($card);
var_dump($query);
//Your $query may be some data got from db;
$card_name = "";
if(!empty($query)){
$card_name = $query[0]->name;  //You must verify the name attribute and it should in the $query result;
}

$header["page_title"] = $card_name;

View

视图

<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>

#8


3  

You may need to create some routes for your show function. Codeigniter URI Routing

您可能需要为show函数创建一些路径。 Codeigniter URI路由

$route['your_controller_name/show/(:any)'] = 'your_controller_name/show/$1';

I am not sure if you have set up a htaccess for your main directory so you could remove the index.php from your url.

我不确定你是否为你的主目录设置了一个htaccess,所以你可以从你的网址中删除index.php。

Try this code below

请尝试以下代码

Model:

模型:

<?php 

class Listing_model extends CI_Model {

function get_card_title($card) {
    $this->db->where('slug', $card);
    $query = $this->db->get($this->db->dbprefix . 'creditcards');
    if ($query->num_rows() > 0) {
        $row = $quer->row();
        return $row->title;
    } else {
        return false;
    }
}

}

Controller: Your_controller_name.php

控制器:Your_controller_name.php

<?php

class Your_controller_name extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('listing_model');
    }

    public function show($card) {
        $data['title'] = $this->listing_model->get_card_title($card);

        $this->load->view('includes/header', $data);
        $this->load->view('listings/listing_card', $data);
        $this->load->view('includes/footer');
    }
}

View:

视图:

<head>
<title><?php echo $title;?></title>
</head>

#9


1  

In your listing card view, do this:

在您的列表卡视图中,执行以下操作:

foreach ($query  as  $rec){
    <title><?php echo $rec->title ?></title>
}

replace 'title' with the name of the column on your database that keeps the title of the credit card...so you are passing the results of the query you ran in your controller to this view, and then using a foreach loop to display data of the specific credit card

将'title'替换为数据库中保留信用卡标题的列的名称...所以您将在控制器中运行的查询结果传递给此视图,然后使用foreach循环显示特定信用卡的数据

#10


1  

You can use template library for robustness and use as follows:

您可以使用模板库来获得稳健性,并使用如下:

Controller

调节器

$this->template->title('Home :: ' . $this->data['metadata']['site_name'])
            ->set_layout('home_layout')
            ->build('listing_card', $this->data);

Views

查看

    <title><?php echo $template['title']; ?></title>
    <?php echo $template['metadata']; ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Reference: https://github.com/philsturgeon/codeigniter-template

参考:https://github.com/philsturgeon/codeigniter-template

#11


1  

Just to add another, there's no reason this shouldn't work:

只是为了添加另一个,没有理由这不起作用:

$data['query'] = $this->Listing_model->get_card($card);
$this->load->view('header', array('page_title' => $data['query'][0]->column_name)); 
//Will there only be one result? Consider returning $query->row(). Multiple,      
//loop through and set one title

In your view:

在你看来:

<title><?=isset($page_title) ? $page_title : "";?></title>

If this doesn't work your query isn't returning what you think it is.

如果这不起作用,则查询不会返回您认为的内容。

#12


1  

Controller :

控制器:

$data["page_title"] = $result[0]['title_field'];

view: and You just need to write in your header file like :

view:你只需要在头文件中写入:

<title><?php echo $page_title; ?></title>

#13


1  

In your model - don't return $query->result(), just return $query:

在你的模型中 - 不要返回$ query-> result(),只返回$ query:

function get_card($card = FALSE)
{
    $query = $this->db->get_where('creditcards', array('slug' => $card), 0,1);
    return $query;
}

Controller:

控制器:

public function show($card = NULL)
{
    // verify that you got something back from the database
    // or show an error 
    if( ! $query = $this->Listing_model->get_card($card) )
    {
        $this->_showNoResultsFor($card) ; 
    } 
    else
    {

        // get one record from the query using row()
        $onecard = $query->row() ; 

        // assign the title using whatever your field name is called 
        $header["page_title"] = $onecard->thetitle ; 

        // Now assign the query result() to data 
        $data['query'] = $query->result() ; 

        $this->load->view('includes/header',$header);
        $this->load->view('listings/listing_card',$data);
        $this->load->view('includes/footer');

    }    

}