首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  PHP+MYSQL编程 » [求助]生成静态页面问题  


  作者:slopesun
  注册时间:2004-09-07
  主题/回复:58/67
  积分:753
  等级:★★★(六级)
  称号:声名鹊起

  ken.wang@esolux...
  185627321
  www.

 

 发表:2005-06-14 20:31:52 阅读 2230 次 回复 2 次 得分1  |   字号 字色
[求助]生成静态页面问题
搜索了一下关于php生成静态页面问题,然后弄了个程序,但是出现了错误.
错误描述:
数据库里面有5条信息,生成的index.html,index1.html,index2.html所显示的announce_id都是1,2 不知道是什么原因.也就是所分爷的效果没出来.
程序如下:
<? 

$sql_conn=@mysql_pconnect("localhost","root","root") or die ("can not connect the server");
   $sql_db=@mysql_select_db("test",$sql_conn) or die ("can not connect the database");
  $fp=fopen("temp.html","r");
  $content  = fread ($fp,filesize("temp.html"));
  $onepage =2; 
  $sql = "select * from announce";
  $query = mysql_query ($sql);
  $num= mysql_num_rows ($query); 
  $allpages  = ceil ($num / $onepage); 
  for ($i = 0;$i<$allpages; $i++)

   if ($i == 0)
   { 
 $indexpath = "index.html"; 
   } else 
   { 
 $indexpath = "index_".$i.".html"; 
   }
   $start = $i * $onepage;   
   $list =" "; 
   $sql_for_page="select announce_id,title from announce limit $start,$onepage"; 

   $query_for_page=mysql_query ($sql_for_page); 
   while ($result =mysql_fetch_array($query_for_page)){ 
 $list.=$result[announce_id]."<br>"; 
   } 

   $content = str_replace ("{mycontent}",$list,$content); 
   if (is_file ($indexpath)){ 
 @unlink ($indexpath); //若文件已存在,则删除 
   } 
   $handle  = fopen ($indexpath,"w"); //打开文件指针,创建文件 
   /* 
   检查文件是否被创建且可写 
   */ 
   if (!is_writable ($indexpath)){ 
 echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo 
   } 
   if (!fwrite ($handle,$content)){ //将信息写入文件 
 echo "生成文件".$indexpath."失败!"; //修改为echo 
   } 
   fclose ($handle); //关闭指针 


 fclose ($fp); 
  die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!"); 
?>
 
 1#楼  
 
  回复人:一起PHP
  注册时间:2004-02-27
  主题/回复:247/1521
  积分:4649
  等级:★★★★★☆(十一级)
  称号:论坛圣人

   
 1#楼 发表于2005-06-15 01:58:05  评分:1 

仔细看了20多分钟,没有发现很明显的错误。
你把 
$sql_for_page="select announce_id,title from announce limit $start,$onepage"; 
这一句的下面,加上一句 echo $sql_for_page;
也就是让它输出每次的sql语句,重点看一下limit后面的数字是不是对。
也就是说,你这个程序一共循环三次,分别输出如下SQL语句
select .....limit 0,2
select .....limit 2,2
select .....limit 4,2
你看看是不是正确。如果这个正确的话,查询出来的结果也应该正确。

 2#楼  
 
  回复人:slopesun
  注册时间:2004-09-07
  主题/回复:58/67
  积分:753
  等级:★★★(六级)
  称号:声名鹊起

   
 2#楼 发表于2005-06-15 17:50:42  评分:× 

这是我的测试过程:
在$content = str_replace ("{mycontent}",$list,$content);前
print_r($list);得到结果
1
2
3
4
5

在$content = str_replace ("{mycontent}",$list,$content); 后
print_r($content);得到结果
1
2
1
2
1
2

问题就在这了.但看了半天也没找出办法,附上数据库和temp.html.有兴趣的朋友也可以测试一下,看看到底是什么原因
数据库:
-- 表的结构 `announce`
-- 

CREATE TABLE `announce` (
  `announce_id` int(11) NOT NULL auto_increment,
  `title` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`announce_id`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;

-- 
-- 导出表中的数据 `announce`
-- 

INSERT INTO `announce` VALUES (1, 'qqqqqqqqqqqqqqqqqqqqqqqq');
INSERT INTO `announce` VALUES (2, 'wwwwwwwww');
INSERT INTO `announce` VALUES (3, '');
INSERT INTO `announce` VALUES (4, 'rr');
INSERT INTO `announce` VALUES (5, 'tttttttt');


temp.html
<HTML>
  <TITLE>{title}</TITLE>
  <BODY>
 {mycontent}
  </BODY>
</HTML>
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.007sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版