自定义SQL语句
当提供的API满足不了我们对SQL语句的要求的时候,我们通常是自己来写SQL语句,CI也提供了比较强大了,能够满足我们需求的一般的sql的API。
<span>$res</span>=<span>$this</span>->db->select('id,name'<span>) </span>->from('表名'<span>) </span>->whrer('id >=',5)<span>//</span><span>注意id后面要有个空格</span> ->limit(3,2<span>)<span>//这里与sql的limit是顺序是反的</span> </span>->order_by('id desc '<span>) </span>->get();<span>//</span><span>翻译成sql语句</span> <span>var_dump</span>(<span>$res</span>-><span>result()); </span><span>echo</span> <span>$this</span>->db->last_query();<span>//</span><span>先是最近一条SQL</span>
自定义扩展控制器
在application/core中新建MY_Controller.php
<span>class</span> MY_Controller <span>extends</span><span> CI_Controller { </span><span>public</span> <span>function</span><span> __construct() { parent</span>::<span>__construct(); </span><span>//</span><span>一定呀先调用父类的构造方法 //登录验证、权限验证、其他操作。。。</span> <span> } }</span>
同时需要在application/config/config.php中配置一下:
<span>$config</span>['subclass_prefix'] = 'MY_';
自定义扩展模型
在application/models中创建user_model.php
<span>class</span> User_model <span>extends</span><span> CI_Model { </span><span>public</span> <span>function</span><span> getAll() { </span><span>$res</span> = <span>$this</span>->db->get('表名'<span>); </span><span>return</span> <span>$res</span>-><span>result(); } }</span>
在控制器中调用自定义模型
application/controllers:
<span>class</span> User <span>extends</span><span> MY_Controller { </span><span>public</span> <span>function</span><span> index() { </span><span>$this</span>->load->model('User_model');<span>//</span><span>调用以类名为主,而不是文件名</span>
<span>$list</span> = <span>$this</span>->User_model->getAll();<span>//</span><span>调用模型获取数据</span>
<span>$this</span>->load->view('user/index',<span>array</span>('list'=><span>$list</span>));<span>//</span><span>加载视图</span><span> } }</span>
加载模型的时候可以给模型取一个名字:
<span>$this</span>->load->model('User_model','user');<span>//</span><span>调用以类名为主,而不是文件名 </span> <span>$list</span> = <span>$this</span>->user->getAll();<span>//</span><span>调用模型获取数据</span>
Url相关函数
在表单验证的时候,需要把数据传给控制器,怎么准确的而且可扩展的写action呢,调用API:
<span>public</span> <span>function</span><span> addView() { </span><span>$this</span>->load->helper('url');<span>//</span><span>为了不把表单传递的地址写死,用url函数</span> <span>$this</span>->load->view('user/add'<span>); }</span>
在user/add.php视图当中:
<span><</span><span>form </span><span>action</span><span>="<?php echo site_url('user/insert'); ?>"</span><span> method</span><span>="post"</span><span>></span> <span><!--</span><span> ........</span><span>--></span> <span></</span><span>form</span><span>></span>
如果是index.php目录的话,用:
base_url();
这个API。
同时,每次加载url很麻烦,也可是设置成自动加载,在config/config.php中修改:
<span>$config</span>['helper'] = <span>array</span>('url');
可能在后面的版本中就没有这个自动加载了。
路由
$route['rouxx/showxx/([d] ).html'] = 'rou/show/$1';//插入这句话
分页
总共有多少条记录
一页要有多少条记录
总共多少页
当前页前后要显示多少个分页链接
<span>//</span><span>总条数</span> <span>$config</span>['total_rows'<span>] </span><span>//</span><span>一页显示几条</span> <span>$config</span>['per_page'<span>] </span><span>//</span><span>定义当前页的前后各有几个数字链接</span> <span>$config</span>['num_links'<span>] </span><span>//</span><span>定义没有分页参数,主URL</span> <span>$config</span>['base_url']
<span>$this</span>->load->library('pagination');
<span>$this</span>->pagination->initialize(<span>$config</span>);
<span>echo</span> <span>$this</span>->pagination->create_links();
<span>echo</span> <span>$this</span>->db->limit(<span>$num</span>,<span>$start</span>); <span>//</span><span>从$start查$num条</span>
<?<span>php </span><span>if</span> ( ! <span>defined</span>('BASEPATH')) <span>exit</span>('No direct script access allowed'<span>); </span><span>class</span> Page <span>extends</span><span> CI_Controller { </span><span>public</span> <span>function</span><span> user_add(){ </span><span>$this</span>->load->model('test_m'<span>); </span><span>for</span> (<span>$i</span> = 1;<span>$i</span> <= 100;<span>$i</span>++<span>){ </span><span>$name</span> = 'u'.<span>$i</span><span>; </span><span>$arr</span> = <span>array</span>("usid"=><span>$i</span>,"uname"=><span>$name</span>,"upass"=>123456<span>); </span><span>$this</span>->test_m->user_insert(<span>$arr</span><span>); } } </span><span>public</span> <span>function</span><span> pagelist(){ </span><span>$this</span>->load->model('test_m'<span>); </span><span>$user</span> = <span>$this</span>->test_m-><span>user_select_all(); </span><span>$allnum</span> = <span>count</span>(<span>$user</span><span>); </span><span>$pagenum</span> = 20<span>; </span><span>$config</span>['total_rows'] = <span>$allnum</span><span>; </span><span>$config</span>['per_page'] = <span>$pagenum</span><span>; </span><span>$config</span>['num_links'] = 3<span>; </span><span>$config</span>['base_url'] = "/CI/index.php/page/pagelist"<span>; </span><span>$config</span>['use_page_numbers'] = <span>true</span><span>; </span><span>$this</span>->load->library('pagination'<span>); </span><span>$this</span>->pagination->initialize(<span>$config</span><span>); </span><span>var_dump</span>(<span>$this</span>->pagination-><span>create_links()); </span><span>echo</span> <span>$this</span>->pagination-><span>create_links(); </span><span>echo</span> "<br />"<span>; </span><span>$id</span> = <span>$this</span>->uri->segment(3); <span>//</span><span>获得url第三段字符</span> <span>$id</span> =<span>$id</span> ? <span>$id</span>:1<span>; </span><span>$start</span> = (<span>$id</span> - 1) * <span>$pagenum</span><span>; </span><span>$list</span> = <span>$this</span>->test_m->user_select_limit(<span>$start</span>,<span>$pagenum</span><span>); </span><span>var_dump</span>(<span>$list</span><span>); } }</span>
上传文件
视图 /views/up.php:
<span><</span><span>html</span><span>></span> <span><</span><span>form </span><span>action</span><span>="ci/CodeIgniter_2.2.0/index.php/upload/up"</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span> <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="upfile"</span> <span>/></span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="sub"</span><span> value</span><span>="提交"</span> <span>/></span> <span></</span><span>form</span><span>></span> <span></</span><span>html</span><span>></span>
控制器:
<span>$config</span>['upload_path'] = './uploads/'<span>; </span><span>//</span><span>设置允许上传的类型</span> <span>$config</span>['allowed_types'] = 'gif|jpg|png'<span>; </span><span>$config</span>['max_size'] = '100'<span>; </span><span>//</span><span>如果是图片还可以设置最大高度和宽度</span> <span>$config</span>['max_height'] = 768<span>; </span><span>$config</span>['max_width'] = 1024;
还可以设置其他的一些额外参数,详细看用户手册。
<span>//</span><span>upload为调用的类名,全小写</span> <span>$this</span>->load->library('upload',<span>$config</span><span>); </span><span>//</span><span>如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去</span> <span>$this</span>->upload->do_upload('上传框的name');
<span>//</span><span>出错信息</span> <span>$error</span> = <span>array</span>('error' => <span>$this</span>->upload-><span>display_error()); </span><span>//</span><span>成功信息</span> <span>$data</span> = <span>array</span>('upload_data' => <span>$this</span>->upload->data());
<?<span>php </span><span>if</span> ( ! <span>defined</span>('BASEPATH')) <span>exit</span>('No direct script access allowed'<span>); </span><span>class</span> Upload <span>extends</span><span> CI_Controller { </span><span>//</span><span>显示带表单的视图</span> <span>public</span> <span>function</span><span> index(){ </span><span>$this</span>->load->view('up'<span>); } </span><span>//</span><span>显示上传信息</span> <span>public</span> <span>function</span><span> up(){ </span><span>$config</span>['upload_path'] = './uploads/'<span>; </span><span>$config</span>['allowed_types'] = 'gif|jpg|png'<span>; </span><span>$config</span>['max_size'] = "2000"<span>; </span><span>$this</span>->load->library('upload',<span>$config</span><span>); </span><span>//</span><span>打印成功或错误的信息</span> <span>if</span>(<span>$this</span>->upload->do_upload('upfile'<span>)) { </span><span>$data</span> = <span>array</span>("upload_data" => <span>$this</span>->upload-><span>data()); </span><span>var_dump</span>(<span>$data</span><span>); } </span><span>else</span><span> { </span><span>$error</span> = <span>array</span>("error" => <span>$this</span>->upload-><span>display_errors()); </span><span>var_dump</span>(<span>$error</span><span>); } } }</span>
Session
利用CI类实现session登录
<span>//</span><span>生成一个随机不重复的字符串走位加密的key保存到config.php的encryption_key中</span> <span>$config</span>['encryption_key']='adb8bf6d0ac4e17b42a80941582497a4';
<span>$this</span>->load->library('session');
<span>$array</span> = <span>array</span>('id'=>3,'name'=>'jack'<span>); </span><span>$this</span>->session->set_userdata(<span>$array</span>);
<span>$this</span>->session->userdata(session名);
<span>$this</span>->session->unset_userdata('SESSION名');
$config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = TRUE $config['sess_use_database'] = FALSE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300;
<span>//</span><span>设置</span> <span>$this</span>->session->set_flashdata('test','aaaaa'<span>); </span><span>//</span><span>读取</span> <span>$test</span> = <span>$this</span>->session->flashdata('test');
控制层
function test_func(){
//获取model页面需要的两个参数
$competition_id = $_GET["competition_id"];
$report_class = $_GET["report_class"];
$this->load->model("Action"); //引入model
$data["head"] = $this->Action->get_report_item($competition_id, $report_class); //引用model的函数
$this->load->view("test_result",$data); //将结果显示在test_result.php页面中
}
view层:
字段名称(含义)</td> //该td中显示的是你从数据库、即model层中获取到的数据的含义,想显示多少,显示哪个,在这里确认 </tr> |
test; ?></td> </tr> </table> echo “123”; }?> </div>
在view文件里可以直接用$title了
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
PHP数组从URL参数中获取的行为不如预期
我有一个包含类别ID的URL参数,我想将其视为一个数组,如下所示:http://example.com?cat[]=3,9,13在PHP中,我使用它从URL参数获取数组:$catI...
来自于 2024-04-06 22:09:02
0
1
1428
返回值中变量的格式是什么?
我是php的新学习者。我发现有一段代码:if($x<time()){return[false,'error'];}逻辑或变量并不重要,但我不明白[false,'error']...
来自于 2024-04-06 21:55:20
0
1
778
使用MySQL SQL查询计算另一张表中字段的总和
我有一个这样的模式:具有属性“user_id”和“username”的用户表以及具有属性“customer_id”(user_id的FK)和“finalPrice”的订单表数据库架...
来自于 2024-04-06 19:39:29
0
1
441
MySQL中如何进行分组和计数?
我正在尝试编写一个查询,提取发送给朋友的未删除消息的总数。这是表结构的屏幕截图:我想要实现的输出结果如下:idmessages_count---------------------...
来自于 2024-04-06 18:30:17
0
1
353
用 HTML 代码(从数组生成)替换字符串的顶级方法
给定一个像这样的字符串...$htmlPattern="Usernameis:#name#andusercompanyis#company#";如何将子字符串#...
来自于 2024-04-06 17:40:41
0
2
410
热门推荐
热门教程
更多>
|