php - codeigniter 中的模板

标签 php mysql codeigniter

我正在尝试使用模板方法开发一个网站 我已经创建了不同的文件并想运行一个查询,并且该查询将在所有模板上可用,是否可以这样做?就像我想给你一个例子,我在 Controller 中做了什么,我创建了一个这样的变量

$data['navigation'] = 'templates/main_menu';
$this->load->view('main');

好吧,现在我在 main View 中所做的是我在其中包含页眉和页脚,并像这样动态调用导航文件

$this->load->view($navigation);

好吧,现在我想做的是将图标存储在数据库和其他设置中,所以我在头文件中放置了一个查询

<?php $settings = $this->db->get_where('settings', array('id' => 2));\\ This query is just placed above the doctype in the header file and I would like to be called every where  ?>

templates/main_menu.php
$settings->row()->header_bg_color

但我收到一个错误消息: undefined variable :设置,如果我将此设置查询放在 main_menu.php 文件中,它的工作方式就像应该的方式一样我的意思是再次调用查询没有意义再一次,如果我需要最好的方法是什么,模型的用途是什么,如果是的话,它会被用于查询吗,如果我把这个设置查询放在模型中,我将如何访问它是可能的请多多指教

谢谢

最佳答案

hi 下面是 Controller 的代码。我已经创建了 common_library.php 文件,我在其中编写了我想要使用所有 View 的所有常用函数。

class Maintenance extends CI_Controller {
function Maintenance() {
parent::__construct();
// file for all common function 
    include('application/controllers/common/common_library.php');
    $this->load->model('maintenance_model');
    $this->load->library('session');
    $this->load->library('form_validation');
}
function index(){
/*
    *   @ desc : default function that loads the maintenance_view.
    *   
    *   
    */
    $data['common_query']   = commonQuery() // define this function in common_library file
    $data['model_data'] = $this->maintenance_model->projectdetail();
    $data['formtitle']  = 'Maintenance List | BMS';

    $this->load->view('maintenance_view',$data);
}
}

关于php - codeigniter 中的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369925/

相关文章:

javascript - 语法错误: Unexpected token s in JSON at position 1

PHP 使用 LetsEncrypt 验证失败

mysql - 如何在Mysql中查找相似命名的数据库

javascript - 如何将每个查询结果存储在另一个数组中并在最后返回到 Controller

javascript - 使用 AJAX 和 Codeigniter 上传多个文件

php - 将mysql数字更改为文本

php - 如何在 symfony 的 Twig 日期中显示日期名称

php - 在XAMPP中创建数据库(MySQL PDO)并使用php代码连接它

php - 如何将结果从 MySQL 存储到 PHP 数组?

php - sendmail 在 CodeIgniter 上失败,但在 CLI 上有效