简体   繁体   English

如何使非codeigniter文件夹/文件中的加载codeigniter数据库库?

[英]how to make load codeigniter database library inside a non codeigniter folder / file?

i have a folder named superfunctions 我有一个名为superfunctions的文件夹

在此处输入图片说明

that folder is in same level with the controller, model and views folder 该文件夹与controller,model和views文件夹处于同一级别

is it possible to call the the database library inside the php files of that said folder. 是否有可能在该文件夹的php文件中调用数据库库。

I want to use this->db->query("") etc... 我想使用this->db->query("")等...

on or inside 在上面或里面

application/superfunctions/function1.php

function1.php is just pure function / not a class function1.php仅仅是纯函数/不是类

The proper way to achieve your goal is to make it a helper file. 实现目标的正确方法是使其成为帮助文件。

/application/helpers/function1.php /application/helpers/function1.php

function1.php function1.php

<?php
if(!function_exists('function1'))
{
    function function1()
    {
        // Get the CodeIgniter instance by reference
        // Basically, $this from the controller is now $CI within this function
        $CI = &get_instance();

        $CI->db->query("");
        // do whatever

        return 'hi';
    }
}

So whenever you need it in your controller then just do: 因此,只要您在控制器中需要它,就只需执行以下操作:

class Welcome extends CI_controller {

    public function __construct()
    {
        $this->load->helper('function1');
    }

    public function index()
    {
        echo function1();
    }
}

why not create a library with those functions, and autoload that library On the other hand, you can create a class in you sub folder like 为什么不使用这些功能创建一个库并自动加载该库,另一方面,您可以在子文件夹中创建一个类,例如

<?php
class My_super_class{

    protected $CI;

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

        $this->CI =& get_instance();
    }

    public function do_somthing( $param1 = NULL ){
        //param1 as array
        $this->CI->db->insert('table', $param1);
    }

    public function check_login(){
        $this->CI->load->library('session');

        return ( ! empty($this->CI->session->userdata('id'))) ? TRUE : FALSE;
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM