codeigniter学习2

16 篇文章 0 订阅
8 篇文章 0 订阅
1 缓存
  /system/cache目录清空,设置权限为666,
$this->output->cache(5);

5是你想要你的缓存持续的分钟数,即在页面被重新生成前会持续多长时间使用静态HTML文件。

2 文件辅助函数
  this->load->helper('file');

r”为读,“w”为写(写入文件,覆盖已存在的数据),“a”为追加(写入文件,在已存在的数据后添加)。每种情

况下,添加一个“+”,例如“a+”,打开文件以进行读写操作。“a”和“w”,而不是“r”或“r+”,如果没有的话

,总是创建文件
write_file('e:/filetest.txt', 'hello world', 'a+');

  delete_files('c:/mydirectory/');删除目录下的所有文件

3 文件上传



在system/application/config/upload.php中

  <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
$config['upload_path'] = 'uploads';
?>
$this->load->library('upload', $config);
允许用户上传的文件类型。像这样设置:
$config['allowed_types'] = 'gif|jpg|png';

  上传的例子:、

$config['upload_path'] = 'uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '8000';

$this->load->library('upload', $config);

if ( ! $this->upload->do_upload('file'))
{
echo $this->upload->display_errors();
}


4 crud套路:
  
增加,编辑:
function saveNews($do)
{
if ($do == "add")
{
$data = array(
'title'   => $this->input->post('title'),
'cid'     => $this->input->post('cid'),
'content' => $this->input->post('content'),
'time'    => (string) date('Y-m-j'),
);
$this->db->insert('news',$data);
if ($this->db->affected_rows())
{
return true;
}
else
{
return false;
}
}
else if ($do == "edit")
{
$data = array(
'title'   => $this->input->post('title'),
'cid'     => $this->input->post('cid'),
'content' => $this->input->post('content'),
);
$this->db->where('id',$this->input->post('id'));
$this->db->update('news', $data);
if ($this->db->affected_rows())
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
删除:
//删除单条新闻
if($id!=0)
{
//$this->db->where('id',(int)$id);
//$this->db->delete('news');
$this->db->delete('news', array('id' => $id));
return true;
}
//批量删除新闻
else
{
$this->db->where_in('id',$this->input->post('id'));
$this->db->delete('news');
return true;
}

5 session的用法
  设置:
$query = $this->db->get("admin");
if ($query->num_rows() > 0)
{

$session_data = array('admin'=>$this->input->post("username"));
$this->session->set_userdata($session_data);
                            }
判断session是否有:
if ($this->session->userdata("admin"))
{
redirect("admin/index");
}

  销毁session:
  $this->session->sess_destroy();

6 跨脚本XSS-CLEAN:
   $data = $this->input->xss_clean($data);
  要让其自动处理:
  你可以在application/config/config.php文件中设定:

$config['global_xss_filtering'] = TRUE;
而 $this->input->post('some_data', TRUE);第二个参数是设置XSS过滤

7 分页
   $this->load->library('pagination');

$config['base_url'] = site_url('admin/listnews');
$config['total_rows'] = $this->db->count_all('news');
$config['per_page'] = '1';
$config['num_links'] = '6';
$config['uri_segment'] = 3;


$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = 'First';

$config['last_link'] = 'Last';


$this->pagination->initialize($config);

$this->db->select('id,title,time');

$this->db->order_by('id','desc');
$query=$this->db->get('news',$config['per_page'],$this->uri->segment(3));

return $query;

  $this->uri->segment(3)表明偏宜量从URL的第3段地址中获得:offset,它表示从哪行开始返回记录。第一个参数

(limit)从 $config['per_page'] 变量中取值,而第二个参数则通过 URL 辅助函数,从 URL 的第三段中取值:$this

->uri->segment(3),比如www.your-site.com/index.php/class/function/ID,所以大家可以看到第三段(index.php

的后面)代表 ID。所以允许采用如下的 URL:

        http://localhost/~yannick/pagination/index.php/books/index/10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值