Tuesday, 10. November 2009, 10:15:07
Zend Framework, MySQL, PHP
修改 Zend/Db/Select.php, 增加以下代码
const FOUND_ROWS = 'foundrows';
const SQL_FOUND_ROWS = 'SQL_CALC_FOUND_ROWS';
protected static $_partsInit = array(
self::FOUND_ROWS => false,
... ...
);
/**
* Makes the query SELECT SQL_CALC_FOUND_ROWS.
*
* @param bool $flag Whether or not add SQL_CALC_FOUND_ROWS to SELECT.
* @return Zend_Db_Select This Zend_Db_Select object.
*/
public function foundRows($flag = true)
{
$this->_parts[self::FOUND_ROWS] = (bool) $flag;
return $this;
}
/**
* Render FOUND_ROWS clause
*
* @param string $sql SQL query
* @return string
*/
protected function _renderFoundRows($sql)
{
if ($this->_parts[self::FOUND_ROWS]) {
$sql .= ' ' . self::SQL_FOUND_ROWS;
}
return $sql;
}
以上代码的具体位置不再详述。
调用方法如下:
$select = $db->select()->foundRows()->from(
... ...
)->where(
... ...
);
$data = $db->fetchAll($select);
$stmt = $db->query("SELECT FOUND_ROWS() as cnt");
list($rec) = $stmt->fetchAll();
$count = $rec['cnt'];
Wednesday, 30. September 2009, 09:20:51
PHP, WINDOWS
1. 下载 WampServer
网址:
http://www.wampserver.com/en/download.php2. 安装 WampServer
Windows 的标准安装步骤,连续点“下一步”即可。
3. 下载 Xdebug
网址:
http://xdebug.org/download.php页面上有好几个版本,刚开始一定会被迷惑,不知道该用哪个版本。
经过试验,我的环境(Windows XP)最终能用的是 5.3 VC6 (32 bit)。
(Apache 以外的服务器需用 nts 版本)
下载之后把文件移动(或复制)到 WampServer 的 php 的 ext 目录。
4. 编辑 php.ini
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
追加以下内容:
extension=php_xdebug-2.0.5-5.3-vc6.dll
;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
追加以下内容:
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
5. 重新启动 WampServer 的 apache。
Tuesday, 29. September 2009, 01:33:50
Debug, Smarty, PHP
很简单的一句话,却常常忘记,因此记录一下:
{$var|@debug_print_var}
稍微复杂一点的用法:
{$var|@debug_print_var:2:100}
意思是缩进长度为2个空格,每行最多字数为100。
其他调试方法可以参考
这个(日文)
Tuesday, 15. September 2009, 05:23:54
Linux, Hosting, PHP
便宜的虚拟主机不提供 Shell,因此需要用命令实现的功能不大容易实现。
做网站经常要用 ln 命令创建链接,而要在一般的虚拟主机上就不得不想其他办法了。
不用命令的情况下,用程序倒也能解决一些问题,比如这个建立链接的目标,可用以下 PHP 程序实现:
<?php
$appLib = realpath(dirname(__FILE__) . '/../library');
$zfLib = realpath(dirname(__FILE__) . '/../../ZF');
symlink("$appLib/Zend", "$zfLib/Zend");
经测试,链接正常建立,然而不能正常工作,只能说虚拟主机的自我保护功能做得还不错。
Thursday, 10. September 2009, 02:05:20
function, PHP
新作了两个函数,结果没用上,放在在这儿以备用。
/**
* 按词分割字符串
* @param $str 元字符串
* @param $maxLength 单词最大长度
* @return array
*/
function splitInWords($str, $maxLength = 16) {
if(empty($str)) return array();
if($maxLength <= 0) $maxLength = 1;
$rawWords = explode(' ', $str);
$words = array();
foreach($rawWords as $word) {
$len = mb_strlen($word);
if($len > $maxLength) {
$pos = 0;
while($len > $maxLength) {
$words[] = mb_substr($word, $pos, $maxLength);
$pos += $maxLength;
$len -= $maxLength;
}
$words[] = mb_substr($word, $pos, $maxLength);
} else {
$words[] = $word;
}
}
return $words;
}
/**
* 按行(指定长度)分割字符串
* @param $str 元字符串
* @param $length 每行最大字符数
* @return array
*/
function splitInLines($str, $length = 80) {
$lines = array();
foreach(split("\n", $str) as $s) {
preg_match_all("/./u", $s, $matches);
$arr = $matches[0];
for($i = 0; $i < ceil(count($arr)/$length); $i++) {
$lines[] = join("", array_slice($arr, $i*$length, $length));
}
}
return $lines;
}
Wednesday, 2. September 2009, 00:39:40
Framework, Zend, PHP
Zend Framework 的组件,不少要用到 PHP 的扩展模块。
详情见:
http://www.mikaelkael.fr/doczf/zh/requirements.extensions.html我整理了一下其中比较常用的模块,列举如下:
apc
ctype
curl
dom
gd
hash
iconv
json
ldap
libxml
mbstring
memcache
mime_magic
pdo_mysql
pdo_pgsql
pdo_sqlite
posix
Reflection
session
SimpleXML
soap
SPL
Sqlite
xml
zlib
搭建环境,编译 PHP 的时候,最好把以上模块包含进去,以免用到的时候再次编译 PHP。
Tuesday, 4. August 2009, 06:20:43
Zend, PHP
一. render不指定render结果: {当前Module}/{当前Controller}/{当前Action}.phtml
$this->render('bar') ;结果: {当前Module}/{当前Controller}/bar.phtml
二. forward$this->_forward('bar') ;结果: {当前Module}/{当前Controller}/bar
$this->_forward('bar', 'foo') ;结果: {当前Module}/foo/bar
$this->_forward('bar', 'foo', 'hoge') ;结果: hoge/foo/bar
$params = array(
'a' => '1',
'b' => '2'
) ;
$this->_forward('bar', 'foo', 'hoge', $params) ;结果: /hoge/foo/bar/a/1/b/2
三. redirect$this->_redirect('/hoge') ;结果: /hoge
$this->_redirect('/hoge/foo') ;结果: /hoge/foo
$this->_redirect('/hoge/foo/bar') ;结果: /hoge/foo/bar
$this->_redirect('http://localhost/hoge/foo/bar') ;结果:
http://localhost/hoge/foo/bar$this->_redirect('http://localhost/hoge/foo/bar?a=1&b=2') ;结果:
http://localhost/hoge/foo/bar?a=1&b=2四. 特殊情况不使用 layout结果: $this->_helper->layout()->disableLayout() ;
不使用 view结果: $this->_helper->viewRenderer->setNoRender() ;
Wednesday, 8. April 2009, 08:15:23
Swf,Apache, Flash, PHP
1. SWF Editor for PHP (swfed)日本人写的扩展模块, 能实现 swf 文件中的图像替换等功能。
下载:
http://sourceforge.jp/projects/swfed/downloads/38169/swfed-0.18.tar.gz安装:
tar -zxvf swfed-0.18.tar.gz
cd swfed-0.18
phpize
./configure
make
cp modules/swfed.so /usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/
(注意:以上路径因环境而异)
编辑 php.ini,加入
extension=swfed.so
重新启动 Apache。
2. ffmpeg-php用于读取视频和音频文件中的信息,能够从视频文件中获取某一帧画面的图片。
首先得安装
ffmpeg。
下载:
http://ffmpeg.mplayerhq.hu/releases/ffmpeg-0.5.tar.bz2安装:
tar -jxvf ffmpeg-0.5.tar.bz2
cd ffmpeg-0.5
./configure --enable-shared
make
make install
接下来安装 ffmpeg-php。
下载:
http://sourceforge.net/project/downloading.php?group_id=122353&use_mirror=jaist&filename=ffmpeg-php-0.6.0.tbz2安装:
tar -jxvf ffmpeg-php-0.6.0.tbz2
cd ffmpeg-php-0.6.0
phpize
./configure
make
make install
编辑 php.ini,加入
extension=ffmpeg.so
※补充:
php执行错误:
Call to undefined method ffmpeg_frame::toGDImage() 的解决方法:
./configure 执行之后,手动编辑 config.h,加入
#define HAVE_LIBGD20 1之后再 make clean && make
重新启动 Apache。
Thursday, 12. March 2009, 05:52:35
mediawiki, Wiki, PHP
闲来没事,更新了一下
以前装的 mediawiki,上传新的代码之后,问题产生:数据库等不能通过浏览器更新。
看了一下
mediawiki 的文档,才知道,需要用命令运行 maintenance 目录下的 update.php。
我用的虚拟主机很初级,没有提供 SSH 功能,于是用到了这个叫
PHPShell 的东西。
在服务器上装上 PHPShell 并进行必要的设置之后,在浏览器上就能运行服务器的命令,当然,这只是个简单的模拟,不能和直接用终端相比,不过,能通过浏览器运行命令,目的已经达到了。
Thursday, 5. February 2009, 11:31:58
Regex, PHP
这东西太常用,有时候又记不住,每次上Google查询,太费事,所以贴一个在这儿。
.(句点) 匹配所有单个字符
^(脱字符号) 匹配出现在行或字符串开头的空字符串
$(美元符号) 匹配出现在行尾的空字符串
A 匹配大写字母 A
a 匹配小写字母 a
\d 匹配所有一位数字
\D 匹配所有单个非数字字符
\w 匹配所有单个字母或数字字符;同义词是 [:alnum:]
[A-E] 匹配所有大写的 A、B、C、D 或 E
[^A-E] 匹配除大写 A、B、C、D 或 E 之外的任何字符
X? 匹配出现零次或一次的大写字母 X
X* 匹配零个或多个大写字母 X
X+ 匹配一个或多个大写字母 X
X{n} 精确匹配 n 个大写字母 X
X{n,m} 至少匹配 n 个且不多于 m 个大写字母 X;如果忽略 m,则表达式将尝试匹配至少 n 个 X
(abc|def)+ 匹配一连串的(最少一个)abc 和 def;abc 和 def 将匹配
Showing posts 1 -
10 of 40.