如何使用模型和自定义助手显示数据库中的数据? [英] How to display data from the database using models and custom helpers?

查看:51
本文介绍了如何使用模型和自定义助手显示数据库中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个新的应用程序功能,在该功能中,我想动态更改从数据库中获取的页面标题,页面标题标题和面包屑的值,而不是手动对其进行单独编码.关键是,如果我更新数据库中的menu_name列,则所有页面标题,页面标题标题和面包屑都会根据其值自动更新.

I am currently creating a new application feature where in I want to dynamically change the values of page headers, page header titles, and breadcrumbs that are fetched from database and not manually code them individually. The thing is, if I update the menu_name column in the database, all page headers, page header titles and breadcrumbs will be automatically be updated based on it's values.

这是我创建的用于获取menu_master表中各列的值的模型

This is the model I created to fetch the values of the columns in the menu_master table

class Page_Header_model extends CI_Model {

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

function get_page_menu_names($menu_id, $menu_name)
{
    $this->db->where('menu_id', $menu_id);
    $this->db->where('menu_name', $menu_name);

    $query = $this->db->get('menu_master);

    return $query->result();
}

这里是我要调用模型的page_header_helper,以便自动显示值

Here is the page_header_helper that I want to call the model in order for the values to be displayed automatically

function __construct()
{
   parent::__construct();
   $CI =& get_instance();
   $CI->load->model('Page_Header_model');
   $CI->Page_Header_model->get_page_menu_names();
}

function get_page_header_data($page_type)
{
      $project_title = 'Testing';

      $page_header_array['inventory'] = array(

        "breadcrumbs" =>  array(
            array('title'=>'Home','url'=>base_url()),
            array('title'=>'Inventory Lists','is_active'=>TRUE),
        ),
        "page_title"  =>  'Inventory Lists',
        "page_header_title" =>  'Inventory Lists : '.$project_title

    );

    $page_header_data = $page_header_array[$page_type];

    return $page_header_data;
    }

推荐答案

我刚刚想出了一种方法.关键是从数据库中获取菜单名称的ID,并将其作为一行返回,在自定义帮助程序中,只需从数据库中调用菜单名称的ID.这是我的操作方式:

I just figured out a way on how to do this. The key is to get the id of the menu name from the database and return it as a row and in the custom helper, just call the id of the menu name from the database. Here is how I did it:

这是我的模特:

        function get_page_menu_names($menu_id)
       {
           $this->db->select('menu_name');

           $this->db->from('menu_master');

           $this->db->where('menu_id', $menu_id);

           $query = $this->db->get();

           $menu_name_row = $query->row();

           if(isset($menu_name_row))
           {
             return $menu_name_row->menu_name;
           }            
       }

这是我从写出的助手中调用它的方式:

And here is how I called it from the helper that I wrote out:

$page_header_array['inventory_master_listing'] = array(

            "breadcrumbs" =>  array(
                array('title'=>'Home','url'=>base_url()),
                array('title'=> $this->my_model_name->get_page_menu_names(1),'is_active'=>TRUE),
            ),
            "page_title"  =>  $this->my_model_name->get_page_menu_names(1),
            "page_header_title" =>  $this->my_model_name->get_page_menu_names(1).' : '.$project_title

        );

这篇关于如何使用模型和自定义助手显示数据库中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆