How to fetch title of an item from a database and

2020-07-02 04:22发布

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?

13条回答
何必那么认真
2楼-- · 2020-07-02 04:50

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楼-- · 2020-07-02 04:50

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>");
查看更多
做个烂人
4楼-- · 2020-07-02 04:52

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

    }    

}
查看更多
\"骚年 ilove
5楼-- · 2020-07-02 04:58

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>
查看更多
甜甜的少女心
6楼-- · 2020-07-02 04:58

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>
查看更多
做自己的国王
7楼-- · 2020-07-02 05:04

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>
查看更多
登录 后发表回答