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();