廣告2


適合宜蘭旅遊的出租套房(土水師的家)
想體驗獨特的藝術空間,感受老闆對每個房間的用心,歡迎來土水師的家。
電話:0918667109 林先生
地址:宜蘭縣礁溪鄉和平路75號
註:記得跟老闆說,"我是阿志的朋友,請給我優惠一點"。
顯示具有 codeigniter 標籤的文章。 顯示所有文章
顯示具有 codeigniter 標籤的文章。 顯示所有文章

2015年6月10日 星期三

[CodeIgniter]CSV匯出下載

    controllers 程式
   
    public function csv()
    {
   
     $this->load->helper('download');   //下載函數
     $this->load->dbutil();               
     $sql = "SELECT * FROM class_item order by class_item_sort";
     $query =$this->sql_base->sql_result($sql);
     $content=$this->dbutil->csv_from_result($query);
     $name = 'test.csv';
     force_download($name,iconv("UTF-8","Big5",$content));
    }

[CodeIgniter]Download 輔助函數

Download 輔助函數


使用下列的程式碼載入補助程式:

$this->load->helper('download');
下列為可以使用的函數:

force_download('filename','data')

產生伺服器的標頭,強制資料被下載到您的電腦桌面。對檔案下載有幫助。第一個參數是你想要下載的檔案的名稱,第二個參數是檔案資料。 參考範例:

$data = 'Here is some text!';
$name = 'mytext.txt';

force_download($name,$data);
如果你想在你的伺服器上下載一個現有的檔案,你必需將檔案讀到一字串中:

$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';

force_download($name,$data);



=================================================================================================


controller程式

  public function download()
  {
     
    $this->load->helper('download');  //載入
   
    $info_file2_field = $this->config->item('info_file2_field'); //讀取設定
    $file_name = $this->input->get('f');
   
   
    if (file_exists($info_file2_field[0][1].$file_name))
    {
            
            $data = file_get_contents($info_file2_field[0][1].$file_name); // Read the file's contents
            force_download($file_name,$data);
    }
    else
    {
      echo "檔案不存在";   
      exit();
    }

2015年5月28日 星期四

CI之CSRF設定

config\config.php

設定
$config['csrf_protection'] = TRUE;


view
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name()?>" value="<?php echo $this->security->get_csrf_hash(); ?>" />

註:檢查表單是否有值

2015年5月27日 星期三

CI預設路徑FCPATH、BASEPATH、APPPATH、VIEWPATH

FCPATH   -> '/'
BASEPATH -> '/system/'
APPPATH  -> '/application/'
VIEWPATH  -> '/application/views/'

2015年5月21日 星期四

CI3.0使用DB driver 當session

config\config.php 設定

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';


DB設定,使用mysql
CREATE TABLE `ci_sessions` (
  `id` varchar(40) NOT NULL,
  `ip_address` varchar(45) NOT NULL,
  `timestamp` int(11) NOT NULL,
  `data` blob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `ci_sessions`
  ADD PRIMARY KEY (`id`), ADD KEY `ip_address` (`ip_address`);


controller 初始化

 public function __construct()
 {
  parent::__construct();
  $this->load->driver('session');
 }


$this->session->userdata('item');  讀取session
$this->session->set_userdata('some_name', 'some_value');  設定session
$this->session->unset_userdata('some_name');  刪除session




2015年4月30日 星期四

CodeIgniter]靜態頁面 Controller + View

靜態頁面 Controller + View

class Page extends CI_Controller {

 public function __construct()
 {
   parent::__construct();
   $this->load->helper('url');
 }
 
 public function index()
 {
   $page = $this->uri->segment(3);
   $this->view($page);
 }

 public function view($page = 'welcome_message')
 {
   $data['title'] = '';
   $this->load->view($page, $data);
 }
}

例:http://localhost/page/view/p2


2015年4月22日 星期三

刪除CodeIgniter index.php


步驟一:
接著先把剛剛的.htaccess檔案複製到c:\AppServ\www\底下,開啟檔案修改。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>


步驟二:
application\config\config.php
$config['index_page'] = '';   //將index.php 改成空白


步驟三:
請確認Apache rewrite已正確啟用

2015年4月14日 星期二

[CodeIgniter] 常用helper函數

Cookie Helper
裝載這個 helper這個 helper 可以通過下面的方法來裝載:
$this->load->helper('cookie');
可以加載庫後直接使用函數:
set_cookie().
get_cookie().
delete_cookie()

Email 輔助函數
本輔助函數的裝載通過如下代碼完成:  $this->load->helper('email');
valid_email('email')
檢查 email 是否是一個正確的 email 地址格式。請注意,這實際上並不表示這個地址能接收郵件,只是簡單地說明這是一個有效的地址格式這個函數返回 TRUE/FALSE
send_email('recipient', 'subject', 'message')
使用 PHP 的本地 mail() 函數發送一封 Email。如果需要更強大的 Email 解決方案,請參考 CodeIgniter 的 Email 類

HTML輔助函數
我們可以在控制器裡面加載html,然後可以在View裡面進行輸出:
$this->load->helper('html');
br() 生成指定個數的換行標籤 () 。例如: echo br(3);
heading() echo heading('Welcome!', 3, 'class="pink"')
將會生成:Welcome!


img()  幫助你創建 HTML  標籤
link_tag() 幫助你創建 HTML  標籤。在鏈接樣式表以及其他內容時非常有用。
nbs() 生成不換行的指定個數的空格標籤( )。例如:echo nbs(3);
ol() 和 ul() 允許你通過簡單或多維的數組生成有序或無序的HTML列表
meta()  幫助你創建meta標籤. 你可以將字符串、簡單數組或者多維數組傳遞給函數. 例如: echo meta('description', 'My Great site');
doctype() 幫助你創建文檔類型聲明以及DTD。默認值是 XHTML 1.0 Strict ,但你也可以指定其他很多文檔類型。 echo doctype();

Inflector 輔助函數
Inflector 輔助函數文件包含允許你把單詞更改為複數、單數或駱駝拼寫法等形式的函數。 $this->load->helper('inflector');
singular()  把一個單詞的複數形式更改為單數形式
plural()     把一個單詞的單數形式更改為複數形式
camelize()  把一個以空格或下劃線分隔的單詞字符串更改為駱駝拼寫法
underscore() 把以空格分隔的多個單詞更改為以下劃線分隔
humanize() 把以下劃線分隔的多個單詞更改為以空格分隔,並且每個單詞以大寫開頭


URL 輔助函數
URL 輔助函數文件包含一些在處理 URL 中很有用的函數 
$this->load->helper('url');
site_url()  做為參數傳遞給該函數的 URI 段可以是一個字符串,也可以是一個數組. 下面是一個字符串的例子:  echo site_url("news/local/123");
base_url()  返回在 config.php 中設定的 base_url. 例: echo base_url();
current_url() 返回當前正在查看的頁面的完整URL(包括段)。
uri_string()  返回此函數的頁面的URI段。
index_page()  返回在 config.php 中設定的 index_page.
anchor() 創建基於你的本地站點URL .
例如echo anchor('news/local/123', 'My News', 'title="News title"');
anchor_popup()
幾乎和anchor() 函數相同,區別是它會在新窗口打開鏈接. 你可以在第三個參數中指定JavaScript窗口屬性來控制窗口的打開方式
mailto() 創建標準HTML電子郵件鏈接.
safe_mailto()
用法和上面的函數相同,區別是它用JavaScript寫了基於順序號碼的不易識別的mailto版本標籤,可以阻止email地址被垃圾郵件截獲.
redirect()
通過發送HTTP頭,命令客戶端轉向到您指定的URL。您既可以指定一個完整的URL,也可以對於站內內容,指定基於網站根目錄的相對URL。本函數會自動根據您的配置文件,構造出完整的URL。

表單輔助函數
用下面的代碼載入該輔助函數: $this->load->helper('form');
form_open() 創建一個開始form標籤
form_open_multipart()
這個函數和上面的form_open()函數完全一樣,不同之處在於它多了一個multipart屬性。如果你要製作一個上傳文件的表單,這個屬性是必須的。
form_hidden()
可以使你創建一個隱藏輸入欄。你可以輸入name和value來創建一個:
form_input()
可以使你創建一個標準輸入欄。你可以在第一和第二個參數里輸入name和value來創建.

目錄輔助函數
請使用如下代碼載入這個輔助函數: $this->load->helper('directory');
directory_map('source directory')
這個函數將讀取第一個參數所給出的路徑的目錄,並且返回該目錄所包含文件的數據。

日期輔助函數
本輔助函數的裝載通過如下代碼完成: $this->load->helper('date');
timezone_menu()  生成一個時區下拉選單,像這樣:
echo timezone_menu('UP8');
文本輔助函數
採用如下方式裝載該輔助函數:$this->load->helper('text');
word_limiter()
根據指定的詞語(由於是英語,對中文應該是以空格為判斷標準,譯者注)數目對一段字符串進行截取
word_censor()
讓你可以對文本中的文字進行審核替換。第一個形參用於獲取原始字符串。第二個形參用於存放你不允許的文字的數組。第三個形參(可選)用於存放一個替換不允許文字的字段。
highlight_code()
對一段代碼(PHP,HTML等)進行著色
本函數使用PHP的 highlight_string() 函數,因此所使用的顏色是你在 php.ini 文件中指定的那些。
highlight_phrase()
對字符串內的一個短語進行突出顯示。第一個參數是原始字符串,第二個參數是你想要突出顯示的短語。如果要用HTML標籤對短語進行標記,那麼第三個和第四個參數分別是你想要對短語使用的HTML打開和關閉標籤。

件輔助函數
使用以下代碼:$this->load->helper('file');
read_file('path')
返回路徑為path的文件內容
write_file('path', $data)
寫進數據到path所指向文件。如果文件不存在則創建之
delete_files('path')
刪除所有包含於path下的文件
get_filenames('path/to/directory/')
獲取path/to/directory目錄下所有文件名組成的數組。如果需要文件名中有其完整路徑則可以設置可選的第二個參數為TRUE。
get_dir_file_info('path/to/directory/', $top_level_only = TRUE)
獲取path/to/directory/目錄下的所有文件的文件名,文件大小,日期,文件權限等,並將這些內容保存到返回的數組當中。
get_file_info('path/to/file', $file_information)
通過給定的路徑和文件名,獲取到文件path/to/file的文件名,文件大小,文件更改日期等。第二個參數允許你說明需要返回的信息,這個參數的選項包括'name', 'server_path', 'size', 'date', 'readable', 'writable', 'executable', 'fileperms'。如果文件不存在則返回FALSE。
symbolic_permissions($perms)
將數字式的權限表示方式(如fileperms()函數所返回值)轉換成採用標準符號的標示方式,例如:"33279"轉換成"-rwxrwxrwx")。
octal_permissions($perms)
將數字式的權限表示方式(如fileperms()函數所返回值)轉換成採用三字符的八進制的標示方式,例如:"33279"轉換成"777")。


下載輔助函數
用下面的代碼加載這個輔助函數$this->load->helper('download');
force_download('filename', 'data')
服務器產生能下載數據到你桌面的頭. 這對你下載文件有幫助. 第一個參數是下載文件的文件名, 第二個參數是文件數據. Example:
$data = 'Here is some text!';
$name = 'mytext.txt';
force_download($name, $data);

如果你想在你的服務器上下載一個存在文件,你需要將它讀到一個字符串中:
$data = file_get_contents("/path/to/photo.jpg"); // 讀文件內容
$name = 'myphoto.jpg';
force_download($name, $data);

CAPTCHA 輔助函數
用下面的代碼加載驗證碼輔助函數: $this->load->helper('captcha');
create_captcha($data)
根據你指定的一系列參數創建驗證碼圖像, 返回值是一個包含此圖像數據的數組.
[array]
(
   'image' => IMAGE TAG
   'time' => TIMESTAMP (毫秒)
 'word' => CAPTCHA WORD
)

字符串輔助函數
採用如下方式裝載該輔助函數:$this->load->helper('string');
random_string()
根據你所指定的類型和長度產生一個隨機字符串。可用於生成密碼串或隨機字串。第一個參數指定字符串類型,第二個參數指定其長度。
alternator()
當執行一個循環時,讓兩個或兩個以上的條目輪換使用。範例:
for ($i = 0; $i < 10; $i )
{
 echo alternator('string one', 'string two');
}
repeater()
重複生成你所提交的數據。
reduce_multiples()
去掉多餘的一個緊接著一個重複出現的特殊字符。

引用網址:http://qing.blog.sina.com.cn/tj/788e5b7a330027vy.html

2015年4月13日 星期一

[CodeIgniter]多文件上傳

  //載入所需類庫
    $this->load->library('upload');
 
    //配置上傳參數
    $upload_config = array(
            'upload_path'   => '',
            'allowed_types' => 'jpg|png|gif',
            'max_size'      => '500',
            'max_width'     => '1024',
            'max_height'    => '768',
    );
    $this->upload->initialize($upload_config);
     
    //循環處理上傳文件
    foreach ($_FILES as $key => $value) {
        if (!empty($key['name'])) {
            if ($this->upload->do_upload($key)) {
                //上傳成功
                print_r($this->upload->data());
            } else {
                //上傳失敗
                echo $this->upload->display_errors();
            }
        }
    }

註:
//設定
// 允許 Word 檔上傳
$config['allowed_types'] = 'doc|docx';
// 允許圖檔上傳
$config['allowed_types'] = 'gif|png|jpg|jpeg|jpe';
// 不限制上傳檔案類型
$config['allowed_types'] = '*';

//單檔上傳程式
           if ($this->upload->do_upload('file1')) {    //<input name="file1" type="file" id="file1">
                //上傳成功
                print_r($this->upload->data());
            } else {
                //上傳失敗
                echo $this->upload->display_errors();
            }


2015年3月30日 星期一

[CodeIgniter]models中使用library

class User_model extends Model {

  function User_model() {
    parent::Model();
    $this->check_login();
  }

  function check_login() {
    $this->load->library('encrypt');
    $email = $this->encrypt->decode($email);
    ....
  }
}

2015年3月24日 星期二

[CodeIgniter]分頁加搜尋

//=====================================
//            Controller
//=====================================

 public function __construct()
 {
   parent::__construct();
   $this->load->library('myclass');
   $this->load->helper('url');  
   $this->load->library('pagination'); //載入分頁  
   $this->load->helper('form');
   //載入資料庫行為
   $this->load->model('sql_base');
   //確認權限
   $this->myclass->is_power_check();
   
 }

public function index()
{


  $data['man_top'] = $this->load->view('manage/man_top','', true); // include top
  $data['page_title'] = $this->config->item('bk_title');

  //====================
  //    分頁設定
  //====================
 
  $sql = "select * from info i
          left join class_item c on c.class_item_id=i.class_item_id
          where 1 ";

  $info_title = $this->input->get('info_title');
  if($info_title!="")
  {$sql = $sql . "and i.info_title like '%".$info_title."%' ";}
 
  $class_item_id = $this->input->get('class_item_id');
  if($class_item_id!="")
  {$sql = $sql . "and i.class_item_id=".(int)$class_item_id."  ";}
 
  $sql = $sql . "order by i.info_date desc,i.info_id desc";


  //計算記錄總數
  $config['total_rows'] = $this->sql_base->sql_result_row($sql);
  //設置本頁路徑
  $config['base_url'] = site_url()."/manage/info?".$this->myclass->remove_qs_key($_SERVER['QUERY_STRING'],'per_page');
  //設置每頁顯示記錄數
  $config['per_page'] = 3;
  //取目前分頁的參數(依路徑位置,例:/manage/info/index/1,第4個參數)
  $config['uri_segment'] = 4;
  //設置分頁導航條樣式
  $config['first_link']      = '首頁';
  $config['last_link']       = '末頁';
  $config['next_link']       = '下一頁>';
  $config['prev_link']       = '<上一頁';
  $config['page_query_string'] = TRUE;  //使用參數方式
  $offset=(int)$this->input->get('per_page');
 
  //分頁初始化
  $this->pagination->initialize($config);
 
  //查詢返回陣列
  $query = $this->sql_base->sql_result($sql." limit ".$offset.",".$config['per_page']);
  //分頁連結
  $data['options'] = $this->sql_base->get_dropdownlist("select * from class_item order by class_item_sort",'class_item_id','class_item_name');
  $data['page_links'] = $this->pagination->create_links();
  $data['query'] =  $query;
  $this->load->view('manage/info', $data);  //將date帶入View

}

//=====================================
//            view
//=====================================
 <form action="<?=site_url();?>/manage/info" method="get" name="srh">
        <tr>
        <td><table width="697" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF" class="tablebgcolor">
          <tr>
            <td width="39" class="titlebgcolor2">
         <div align="left" class="word01">分類</div></td>
            <td width="643" class="titlebgcolor2"><?php
           
echo form_dropdown('class_item_id', $options,'');
?></td>
          </tr>
          <tr>
            <td class="titlebgcolor2"><div align="left" class="word01">標題</div></td>
            <td class="titlebgcolor2"><label>
                <input type="text" name="info_title" id="info_title">
                <span class="word01">(請入關鍵字)
              <input name="submit"  type="submit" value="查詢" >
              </span></label></td>
            </tr>
        </table></td>
      </tr></form>


     //顯示結果列表
     foreach ($query->result_array() as $row){
           echo $row['info_id'];
     }


//顯示分頁
echo $page_links;  


//=====================================
//            model
//=====================================

 //return sql array
 public function sql_result($sql){
  return $this->db->query($sql);
 }
 //run sql
 public function sql_run($data){  
    $this->db->query($data);
 }
 //return sql total row
 public function sql_result_row($sql){  
  $query = $this->db->query($sql);
  return $query->num_rows();
 }

 //return 下拉選單
 function get_dropdownlist($sql,$id,$name,$ct1=0)
 {
$result = $this->db->query($sql);
    $return = array();
    if($result->num_rows() > 0){
        if($ct1==0){$return[''] = '請選擇';}
        foreach($result->result_array() as $row){
            $return[$row[$id]] = $row[$name];
        }
    }
    return $return;
 }