How to fetch title of an item from a database and send it to the header template in CodeIgniter

喜夏-厌秋 提交于 2019-12-09 14:05:58

问题


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:

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?


回答1:


Try this

  1. Model is changed
  2. Controller is changed.

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:


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

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:

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

your limit is 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:


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:


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:


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:


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:


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:


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

$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.

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

<?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:


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




回答10:


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




回答11:


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:


Controller :

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

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

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



回答13:


In your model - don't return $query->result(), just return $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');

    }    

}


来源:https://stackoverflow.com/questions/34467450/how-to-fetch-title-of-an-item-from-a-database-and-send-it-to-the-header-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!