这个问题,足足花费了我以下的时间解决:
我的controllers中的guestbook.php文件如下:
<?php
require_once 'Zend/Controller/Action.php';
class User_GuestbookController extends Zend_Controller_Action
{
/**
* The default action - show the home page
*/
public function init()
{
$this->view->baseUrl="http://".$_SERVER["SERVER_NAME"].$this->_request->getBaseUrl();
Zend_Loader::loadClass('guestbook');
}
public function showAction()
{
$this->view->currentPage=$_SERVER['PHP_SELF'];
$this->view->maxRows_rsRead=5;
$this->view->pageNum_rsRead=0;
if(isset($_GET['pageNum_rsRead']))
{
$this->view->pageNum_rsRead=$_GET['pageNum_rsRead'];
}
$this->view->startRow_rsRead=$this->view->pageNum_rsRead*$this->view->maxRows_rsRead;
$guestbook=new guestbook();
$db=$guestbook->getadapter();
$order='marker desc';
$count=$this->view->maxRows_rsRead;
$offset=$this->view->startRow_rsRead;
$this->view->rowset=$guestbook->fetchAll(null,$order,$count,$offset)->toArray();
$row_rsRead=$this->view->rowset[0];
if(isset($_GET['totalRows_rsRead']))
{
$this->view->totalRows_rsRead=$_GET['totalRows_rsRead'];
}else{
$this->view->totalRows_rsRead=count($guestbook->fetchAll());
}
$this->view->totalPages_rsRead=ceil($this->view->totalRows_rsRead/$this->view->maxRows_rsRead)-1;
$this->view->queryString_rsRead = "";
if(!empty($_SERVER['QUERY_STRING']))
{
$params=explode("&",$_SERVER['QUERY_STRING']);
$newParams=array();
foreach ($params as $param)
{
if(stristr($param,"pageNum_rsRead")==false&&stristr($param,"totalRows_rsRead")==false)
{
array_push($newParams,$param);
}
}
if(count($newParams)!=0)
$this->view->queryString_rsRead="&".htmlentities(implode("&",$newParams));
}
$this->view->queryString_rsRead=sprintf("&totalRows_rsRead=%d",$this->view->totalRows_rsRead);
function MakeHyperlink($text){
$text=preg_replace("/((http(s?):\/\/)|(www\.))([\S\.]+)\b/i","<a href=\"http$3://$4$5\" target=\"_blank\">$2$4$5</a>",$text);
$text=preg_replace("/([\w\.]+)(@)([\S\.]+)\b/i","<a href=\"mailto:$0\">$0</a>",$text);
return nl2br($text);
}
}
}
?>
我的views中的scripts中的guestbook中的show.phtml文件如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#main {
width: 500px;
margin-top: 50px;
margin-right: auto;
margin-left: auto;
}
.forTableBgRight {
background-color: #FEF;
}
.forTableBgLeft {
background-color: #FCF;
}
.Header_Footer_bg {
background-color: #FFC;
}
</style>
</head>
<body>
<div id="main">
<table width="500" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="1" class="Header_Footer_bg">
<tr class="forText">
<td width="50%">显示:<?php echo ($this->startRow_rsRead+1)?>-<?php echo min($this->startRow_rsRead+$this->maxRows_rsRead,$this->totalRows_rsRead)?></td>
<td width="50%" align="right">留言总数:<?php echo $this->totalRows_rsRead ?>
</td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"><table width="497" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td background="images/dot.gif"><img src="images/dot.gif" width="3" height="5"></td>
</tr>
</table></td>
</tr>
</table>
<table width="500" border="0" align="center" cellpadding="1" cellspacing="1">
<?php foreach($this->rowset as $row_rsRead):?>
<tr valign="top"><td width="120" class="forTableBgLeft">留言时间:</td>
<td width="380" class="forTableBgRight"><?php echo $row_rsRead['date'];?></td>
</tr><tr valign="top"><td width="120" class="forTableBgLeft">留言者:</td>
<td width="380" class="forTableBgRight">
<?php echo $row_rsRead['name'];?></td>
</tr>
<tr valign="top"><td width="120" class="forTableBgLeft">电子邮件:</td>
<td width="380" class="forTableBgRight">
<?php echo MakeHyperlink($row_rsRead['email']); ?></td>
</tr>
<tr valign="top"><td width="120" class="forTableBgLeft">留言内容:</td>
<td width="380" class="forTableBgRight">
<?php echo $row_rsRead['comment']; ?></td>
</tr>
<?php if($row_rsRead['remessage']!=''):?>
<tr valign="top"><td width="120" class="forTableBgLeft">回复留言:</td>
<td width="380" class="forTableBgRight">
<?php echo $row_rsRead['remessage']; ?></td>
</tr>
<tr valign="top">
<td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td background="images/dot.gif"><img src="images/dot.gif" width="3" height="5"></td>
</tr></table></td></tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
<table width="498" border="0" align="center" cellpadding="0" cellspacing="0">
<tr valign="middle" class="Header_Footer_bg"><td width="30%" height="20">
<?php if($this->pageNum_rsRead>0):?>
<?php { ?>
<a href="<?php echo printf("%s?pageNum_rsRead=%d%s",$this->currentPage,max(0,$this->pageNum_rsRead-1),$this->queryString_rsRead); ?>">上一页</a>
<?php } ?>
<?php endif; ?>
</td>
<td width="30%" align="center"><a href="sign.php">我要留言</a></td>
<td width="30%" align="right">
<?php if($this->pageNum_rsRead<$this->totalPages_rsRead):?>
<?php { ?>
<a href="<?php echo printf("%s?pageNum_rsRead=%d%s",$this->currentPage,min($this->totalPages_rsRead,$this->pageNum_rsRead+1),$this->queryString_rsRead); ?>">下一页</a>
<?php } ?>
<?php endif;?>
</td></tr></table>
</div>
</body>
</html>
我在浏览器里输入:http://localhost/mvcnew/public/index.php/user/guestbook/show,首页正常,效果如下:
奇怪的事情是:我点击 “下一页” 浏览器本该显示:http://localhost/mvcnew/public/index.php/user/guestbook/show?pageNum_rsRead=0&totalRows_rsRead=17,但浏览器显示的是:http://localhost/mvcnew/public/index.php/user/guestbook/show?pageNum_rsRead=0&totalRows_rsRead=1781,而且,接下来,不管点击“上一页”还是“下一页”,最后的那个数字会越变却奇怪,从而,使得整个程序运行出现紊乱。
试验了N种方法,最后,发现原因竟然是应将prinf()改为sprintf();即,将程序中的
printf("%s?pageNum_rsRead=%d%s",$this->currentPage,max(0,$this->pageNum_rsRead-1),$this->queryString_rsRead);
printf("%s?pageNum_rsRead=%d%s",$this->currentPage,min($this->totalPages_rsRead,$this->pageNum_rsRead+1),
分别改为:
sprintf("%s?pageNum_rsRead=%d%s",$this->currentPage,max(0,$this->pageNum_rsRead-1),$this->queryString_rsRead);
sprintf("%s?pageNum_rsRead=%d%s",$this->currentPage,min($this->totalPages_rsRead,$this->pageNum_rsRead+1),
程序一切运行正常了。
谁能解释原因?
另外,谁能解释一下:
function MakeHyperlink($text){
$text=preg_replace("/((http(s?):\/\/)|(www\.))([\S\.]+)\b/i","<a href=\"http$3://$4$5\" target=\"_blank\">$2$4$5</a>",$text);//执行正则表达式的搜索和替换,http地址处理
$text=preg_replace("/([\w\.]+)(@)([\S\.]+)\b/i","<a href=\"mailto:$0\">$0</a>",$text);//e-mail 处理
return nl2br($text);
}