ThinkPHP中forward和redirect的区别

ThinkPHP中forward和redirect的区别在那里呢? 首先看代码注释: forward: 执行某个Action操作(隐含跳转) 支持指定模块和延时执行 redirect: Action跳转(URL重定向) 支持指定模块和延时跳转 其次看代码组织,二者的行为是不同的: forward: [code] if(is_array($action)) { //通过类似 array(&$module,$action)的方式调用 call_user_func($action); }else { if(empty($module)) { $module = defined('C_MODULE_NAME')?C_MODULE_NAME:MODULE_NAME; } if( MODULE_NAME!= $module) { ...

How to disable/enable an element with jQuery

Sometimes you need to disable or enable some elements in your document and jQuery makes this task easy. All you have to do is to set disabled attribute to "disabled". Example: [code] // To disable $('.someElement').attr('disabled', 'disabled'); [/code] In order to enable any disabled element you need to set the disabled attribute to empty string or remove it entirely like in the code below. [code] // To enable $('.someElement').removeAttr('disabled'); // OR you can set attr to &...

Javascript Equivalent of PHP’s trim function

Trim function in PHP can remove whitespace from the beginning and end of a string. Javascript do not have this function, but is easy to made one. [code] // remove space in a string function trim(str) { if(!str || typeof str != 'string') return null; return str.replace('/^\s+|\s+$/g', ''); } [/code]

Javascript Equivalent of PHP’s explode() and implode()

Do you know javascript has equivalent functions as php's explode() and implode(). Let's say we have a string: [code] $string = 'aa:bb:cc'; [/code] We want to break down each number into an array called $num_array. In PHP we could do this: [code] $explode_array = explode(':', $string); [/code] The PHP implode function would produce the following: [code] $explode_array[0] = 13 $explode_array[1] = 42 $explode_array[2] = 43 $explode_array[3] = 56 [/code] In Javascrip...

ThinkPHP上传类库的Bug修复

最近开发网站用到了ThinkPHP 2.0的上传类库, 用来实现一个图片上传功能。 使用过程中发现,如果开启了子目录创建方式,则会出现一些问题: [code]public $autoSub = true;[/code] 1. 无法生成缩略图, 此问题的修复办法如下, 在 UploadFile.class.php 中找到 line#145 [code] if ($this->thumb) { // 生成图像缩略图 import("@.ORG.Image"); $image = Image::getImageInfo($filename); if(false !== $image) { //是图像文件生成缩略图 $thumbWidth = explode(',',$this->thumbMaxWidth); $thumb...

Mass Replace String use MySQL REPLACE function

Since i changed the path for a friend's blog, i need update the files and images path URL in all previous posts. The best way i found is use MySQL REPLACE function to replace string in hundreds of records. Only need 1 minute. Here is the SQL query to replace string in database table: [code] UPDATE table_name SET column_name = REPLACE(column_name,”original_string”,”replace_string”) [/code] SQL query for mass replace string in wordpress table [code] UPDATE `wp_posts` SET `post_cont...

使用正则表达式进行ThinkPHP的自动验证

ThinkPHP的自动验证机制是为了进行表单数据验证,验证可以支持function、 callback、confirm、equal、unique和regex,这里要讲的是使用正则表达式进行验证。 一般我们见的比较多的是设置规则为require、email之类的,其实这些本身也是属于正则表达式验证方式,只是系统内置定义了一些常用的正则表达式而已。这些内置的正则表达式的定义可以参考model类的regex方法,内置支持的正则定义包括: require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字、zip 邮编、integer 整数、double 浮点数、english 英文字母,但是并不局限于这些正则规则的,我们完全可以直接在验证规则里面使用正则表达式进行定义,这样我们可以凭借强大的正则表达式来进行表单字段验证,例如: array(‘name’,'/^[a-z]\w{3,}$/i’,'名字不符合要求!’); array(‘password’,'/^[a-z]\w{6,30}$/i’,'密码不符合要求!’); array(‘ac...

ThinkPHP的动态数据查询方法

ThinkPHP提供了数据的动态查询方法,可以简化你的查询代码,例如: $User->where(‘name=”ThinkPHP”‘)->find(); 可以简化为: $User->getByName(‘ThinkPHP’); $User->where(‘email=”thinkphp@qq.com”‘)->find(); 可以简化为: $User->getByEmail(‘thinkphp@qq.com’); getBy**** 方法里面的**** 会转换成小写的字段名,如果字段不存在,就会出错。 如果你的字段名是user_id ,那么查询方法应该写成: $User->getByUserId(5); UserId 会被解析成为数据库的user_id字段,这点需要注意,以免引起不必要的麻烦。 目前尚不支持,对多个字段的动态查询。

避免ThinkPHP URL目录过深的技巧

按照ThinkPHP的默认URL模式,通常是: http://serverName/模块名/操作名/变量1/值1/变量2/值2… 很多人担心这样的URL会导致目录层次过深,而且由于这样的URL改变了当前的相对路径,所以如果不注意写法,经常会导致JS和CSS加载不到。问题就在于这个”/”,这两个问题都可以通过一个小技巧解决,而且不影响你的开发,只需要在项目配置文件中设置 ‘URL_PATHINFO_DEPR’=>’-', 这个配置默认值是”/” 我们更改为”-” 配置修改以后,上面的URL地址就可以变成: http://serverName/模块名-操作名-变量1-值1-变量2-值2… 不过要注意的是,模板里面的链接地址最好是用U方法动态生成的,而不是固定写死的,否则模板会有一定的修改工作。

ThinkPHP判断当前操作的请求类型

在很多情况下面,我们需要判断当前操作的请求类型是GET POST 甚至是PUT DELETE,一方面可以针对作出不同的逻辑处理,另外一方面有些情况下面需要验证安全性,过滤不安全的请求。 TP的Action类内置了一些判断方法用于判断请求类型,包括: isGet 是否是GET提交 isPost 是否是POST提交 isPut 是否是PUT提交 isDelete 是否是DELETE提交 isHead 是否是HEAD提交 使用举例如下: [code] class UserAction extends Action{ public function update(){ if ($this->isPost()){ $User = M(‘User’); $User->create(); $User->save(); $this->success(‘保存完成’); } else { $this->error(‘非法请求’); } } ...