自由學習的風

幽夢影 張潮 少年讀書,如隙中窺月;中年讀書,如庭中望月;老年讀書,如臺上玩月。皆以閱歷之淺深,為所得之淺深耳。
顯示具有 CodeIgniter 標籤的文章。 顯示所有文章
顯示具有 CodeIgniter 標籤的文章。 顯示所有文章

[轉][CodeIgniter] 利用 hook 來處理全部的輸出

2017年9月17日 星期日

參考 http://lzzpnk.blogspot.tw/2012/06/codeigniter-hook-alt.html

1. 在 ./application/config/config.php
 $config['enable_hooks'] = TRUE;


2. ./application/config/hooks.php 設定執行的程式
$hook['display_override'][] = array(
        'class'    => '',
        'function' => 'images_auto_set_alt',
        'filename' => 'output.php',
        'filepath' => 'hooks',
        'params'   => array( 'output_display' => TRUE )
    );​ 
3. ./application/hook/output.php
/**
     * 將輸出的 圖片(<img>) 進行處理;自動配置 alt 屬性。
     *
     * @param array   $setting=array() 設定
     *                $setting['output_display'] => 是否直接輸出給瀏覽器。FALSE代表不直接輸出,僅回存至buffer。
     * @return NULL                    不會回傳東西。
     */
    function images_auto_set_alt( $setting=array() ) {
        $CI =& get_instance();
        $CI->load->library( 'simple_html_dom' ); // require_once APPPATH . 'libraries/simple_html_dom.php';
        $buffer = $CI->output->get_output();
        $DOM = str_get_html( $buffer );
        foreach ( $DOM->find( 'img' ) as $key => $img ) {
            if ( empty( $img->alt ) && ! strpos( $img->alt, $CI->config->item( 'site_name' ) ) ) {
                $img->alt = $CI->config->item( 'site_name' );
            }
            else {
                $img->alt .= ' - ' . $CI->config->item( 'site_name' );
            }
        }
        $CI->output->set_output( $DOM->save() );
        if ( $setting['output_display'] ) {
            $CI->output->_display();
        }
    } 

[CodeIgniter] 將 Session 資料存入資料庫

2017年9月2日 星期六

Reference: http://superlevin.ifengyuan.tw/codeigniter3-0%E5%B0%87session%E5%AF%AB%E5%85%A5db/

CREATE TABLE IF NOT EXISTS `ci_sessions` (        `id` varchar(40) NOT NULL,        `ip_address` varchar(45) NOT NULL,        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,        `data` blob NOT NULL,        PRIMARY KEY (id),        KEY `ci_sessions_timestamp` (`timestamp`));


設定 application/config/config.php
$config['sess_driver'] = 'database';$config['sess_save_path'] = 'ci_sessions';









[CodeIgniter] 讀取 2 個資料庫

Reference: stack overflow https://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections#

同時連線2個資料庫

Database 1:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Database 2:
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;

Use it:
function my_model_method()
{
  $otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.

  $query = $otherdb->select('first_name, last_name')->get('person');
  var_dump($query);
}