Home  >  Article  >  Backend Development  >  PHP高手,我刚刚学PHP,在生成静态分页遇到了点有关问题,就是反页的有关问题,希望可以給出一个漂亮完整的翻页代码,多谢

PHP高手,我刚刚学PHP,在生成静态分页遇到了点有关问题,就是反页的有关问题,希望可以給出一个漂亮完整的翻页代码,多谢

WBOY
WBOYOriginal
2016-06-13 10:18:13816browse

各位大哥PHP高手,小弟我刚刚学PHP,在生成静态分页遇到了点问题,就是反页的问题,希望可以給出一个漂亮完整的翻页代码,谢谢!




php生成静态html分页实现方法




$conn=mysql_connect('localhost','root','123456')
  or die('连接失败:'.mysql_error());
//选择数据表
if (mysql_select_db('bbs',$conn))
  {
  echo'选择数据库成功!'.'

';
  
  }
  else
  {
  echo'数据库选择失败!'.mysql_error().'

';
  }
@header("Content-Type: text/html; charset=utf-8");
mysql_query("SET NAMES 'utf8'");
 
$fp = fopen ("moban/temp.html","r");
$content = fread ($fp,filesize ("moban/temp.html"));
$onepage =2;
$sql = "select id from message";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
$filedir="news_1";
if (!is_dir($filedir)) {
//如果不存在就建立
mkdir($filedir,0777);
}
for ($i = 0;$iif ($i == 0){
$indexpath = "$filedir/index.html";
} else {
$indexpath = "$filedir/index_".$i.".html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select * from message limit $start,$onepage";
$result=mysql_query($sql_for_page);
 while($row=mysql_fetch_array($result))
 {
 $list .=$row['id']."   ".$row['title'].'
'.$row['content'].'
'; 
 } 
$content1 = str_replace ("{ articletable }",$list.$i,$content);
//分页
$list1 = '';
for ($j = 0;$jif ($j == 0){
$list1 .= '第'.$j.'页 |';
} else {
$list1 .= "第".$j."页 |";
}
}
$content2 = str_replace("{ mune }",$list1,$content1);
  
if (is_file ($indexpath)){
@unlink ($indexpath); //若文件已存在,则删除
}
$handle = fopen ($indexpath,"w"); //打开文件指针,创建文件
/*
  检查文件是否被创建且可写
*/
if (!is_writable ($indexpath)){
echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo
}
if (!fwrite ($handle,$content2)){ //将信息写入文件
echo "生成文件".$indexpath."失败!"; //修改为echo

fclose ($handle); //关闭指针
}
fclose ($fp);die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
?>



------解决方案--------------------
/*
$fp=fopen("tmp.htm","r");//只读打开模板
$str=fread($fp,filesize("tmp.htm"));//读取模板中内容
//下一步将要替换的内容替换成要生成的内容
$str=str_replace("{title}",'新标题',$str);//将title替换成新标题
$str=str_replace("{content}",'新内容',$str);//将content替换成新内容
fclose($fp);//关闭文件

$handle=fopen('news.htm',"w");//写入方式打开新闻路径
fwrite($handle,$str);//把刚才替换的内容写进生成HTM文件
fclose($handle);
echo "生成成功";
*/
//批量生成
$row=array (array ("新闻标题","新闻内容"),array("新闻标题2","新闻内容2"));//二维数组
foreach($row as $id => $val){
 $title=$val[0];
 $content=$val[1];
 $path=$id.'.htm';
 $fp=fopen("tmp.htm","r");//只读打开模板
 $str=fread($fp,filesize("tmp.htm"));
 //$str=file_get_contents("tmp.htm","r");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn