CI框架常用代码1

CI框架:
1.替代语法:<?php echo $test;?> → <?=$test;?>
        <?php foreach($list as $tiem):?>
        <li><?=$item?> </li>
        <?php endforeach;?>
2.读取配置文件数据(config.php):
        $config['pcount']=10;  [config.php]
        $pagesize=intval($this->config->item('pcount'));
3.读取uri参数:
        $page=intval($this->uri->segment(5));
4.配置Sql数据库连接:
        $db['default']['hostname'] = '.';
        $db['default']['username'] = 'sa';
        $db['default']['password'] = '52ebook';
        $db['default']['database'] = 'SDMS_DB';
        $db['default']['dbdriver'] = 'sqlsrv';
        $db['default']['dbprefix'] = '';
        $db['default']['pconnect'] = FALSE;
        $db['default']['db_debug'] = TRUE;
        $db['default']['cache_on'] = FALSE;
        $db['default']['cachedir'] = '';
        $db['default']['char_set'] = 'utf8';
        $db['default']['dbcollat'] = 'Chinese_PRC_CI_AS';
        $db['default']['swap_pre'] = '';
        $db['default']['autoinit'] = TRUE;
        $db['default']['stricton'] = FALSE
5.域名判断:
    switch ($_SERVER['HTTP_HOST']) {
         case 'sanofi.gooddr.com'://网站域名
            define('SQLSERVER', true);
            $application_folder = './application/sanofi_new'; //项目目录
            break;
        case 'report.gooddr.loc'://网站域名
            $application_folder = './application/data'; //项目目录
            break;
        case 'sanofi.gooddr.loc'://网站域名
            $application_folder = './application/sanofi'; //项目目录
            break;
          case 'www.yishijob.com'://网站域名
            $application_folder = './application/yishijob'; //项目目录
            break;
    }
6.自定义库:
    类名大写,引用小写
7.路径:
    <?php  echo base_url();?>theme/assets/admin/layout3/img/avatar1.jpg
    site_url()                                                        [index.php]
8.自定义类库调用CI类:
    public function __construct()
            {
                $this->CI =& get_instance();
                $this->CI->load->database();
            }
    //实现MySQL数据分页
            public function GetPage($count_sql,$sql,$currentpage=1,$pagesize=10)
            {
                $cpage=($currentpage-1)*$pagesize;
                $pagecount=$this->CI->db->query($count_sql)->result_array();
                $pcount=ceil($pagecount[0]['pagecount']/$pagesize);
                $sql.=" limit $cpage,$pagesize";
                $selresult=$this->CI->db->query($sql)->result_array();
                $data=array(
                    'pagecount'=>$pcount,
                    'data'=>$selresult
                );
                return $data;
            }
9.页面导航:
    <a class="btn btn-link" href="<?php  echo site_url();?>/welcome/page/last/<?php echo $currentpage;?>/">上一页</a>
    <a class="btn btn-link" href="<?php  echo site_url();?>/welcome/page/next/<?php echo $currentpage;?>/">下一页</a>
10.官方支持分页:
    Control:
            $page=1;
            $currentpage=1;
            if(!empty($this->uri->segment(3)))
            {
                $currentpage=$this->uri->segment(3);    
            }
                
             $this->load->library('pagination');
             $config['base_url'] = 'http://ci/index.php/welcome/index';
             $config['total_rows'] = 200;
             $config['per_page'] = 10; 
             $config['use_page_numbers'] = TRUE;    
             $this->pagination->initialize($config);
             
            $count_sql="select count(id) as pagecount from tk_main_question";
            $sql='select id,serial_code,caption from tk_main_question';
                
            $data=$this->common->GetPage($count_sql,$sql,$page,$currentpage,10);
            
            $this->load->view('pageindex2',$data);
        
        View:
            <?php echo $this->pagination->create_links();?>
11.读取配置文件:
            $this->config->load('sanofi');
            $sanofi=$this->config->item("sanofi");
            $data=array(
                'qc_list'=>$qc_list,
                'per'=>$per[0]['qc_per'],
                'reject_reason'=>$sanofi['reject_reason']
            );
12.插入数据后获取最后一位自增长id;
    $this->db->query($sql);
    $qc_id=$this->db->insert_id();
13.获取最后一次查询:
    $this->db->last_query();
14.初始化配置:
    config/config:
        $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];
        $config['index_page'] = '';
    .htaccess
        RewriteEngine on
        RewriteCond $1 !^(index.php|theme|css|img|js|cron|libs|uploads|css|tmp|robots.txt)
        RewriteRule ^(.*)$ index.php/$1 [L]
15.分页实现:
     //分页处理
            $page = $this->uri->segment(3) ? : 1;
            $per_page = $config['per_page'] = 10;
            $offset = $this->uri->segment(4);
            $config['base_url'] = '/datum/datum_list/'.$page.'/';
            $config['uri_segment'] = 4; 
            $offset = $offset ? :0;
            
            $sql="select count(id) as total_count from artices;";
            $total_db_result=$this->db->query($sql)->result_array();
            $sql="select * from artices order by id desc limit {$offset},{$per_page}";
            $db_result=$this->db->query($sql)->result_array();
            
            $num_rows = $config['total_rows'] = $total_db_result[0]['total_count'];
            $this->pagination->initialize($config);
            $page_link = $this->pagination->create_links();
16.文件上传+缩略图:
    //图片上传
            $config['upload_path'] = 'uploads/articles/';
            $config['allowed_types'] = 'jpg|png|jpeg|bmp';
            $this->load->library('upload', $config);
            $img='';
            if($this->upload->do_upload('img'))
            {
                $data = $this->upload->data();
                $path = './uploads/articles/'.$data['orig_name'];
                $ext=end(explode('.',$data['orig_name']));
                $new = time().$data['raw_name'].'.'.$ext;
                $src = '/uploads/articles/'.str_replace('.'.$ext, '_thumb.'.$ext, $new);
                $this->commfunctions->thumb_img($path,$new);
                unlink($path);
                $img=$src;
            }
17.CI+bootstrap 分页
                  //分页处理
            $page = $this->uri->segment(3) ? : 1;
            $per_page = $config['per_page'] = 10;
            $offset = $this->uri->segment(4);
            $config['base_url'] = '/templet/templet_list/'.$page.'/';
            $config['uri_segment'] = 4;
            $config['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
            $config['full_tag_close'] = '</ul></div>';
            $config['anchor_class'] = "";
            $config['cur_tag_open'] = '<li class="active"><a>';
            $config['cur_tag_close'] = '</a></li>';
    
            $config['next_link'] = '下一页';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
    
            $config['prev_link'] = '上一页';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
    
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
    
            $config['first_link'] = '首页';
            $config['first_tag_open'] = '<li class="prev page">';
    
            $config['first_tag_close'] = '<li class="next page">';
            $config['last_link'] = '尾页';
    
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
    
            $offset = $offset ? :0;
            
            $sql="select count(id) as total_count from templates where status=1;";
            $total_db_result=$this->db->query($sql)->result_array();
            $sql="select * from templates where status=1 order by id desc limit {$offset},{$per_page}";
            $db_result=$this->db->query($sql)->result_array();
            
            $num_rows = $config['total_rows'] = $total_db_result[0]['total_count'];
            $this->pagination->initialize($config);
            $page_link = $this->pagination->create_links();
             
            $data=array(
                 'uid'=>$uid,         //用户id
                 'templet_list'=>$db_result,
                 'total_rows' => $total_db_result[0]['total_count'],
                 'page_link' => $page_link,
            );
            //载入模板
            $this->load->view('head.php', $data);
            $this->load->view('sidebar.php', $data);
            $this->load->view('/follow/templet_manager.php',$data);
            $this->load->view('foot.php', $data);
18.数据库操作所影响的行数:
        $result=$this->db->affected_rows();
19.CI事务处理:
        $this->db->trans_start();
        $this->db->trans_complete();
原文地址:https://www.cnblogs.com/zhaobijin/p/5807790.html