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

各位大哥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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment