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 05:04

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>
查看更多
叛逆
3楼-- · 2020-07-02 05:05

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 
查看更多
ら.Afraid
4楼-- · 2020-07-02 05:05

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>
查看更多
老娘就宠你
5楼-- · 2020-07-02 05:07

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

查看更多
我命由我不由天
6楼-- · 2020-07-02 05:09

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

查看更多
神经病院院长
7楼-- · 2020-07-02 05:10

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.

查看更多
登录 后发表回答