The examples in this article describe two commonly used functions of ThinkPHP: verification code and paging. It is very common in ThinkPHP project development and has high practical value. The complete example is shared with everyone for your reference. The details are as follows:
1. Verification code:
Import the verification code class, there is a verification code method in aoliThinkPHPLibORGUtilImage.class.php
1. English verification code:
buildImageVerify($length,$mode,$type,$width,$height,$verifyName)
The parameters are as follows:
length: The length of the verification code, the default is 4 digits
mode: The type of verification string, the default is number, other supported types are 0 letters 1 numbers 2 uppercase letters 3 lowercase letters 4
Chinese 5 mixed (removed the confusing character oOLl and the number 01)
type: The image type of the verification code, the default is png
width: The width of the verification code. By default, it will be automatically calculated based on the length of the verification code
height: the height of the verification code, the default is 22
verifyName: SESSION record name of the verification code, the default is verify
2. Chinese verification code:
GBVerify ($length,$type,$width,$height,$fontface,$verifyName)
The parameters are as follows:
length: The length of the verification code, the default is 4 digits
type: The image type of the verification code, the default is png
width: The width of the verification code. By default, it will be automatically calculated based on the length of the verification code
height: the height of the verification code, the default is 50
fontface: The font file used, use the complete file name or put it in the directory where the image class is located. The default font file used is simhei.ttf (this file can be found under the Fonts directory of the window)
verifyName: SESSION record name of the verification code, the default is verify
3. If the verification code cannot be displayed, please check:
①.PHP Whether GD library support has been installed;
②. Is there any output before output (especially the BOM header information output of UTF8);
③.Is the Image class library imported correctly?
④. If it is a Chinese verification code, check whether the font file has been copied to the directory where the class library is located;
4.action part:
CommonAction.class.php page code is as follows:
<?php class CommonAction extends Action{ function verify(){ import('ORG.Util.Image'); //英文验证码 //Image::buildImageVerify(5,5,gif,90,30,'verify'); //中文验证码 Image::GBVerify(); } } ?>
5.view template part:
The template index.html page is as follows:
验证码:<input type="text" name="verify" /><img src="__APP__/common/verify" onclick="show(this)" /><br /> <input type="submit" value="注册" /> </form> <script type="text/javascript"> function show(obj){ obj.src="__APP__/common/verify/random/"+Math.random(); } </script>
6. Controller:
The controller UserAction.class.php is as follows:
//验证码验证 if($_SESSION['verify']!=md5($_POST['verify'])){ $this->error('验证码不正确'); }
2. Pagination:
1. Import the paging class, there is a verification code method in aoliThinkPHPLibORGUtilPage.class.php
2.action part:
UserAction.class.php page is as follows:
function index(){ import('ORG.Util.Page');//引入分布类 $user=M('user'); $count=$user->count(); $page=new Page($count,3);//一页显示5条 $page->setConfig('theme','<div style="font-weight:bold;">总共:%totalRow%%header% %nowPage%/%totalPage%页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage% %end%</div>'); $show=$page->show(); $list=$user->field(array('id','username','createip'))->order('id desc')->limit($page->firstRow.','.$page->listRows)->select(); $this->assign('alist',$list); $this->assign('page',$show); $this->display(); }
3.view template part:
The template page index.html page is as follows:
<volist name="alist" id="vo"> <li><span>ID:</span>{$vo['id']}<span>用户名:</span>{$vo['username']}<span>注册ip:</span>{$vo['createip']}<a href="__URL__/del/id/{$vo['id']}">删除</a> <a href="__URL__/edit/id/{$vo['id']}">编辑</a></li> </volist> {$page}
Interested readers can debug and run the ThinkPHP verification code and paging examples in this article. I believe there will be new gains.
Use the setConfig method in the paging class to customize the paging style:
I have a custom paging class that I used before. You can change it here.
/** * * Enter public paging class* @param array $map paging filtering conditions* @param class $Form data model* @param integer $limit number of items displayed in paging* @param string $order Sorting* @return array */ public function _list($map,$Form,$limit=9,$order='add_time'){ $res=array(); $p=empty($_GET['p']) ? 0 : (int)$_GET['p']; $res['list'] = $Form->field(true)->where($map)->order($order)-> page($p.','.$limit)->select();import('ORG.Util.Page'); // Import paging class $count = $Form->where($map)-> ;count();// Query the total number of records that meet the requirements $Page = new Page($count,$limit);// Instantiate the paging class and pass in the total number of records and the number of records displayed on each page $Page-> rollPage=3;$Page->setConfig('theme'," %upPage% %linkPage% %downPage%
public function index() {//Just change the style of the icon $user = M('User'); import('ORG.Util.Page'); $count = $user->count( ); $listRows = 5; $page = new Page($count, $listRows); $list = $user->limit("{$page->firstRow},{$page->listRows}") ->select(); $page->setConfig('prev', '');//Previous page $page->setConfig('next', '');//Next page// $page-> setConfig('first', ''); // $page->setConfig('last', ''); $page->setConfig('theme', '%upPage% %downPage%');// Only display the upper and lower page options $this->assign('page', $page->show()); $this->assign('list', $list); $this->display(); }