在Codeigniter Controller中显示数据库结果数组,并在视图中传递

时间:2018-10-30 18:11:48

标签: php codeigniter-3

我们如何从控制器的结果数组中获取特定详细信息,并将结果数组传递给视图。下面,我编写了代码,用于在codeigniter模型中从数据库中获取数据,然后将其包含在控制器中,并且还可以将其传递来查看和回显那里的结果,但是我想获取一些特定的列结果(Metatitle,Metadesc ,metakeywrd),因此我只能在控制器中设置meta_title,meta_description,meta_keywords值并将其动态传递给head来查看

这是我的控制器

<?php
class India extends CI_Controller {

     public function __construct()
    {
        parent::__construct();       
    }

public function memberview()
    {
     $data['meta_title'] = '';
     $data['meta_description'] = '';
     $data['meta_keywords'] = '';
     $teamid = $this->uri->segment(6);      
     $data['view'] = 'region/india/team-member-view.php';
     $this->load->model('region/India_model');
     $data['team'] = $this->India_model->tmview($teamid);
     $data['teamlist'] = $this->India_model->teamlist();
     $this->load->view('region/layout', $data);


    }


}
?>

这是我的模特

  <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class India_model extends CI_Model{

    public function tmview($teamid){
            $this->db->select('*');
            $this->db->from('ojiteam');
            $this->db->where("id",$teamid);
            $query = $this->db->get();
            return $query->result_array();

        }               
}
?>

在View中,我正在以这种方式获取数据,此方法工作正常,但在控制器上设置了头部meta标签,在控制器文件中设置了该meta标签的多个静态页面,而对于meta标签有一些动态页面,已保存到数据库列中。

                  <?php        
                  foreach($team as $value){
                      };
    ?>

<!doctype html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="content-language" content="en"/>
    <title><?php echo $meta_title; ?></title>
    <meta name="description" content="<?php echo $meta_description; ?>" />
    <meta name="keywords" content="<?php echo $meta_keywords; ?>" />

1 个答案:

答案 0 :(得分:0)

我不确定我是否了解您想要什么,但是如果我知道我想这就是您想要的?

$team = $this->India_model->tmview($teamid);
 $data['meta_title'] = $team['meta_title'];
 $data['meta_description'] = $team['meta_description'];
 $data['meta_keywords'] = $team['meta_keywords'];