Professional Documents
Culture Documents
Ly do: Khi s dng th vin, helper ,model , view bn trong controller ch cn s dng
$this->load-> nhng khi s dng ngay bn trong helper hoc library th chng ta khng
th s dng $this c m chng ta cn khi to dng siu i tng mi bng cach s
dng & get_instance().
- To file helper yu cu inh dng ten_helper
Load helper $this->load->helper(); tn k bao gm _helper
- To file library yu cu tn file ging tn class
Load libarary $this->load->library();
- To model yu cu tn file ging tn class
Load file model t 1 th mc cn truyn ng dn tng i
$this->load->model(blog/queries)
Load model v i tn model qua tham s th 2
$this->load->model (model_name,ten_thay_doi)
Load database $this->load->database(options,true/false)
Hm tai file $this->load->file(filepath/filename,true/false) false tra v trnh duy t, true
tra v chui
-Load ngn ng $this->load->language(ten file,ten folder) ,hm tt $this->lang>load(ten,tham so thu 2 co the co hoac khog(de lua chon goi ngon ngu)khng co se lay
goi mac dinh trong config languge
-Load file config $this->load->config(ten) ,hm tt $this->config->load()
- t cac goi ng dng trong th mc application/third_party
S dng trong controller :
$this->load->add_package_path(APPPATH.'third_party/foo_bar/');
$this->load->library('foo_bar')
$this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
1.Xy dng th vi n load template /vlogplus/application/libraries/template.php
- To library class template
* To phng thc set ($file,$data)
+ x ly vi thng tin trong layout
if (strpos($file, '/header') == TRUE)
if (strpos($file, '/left') == TRUE)
if (strpos($file, '/right') == TRUE)
{ Tra li cac bin gia tri $data[]}
Return $this->load->view($file,$data,TRUE); s dng tai file view v. $file l tn file
mun tai khng cn ui .php, $data l mang ho c project chng se i qua b hm PHP
extra view co th hiu c, thng s th 3 m c inh l false nu l false d li u tra
v cho trnh duy t nu l true d li u tra v dng chui
* To phng thc render($file,$part=array(){$this->load->view($file,$part)}
2. Goi layout trong controller
- X ly vi file
Hm file() oc ton b file v tra v 1 mang
B1: M file dung @ tranh bao li ra mn hnh nu file cha tn ti @fopen($path,"r")
B2:M file v to file khi file cha tn ti fopen($path,"w+")
Cac ch m file:
R: ch oc
R+: oc v ghi
W : ch ghi ,n i dung se bi xoa nu nh co n i dung, cha co to mi
W+ oc v ghi
A : m file ch ghi, cha co to mi v ghi ni d li u vo
A+: m file oc v ghi, cha co to mi v ghi ni d li u vo
B: m ch binary
B3:Ghi vo file fwrite($file,$string)
\r\n xung dong trong h thng window
\n xung dong h thng UNIX
\r xung dong h thng old MAC
Ho c xung dong s dng PHP_EOL
B4. oc file
oc file theo tng ki t fgetc($file)
oc file theo tng dong fgets($file)
Kim tra oc n ki t cui cung cua file cha feof($file)
Ly kich thc file cn oc filesize($path)
oc ht tt ca file fread($file, filesize($path))
B5. ong file fclose($file)
Kim tra file co tn ti khng file_exists($path).
Kim tra file co quyn ghi khng is_writable($path)
Ly n i dung m t file bt k k dung hm fread: files_get_contents($path) nu
$path l 1 link kt qua l file html cua trang web o
Ghi n i dung vo file k dung fwrite: file_put_contents($path,$content)
i tn file: rename($name1,$name2)
Copy file copy($path_resource,$path_des)
Xoa file unlink ($path)
Kim tra folder co tn ti khng is_dir($path)
To folder mi mkdir($path)
L nh khac trong php
1 In_array(giatrikiemtra,array(,)) Kim tra 1 phn t co thu c mang khng
2 Gi thu vi n session $this->load->library(session)
To thng bao flash session, hin thi duy nht cho thao tac tip theo v t ng c xoa
sau o
$this->session->set_flashdata(key,message)
Goi thng bao $this->session->flashdata(key)
3. to bin session
$this->session->set_userdata(array())
Goi bin session $this->session->userdata($bien)
Khi to curl_init()
Set cu hnh curl_setopt($ch-i tng curl, CURL_tenecauhinh,gia tri cua cu hnh)
Thc thi curl_exec($ch-i tng curl);
ong cu hnh curl_close($ch)
VD: ly anh t website khac v
function curl_download($Url,$saveTo)
{
$fp= fopen($saveTo,'w+');
$ch= curl_init($Url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
$result= curl_close($ch);
return $result;}
Tng hp cac l nh SQL trong codeigniter
$this->load->database(ten database muon kt ni trong file config,TRUE) s dng true
se tra v i tng database,true l m c inh
To 1 query v goi $this->db->query($query)
G p where v get: $this->db->get_where(table,array(),$from,$to)
Ly phn t max $this->db->select_max(field)->get(table)
$this->db->select_min(field)->get(table)
$this->db->select_avg(field)->get(table)
$this->db->where_in($bien,$mang)
1.L nh select tra v d li u l 1 kt qua duy nht u tin di dng i tng
$this->db->select()->where(,)->get(table)->row();
1.L n h select tra v d li u l 1 kt qua duy nht u tin di dng i mang
$this->db->select()->where(,)->get(table)->row_array();
2.L nh select tra v d li u l 1 mang i tng
$this->db->select()->where(,)->get(table)->result();
3.L nh select tra v d li u l mang chng mang
$this->db->select()->where(,)->get(table)->result_array();
4. m s lng kt qua tra v
$query->num_rows()
Ho c
$this->db->count_all_results(table)
5.m s c t tra v,ap dng khi kt qua tra v l i tng
$query->num_fields()
6.Giai phong b nh kt qua cua query
$query->free_result()
7.Ly kt qua t bang
$this->db->get(table)->result()
8.L nh update
$this->db->where()->update(table,$data)
Ho c
$this->db->set()->where()->update($table)
9 Lnh insert
$this->db->insert(table,$data)
Kim tra kt qua insert
$this->db->insert_id()
9 kim tra l nh query
$this->db->affected_row()
10. L n
h delete 1 hng
$this->db->where()->delete(table)
11. Sp xp theo th t v gii hn
$this->db->order_by(,ASC/DESC/RANDOM)
$this->db->limit(from,to);
12 Tm kim ging trong csdl
$this->db->like(field,keyword)
13 Lnh ni bang
$this->select()->from(table1)->where(,)->join(table2,table1.field1=table2.field2)
Trong o
select(table1.*,table2.field,table2.field) ly tt ca field trong table1 v cac field cn ly
trong table2
14. X ly OR khi khng h tr or_where
$this->db->where((field=$bien1 OR filed2=$bien1)
Require_one(file defined.php vo index)
Tm hiu v preg_match
Preg_match($pattern,$subject,$matches)
$pattern l pattern so sanh
$subject l chui mun so khp
$matches kt qua tra v khi so khp ng
Vd: if (preg_match('/^[0-9]+$/', '123', $maches)){
var_dump($maches);
kt qu array
Kt qua sai
$pattern : [a-z] [A-Z] [0-9]
$pattern l hoc 1 trong cac ki t moc vung[a2b]
$pattern xac inh chiu di cua chui [a-z]{min,max}
$pattern xac inh chiu di cua chui [a-z]{length}
du . i din cho ki t bt k, so sanh vi du . thm \.
du * i din khng co hoc nhiu ki t
du + i din co 1 hoc nhiu ki t
ki t ? i din co 1 hoc khng co ki t no
(.+) ki t bt k 1 hoc nhiu ln
(.+?) xut hin 1 ln ngn chn thuc tinh ham n Greedy (ly tng kt qua ch k duyt
ht chui mi ly.
\d - Ch s bt k ~ [0-9]
-hm preg_replace($pattern,$replace,$subject)
$pattern chui tm kim so sanh
$replace chui thay th
$subject chui gc cn thay th
Xoa the h1
$partern = '/(<h1>)|(<\/h1>)/';hoc <\/?h1>
$subject = '<h1>Welcome to freetuts.net</h1>';
$replacement = '';
echo preg_replace($partern, $replacement, $subject);
'1');
'2');
'3');
'4');
$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');
var_dump($redis->sDiff('s0', 's1', 's2'));
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "2"
}
$redis->delete('s');
$redis->sAdd('s', 'a');
$redis->sAdd('s', 'b');
$redis->sAdd('s', 'a');
$redis->sAdd('s', 'c');
var_dump($redis->sMembers('s'));
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
}
member1);
member2);
member3); /* key1 => {member1, member2,
member2, member3); /*return 2. key1 => {member1} */
$redis->zAdd('key1', 2, 'val2');
$redis->zAdd('key1', 10, 'val10');
$redis->zRange('key1', 0, -1); /* array('val0', 'val2', 'val10') */
// with scores
$redis->zRange('key1', 0, -1, true); /* array('val0' => 0, 'val2' => 2, 'val10' => 10) */
zRangeByScore, zRevRangeByScore
zRangeByScore($key,$start,$end,$option=array())
option 2 bin withscores=>TRUE, limit=>array(offset,count)
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRangeByScore('key', 0,
$redis->zRangeByScore('key', 0,
'val2' => 2) */
$redis->zRangeByScore('key', 0,
$redis->zRangeByScore('key', 0,
1)); /* array('val2' => 2) */
zRevRange
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRevRange('key', 0, -1); /* array('val10', 'val2', 'val0') */
// with scores
$redis->zRevRange('key', 0, -1, true); /* array('val10' => 10, 'val2' => 2, 'val0' => 0)
*/
Tm hiu v memcached
Cu hnh:
To file config.php trong config
To th vi n memcache_library trong libraries
Goi trong controller $this->memcache_library->
Xoa tt ca phn t trong memcache $this->memcache_library->flush()
Get phn t $this->memcache_library->get($key)
Thm phn t vo key $this->memcache_library->add($key,$value)