CI 框架增加公用函數-如何使用Helper輔助函數


在CI框架增加一個公用的函數,或者說是要在頁面上調用一個函數,可以寫一個幫助類如:menu_helper.php。
類名必有_helper后綴名,這標識為幫助類。文件要放在application/helpers目錄下。寫好函數(方法)后,就可以在頁面或其它地方調用,調用之前要引入:$this->load->helper('menu');
然后就可以直接使用函數(方法)了。
如:
subintercept_helper.php
<?php
/**
 * 中文字符串的截取
 *
 * @access: public
 * @author: linyong
 * @param: string,$str,原字符串
 * @param: int,$len ,截取的長度
 * @return: string
 */
function utf_substr($str,$len){
    for($i=0;$i<$len;$i++){
        $temp_str=substr($str,0,1);
        if(ord($temp_str) > 127){
            $i++;
            if($i<$len){
                $new_str[]=substr($str,0,3);
                $str=substr($str,3);
            }
        }else{
            $new_str[]=substr($str,0,1);
            $str=substr($str,1);
        }
    }
    return join($new_str);
}

    /**
     * 分頁的方法,
     * 
     * @access: public
     * @author: linyong
     * @param: string,$link_url,分頁鏈接地址
     * @param: int,$per_page ,分頁的每一頁顯示行數
     * @param: int,$total_rows ,分頁的總行數
     * @return: void  
     */
    function paging($link_url,$per_page,$total_rows){
            //上一頁‘下一頁的鏈接地址
            $config["base_url"] = $link_url;
            //每頁顯示行數
            $config['per_page'] = $per_page;
            //總的頁數
            $config['total_rows'] = $total_rows;
            //首頁面效果
            $config['first_link'] = '首頁';
            //尾頁效果
            $config['last_link'] = '尾頁';
            //當前頁顯示效果
            $config['cur_tag_open']="&nbsp;<p class='page_hover'>";
            $config['cur_tag_close']="</p>";
            //自定義上一頁
            $config['prev_link'] = '';
            //自定義 下一頁
            $config['next_link'] = '下一頁';    
            return $config;
    }
?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM