æ¯è¾è¯¦ç»PHPçæéæ页é¢æç¨_PHPæç¨
程序员文章站
2024-02-06 08:46:28
...
ä¸ï¼PHPèæ¬ä¸å¨æ页é¢ã
ããPHPèæ¬æ¯ä¸ç§æå¡å¨ç«¯èæ¬ç¨åºï¼å¯éè¿åµå ¥çæ¹æ³ä¸HTMLæ件混åï¼ä¹å¯ä»¥ç±»ï¼å½æ°å°è£ çå½¢å¼ï¼ä»¥æ¨¡æ¿çæ¹å¼å¯¹ç¨æ·è¯·æ±è¿è¡å¤çãæ 论以ä½ç§æ¹å¼ï¼å®çåºæ¬åçæ¯è¿æ ·çãç±å®¢æ·ç«¯æåºè¯·æ±ï¼è¯·æ±æä¸é¡µé¢ã----->ãWEBæå¡å¨å¼å ¥æå®ç¸åºèæ¬è¿è¡å¤çã----->ãèæ¬è¢«è½½å ¥æå¡å¨ã----->ãç±æå¡å¨æå®çPHP解æå¨å¯¹èæ¬è¿è¡è§£æå½¢æHTMLè¯è¨å½¢å¼ã---->ãå°è§£æåçHTMLè¯å¥ä»¥å çæ¹å¼ä¼ åç»æµè§å¨ãç±æ¤ä¸é¾çåºï¼å¨é¡µé¢åéå°æµè§å¨åï¼PHPå°±ä¸åå¨äºï¼å·²è¢«è½¬å解æ为HTMLè¯å¥ã客æ·è¯·æ±ä¸ºä¸å¨ææ件ï¼äºå®ä¸å¹¶æ²¡æçæ£çæ件åå¨å¨é£éï¼æ¯PHP解æèæç¸å¯¹åºç页é¢ï¼ç¶ååéåæµè§å¨ãè¿ç§é¡µé¢å¤çæ¹å¼è¢«ç§°ä¸ºâå¨æ页é¢âã
äºï¼éæ页é¢ã
ããéæ页é¢æ¯æå¨æå¡å¨ç«¯ç¡®å®åå¨çä» å«HTML以åJSï¼CSSç客æ·ç«¯è¿è¡èæ¬ç页é¢ãå®çå¤çæ¹å¼æ¯ãç±å®¢æ·ç«¯æåºè¯·æ±ï¼è¯·æ±æä¸é¡µé¢ã---->ãWEBæå¡å¨ç¡®è®¤å¹¶è½½å ¥æä¸é¡µé¢ã---->ãWEBæå¡å¨å°è¯¥é¡µé¢ä»¥å çå½¢å¼ä¼ éåæµè§å¨ãç±è¿ä¸è¿ç¨ï¼æ们对æ¯ä¸ä¸å¨æ页é¢ï¼å³å¯æ¹ç°ãå¨æ页é¢éç±WEBæå¡å¨çPHP解æå¨è¿è¡è§£æï¼èä¸é常è¿éè¿æ¥æ°æ®åºï¼è¿è¡æ°æ®åºååæä½ï¼ç¶åæè½å½¢æHTMLè¯è¨ä¿¡æ¯å ï¼èéæ页é¢ï¼æ 须解æï¼æ é¡»è¿æ¥æ°æ®åºï¼ç´æ¥åéï¼å¯å¤§å¤§åè½»æå¡å¨ååï¼æé«æå¡å¨è´è½½è½åï¼å¤§å¹ æä¾é¡µé¢æå¼é度åç½ç«æ´ä½æå¼é度ãä½å ¶ç¼ºç¹æ¯ï¼ä¸è½å¨æå°å¯¹è¯·æ±è¿è¡å¤çï¼æå¡å¨ä¸å¿ 须确å®åå¨è¯¥æ件ã
ä¸ï¼æ¨¡æ¿å模æ¿è§£æã
ãã模æ¿å³å°æªå¡«å å 容htmlæ件ãä¾å¦ï¼
ãtemp.html
Code:
{ title }
this is a { file } file's templets
PHPå¤çï¼
ãtempletest.php
Code:
$title = "æè¿å½é æµè¯æ¨¡æ¿";
$file = "TwoMax Inter test templet,
authorï¼Matrix@Two_Max";
ã$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file }",$file,$content);
$content .= str_replace ("{ title }",$title,$content);
echo $content;
?>
ãã模æ¿è§£æå¤çï¼å³å°ç»PHPèæ¬è§£æå¤çåå¾åºçç»æå¡«å (content)è¿æ¨¡æ¿çå¤çè¿ç¨ãé常åå©äºæ¨¡æ¿ç±»ãç®åè¾æµè¡ç模æ¿è§£æç±»æphplibï¼smartyï¼fastsmartyççã模æ¿è§£æå¤ççåçé常为æ¿æ¢ãä¹æäºç¨åºåä¹ æ¯å°å¤æï¼å¾ªç¯çå¤çæ¾è¿æ¨¡æ¿æ件ä¸ï¼ç¨è§£æç±»å¤çï¼å ¸ååºç¨ä¸ºblockæ¦å¿µï¼ç®åæ¥è¯´å³ä¸ºä¸ä¸ªå¾ªç¯å¤çãç±PHPèæ¬æå®å¾ªç¯æ¬¡æ°ï¼å¦ä½å¾ªç¯ä»£å ¥çï¼åç±æ¨¡æ¿è§£æç±»å ·ä½å®æ½è¿äºæä½ã
ãã好äºï¼å¯¹æ¯è¿éæ页é¢ä¸å¨æ页é¢åèªçä¼å£ï¼ç°å¨æ们就æ¥è¯´è¯´ï¼å¦ä½ç¨PHPçæéææ件ã
ããPHPçæéæ页é¢å¹¶ä¸æ¯æPHPçå¨æ解æï¼è¾åºHTML页é¢ï¼èæ¯æç¨PHPå建HTML页é¢ãåæ¶å 为HTMLçä¸å¯åæ§ï¼æ们å建çHTMLè¥æä¿®æ¹ï¼åéå æéæ°çæå³å¯ã(å½ç¶ä½ ä¹å¯ä»¥éæ©ç¨æ£åè¿è¡ä¿®æ¹ï¼ä½ä¸ªäººè®¤ä¸ºé£æ ·ååä¸å¦å æéæ°çææ¥å¾å¿«æ·ï¼æäºå¾ä¸å¿å¤±ã)
ããè¨å½æ£ä¼ ãç¨è¿PHPæ件æä½å½æ°çPHP FANSç¥éï¼PHPä¸æä¸ä¸ªæ件æä½å½æ°fopenï¼å³æå¼æ件ãè¥æ件ä¸åå¨ï¼åå°è¯å建ãè¿å³æ¯PHPå¯ä»¥ç¨æ¥å建HTMLæ件çç论åºç¡ãåªè¦ç¨æ¥åæ¾HTMLæ件çæ件夹æåæé(å³æéå®ä¹0777)ï¼å³å¯å建æ件ãï¼é对UNIXç³»ç»èè¨ï¼Winç³»ç»æ é¡»èèãï¼ä»ä»¥ä¸ä¾ä¸ºä¾ï¼è¥æ们修æ¹æåä¸å¥ï¼å¹¶æå®å¨testç®å½ä¸çæä¸ä¸ªå为test.htmlçéææ件ï¼
Code:
$title = "æè¿å½é æµè¯æ¨¡æ¿";
$file = "TwoMax Inter test templet,
authorï¼Matrix@Two_Max";
ã$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file }",$file,$content);
$content .= str_replace ("{ title }",$title,$content);
// echo $content;
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //æå¼æ件æéï¼å建æ件
/*
ãæ£æ¥æ件æ¯å¦è¢«å建ä¸å¯å
*/
if (!is_writable ($filename)){
die ("æ件ï¼".$filename."ä¸å¯åï¼è¯·æ£æ¥å ¶å±æ§åéè¯ï¼");
}
if (!fwrite ($handle,$content)){ //å°ä¿¡æ¯åå ¥æ件
die ("çææ件".$filename."失败ï¼");
}
fclose ($handle); //å ³éæé
die ("å建æ件".$filename."æåï¼");
?>
ããå®é åºç¨ä¸å¸¸è§é®é¢è§£å³æ¹æ¡åèï¼
ããä¸ï¼æç« å表é®é¢ï¼
ãã
ããå¨æ°æ®åºä¸å建å段ï¼è®°å½æ件åï¼æ¯çæä¸ä¸ªæ件ï¼å°èªå¨çæçæ件ååå ¥æ°æ®åºï¼å¯¹äºæ¨èæç« ï¼åªéæååæ¾éææ件çæå®æ件夹ä¸ç该页é¢å³å¯ãå©ç¨PHPæä½å¤çæç« å表ï¼å为å符串ï¼çæ页é¢æ¶æ¿æ¢æ¤å符串å³å¯ãå¦ï¼å¨é¡µé¢ä¸æ¾ç½®æç« å表çè¡¨æ ¼å å ¥æ è®°{ articletable }ï¼èå¨PHPå¤çæ件ä¸ï¼
Code:
$title = "æè¿å½é æµè¯æ¨¡æ¿";
$file = "TwoMax Inter test templet,
authorï¼Matrix@Two_Max";
ã$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file }",$file,$content);
$content .= str_replace ("{ title }",$title,$content);
// çæå表å¼å§
$list = '';
$sql = "select id,title,filename from article";
$query = mysql_query ($sql);
while ($result = mysql_fetch_array ($query)){
$list .= ''.$result['title'].'';
}
$content .= str_replace ("{ articletable }",$list,$content);
//çæå表ç»æ
// echo $content;
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //æå¼æ件æéï¼å建æ件
/*
ãæ£æ¥æ件æ¯å¦è¢«å建ä¸å¯å
*/
if (!is_writable ($filename)){
die ("æ件ï¼".$filename."ä¸å¯åï¼è¯·æ£æ¥å ¶å±æ§åéè¯ï¼");
}
if (!fwrite ($handle,$content)){ //å°ä¿¡æ¯åå ¥æ件
die ("çææ件".$filename."失败ï¼");
}
fclose ($handle); //å ³éæé
die ("å建æ件".$filename."æåï¼");
?>
ããäºï¼å页é®é¢ã
ããå¦æ们æå®å页æ¶ï¼æ¯é¡µ20ç¯ãæåé¢éå表å æç« ç»æ°æ®åºæ¥è¯¢ä¸º45æ¡ï¼åï¼é¦å æ们éè¿æ¥è¯¢å¾å°å¦ä¸åæ°ï¼1ï¼æ»é¡µæ°ï¼2ï¼æ¯é¡µç¯æ°ã第äºæ¥ï¼for ($i = 0; $i Code:
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$onepage = '20';
$sql = "select id from article where channel='$channelid'";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
for ($i = 0;$iif ($i == 0){
$indexpath = "index.html";
} else {
$indexpath = "index_".$i."html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
$query_for_page = mysql_query ($sql_for_page);
while ($result = $query_for_page){
$list .= ''.$title.'';
}
$content = str_replace ("{ articletable }",$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 ("çæå页æ件å®æï¼å¦çæä¸å®å ¨ï¼è¯·æ£æ¥æ件æéç³»ç»åéæ°çæï¼");
?>
ãã大è´æè·¯å¦æ¤ï¼å ¶ä¸å¦å ¶å®æ°æ®çæï¼æ°æ®è¾å ¥è¾åºæ£æ¥ï¼å页å 容æåçå¯é æ å¨é¡µé¢ä¸å å ¥ã
ããå¨å®é æç« ç³»ç»å¤çè¿ç¨å½ä¸ï¼è¿æ许å¤é®é¢æå¾ èèï¼ä¸å¨æ页é¢ä¸åä¹å¤ï¼é注æçå°æ¹è¿æå¾å¤ãä½å¤§è´æè·¯å³æ¯å¦æ¤ï¼å ¶å®æ¹é¢å¯ä¸¾ä¸åä¸èå¾ã
ç¨PHPå¶ä½éæç½ç«ç模æ¿æ¡æ¶
ã模æ¿è½å¤æ¹åç½ç«çç»æãæ¬æéè¿°å¦ä½éè¿PHP 4çä¸ä¸ªæ°åè½å模æ¿ç±»ï¼å¨ç±å¤§ééæHTML页é¢ææçç½ç«ä¸å·§å¦å°è¿ç¨æ¨¡æ¿æ§å¶é¡µé¢å¸å±ã
æ纲ï¼
===================================
å离åè½åå¸å±
é¿å 页é¢å ç´ éå¤
éæç½ç«ç模æ¿æ¡æ¶
===================================
å离åè½åå¸å±
ãã é¦å æ们æ¥ççåºç¨æ¨¡æ¿ç两个主è¦ç®çï¼
å离åè½ï¼PHPï¼åå¸å±ï¼HTMLï¼
é¿å 页é¢å ç´ éå¤
ãã 第ä¸ä¸ªç®çæ¯è°è®ºå¾æå¤çç®çï¼å®è®¾æ³çæ å½¢æ¯ï¼ä¸ç»ç¨åºåç¼åç¨äºçæ页é¢å 容çPHPèæ¬ï¼åæ¶å¦ä¸ç»è®¾è®¡äººå设计HTMLåå¾å½¢ä»¥æ§å¶é¡µé¢çæç»å¤è§ãå离åè½åå¸å±çåºæ¬ææ³å°±æ¯ä½¿å¾è¿ä¸¤ç»äººè½å¤åèªç¼åå使ç¨ç¬ç«çä¸ç»æ件ï¼ç¨åºååªéå ³å¿é£äºåªå å«PHP代ç çæ件ï¼æ éå ³å¿é¡µé¢çå¤è§
ï¼è页é¢è®¾è®¡äººåå¯ä»¥ç¨èªå·±æçæçå¯è§åç¼è¾å¨è®¾è®¡é¡µé¢å¸å±ï¼æ éæ å¿ç ´åä»»ä½åµå ¥å°é¡µé¢çPHP代ç ã
ãã å¦æä½ æ¾ç»çè¿å ä¸ªå ³äºPHP模æ¿çæç¨ï¼é£ä¹ä½ åºè¯¥å·²ç»æç½æ¨¡æ¿çå·¥ä½æºå¶ãèèä¸ä¸ªç®åç页é¢å±é¨ï¼é¡µé¢çä¸æ¹æ¯é¡µå¤´ï¼å·¦è¾¹æ¯å¯¼èªæ¡ï¼å ¶ä½é¨åæ¯å 容åºåãè¿ç§ç½ç«å¯ä»¥æ¥æå¦ä¸æ¨¡æ¿æ件ï¼
模æ¿ç¤ºä¾
Foo
Bar
ãã å¯ä»¥çåºé¡µé¢å¦ä½ç±è¿äºæ¨¡æ¿æé èæï¼main模æ¿æ§å¶çæ´ä¸ªé¡µé¢çå¸å±ï¼header模æ¿åleftnav模æ¿æ§å¶ç页é¢çå ¬å ±å ç´ ãè±æ¬å·â{}âéé¢çæ è¯ç¬¦æ¯å 容å ä½ç¬¦ã使ç¨æ¨¡æ¿æ主è¦ç好å¤å¨äºçé¢è®¾è®¡è è½å¤æç §èªå·±çææ¿ç¼è¾è¿äºæ件ï¼æ¯å¦è®¾ç½®åä½ãä¿®æ¹é¢è²åå¾å½¢ï¼æè å®å ¨å°æ¹å页é¢çå¸å±ãçé¢è®¾è®¡è å¯ä»¥ç¨ä»»ä½æ®éHTMLç¼è¾å¨æè å¯è§åå·¥å ·ç¼è¾è¿äºé¡µé¢ï¼å 为è¿äºæ件é½åªå å«HTML代ç ï¼æ²¡æä»»ä½PHP代ç ã
ã PHP代ç å ¨é¨ä¿åå°åç¬çæ件ä¸ï¼è¿ä¸ªæ件ä¹å°±æ¯ç±é¡µé¢URLå®é è°ç¨çæ件ãWebæå¡å¨éè¿PHPå¼æ解æ该æ件ï¼ç¶åæç»æè¿åç»æµè§å¨ãä¸è¬å°ï¼PHP代ç æ»æ¯å¨æå°çæ页é¢å 容ï¼æ¯å¦æ¥è¯¢æ°æ®åºæè æ§è¡æç§è®¡ç®çãä¸é¢æ¯ä¸ä¸ªä¾åï¼
// example.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );
// æ¤å¤çPHP代ç 设置$contentä½¿å ¶å å«åéç页é¢å 容
$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
ãã è¿éæ们使ç¨çæ¯æµè¡çFastTemplate模æ¿ç±»ï¼ä½å ¶åºæ¬æ路对äºå ¶ä»è®¸å¤æ¨¡æ¿ç±»æ¥è¯´é½ä¸æ ·ãé¦å ä½ å®ä¾åä¸ä¸ªç±»ï¼åè¯å®å°åªéå»å¯»æ¾æ¨¡æ¿æ件以ååªä¸ä¸ªæ¨¡æ¿æ件ä¸é¡µé¢çåªé¨å对åºï¼æ¥ä¸æ¥æ¯çæ页é¢å 容ï¼æç»æèµäºå 容çæ è¯ç¬¦ï¼ç¶åï¼ä¾æ¬¡è§£æå个模æ¿æ件ï¼æ¨¡æ¿ç±»å°æ§è¡å¿ è¦çæ¿æ¢æä½ï¼æåæ解æç»æè¾åºå°æµè§å¨ã
ãã è¿ä¸ªæ件å®å ¨ç±PHP代ç ææï¼ä¸å å«ä»»ä½HTML代ç ï¼è¿æ¯å®æ大çä¼ç¹ãç°å¨ï¼PHPç¨åºåå¯ä»¥éä¸ç²¾åç¼åçæ页é¢å 容ç代ç ï¼èä¸å¿ 为äºå¦ä½çæHTMLå»æ£ç¡®å°æ ¼å¼åæç»é¡µé¢èæ å¿ã
ãã ä½ å¯ä»¥ä½¿ç¨è¿ç§æ¹æ³åä¸é¢çæ件æé åºä¸ä¸ªå®æ´çç½ç«ãå¦æPHP代ç æ¯ä»¥URLä¸çæ¥è¯¢å符串为åºç¡çæ页é¢å 容ï¼ä¾å¦http://www.foo.com/example.php?article=099ï¼ä½ å¯ä»¥æ®æ¤æé åºä¸ä¸ªå®æ´çæå¿ç½ç«ã
ãã å¾å®¹æçåºéç¨æ¨¡æ¿è¿æ第äºä¸ªå¥½å¤ãå¦ä¸ä¾æ示ï¼é¡µé¢å·¦è¾¹ç导èªæ¡åç¬ä¿å为ä¸ä¸ªæ件ï¼æ们åªéç¼è¾è¿ä¸ä¸ªæ¨¡æ¿æ件就å¯ä»¥æ¹åç½ç«ææ页é¢å·¦è¾¹ç导èªæ¡ã
é¿å 页é¢å ç´ éå¤
ãã âè¿ç¡®å®ä¸éâï¼ä½ ä¹è®¸ä¼æ³ï¼âæçç½ç«ä¸»è¦å°±æ¯ç±å¤§éçéæ页é¢ææãç°å¨æå¯ä»¥ä»ææ页é¢ä¸å é¤å®ä»¬çå ¬å ±é¨åï¼è¦æ´æ°è¿äºå ¬å ±é¨åå®å¨å¤ªéº»ç¦äºã以åæå°±å¯ä»¥ç¨æ¨¡æ¿å¶ä½åºå¾å®¹æç»´æ¤çç»ä¸é¡µé¢å¸å±ãâä½äºæ 并éè¿ä¹ç®åï¼â大éçéæ页é¢âéåºäºé®é¢çæå¨ã
ãã 请èèä¸é¢çä¾åãè¿ä¸ªä¾åå®é ä¸åªæä¸ä¸ªexample.php页é¢ï¼å®ä¹æ以è½å¤çææ´ä¸ªç½ç«çææ页é¢ï¼æ¯å 为å®å©ç¨äºURLä¸çæ¥è¯¢å符串ä»æ°æ®åºä¹ç±»çä¿¡æ¯æºå¨æå°æé åºé¡µé¢ã
ãã æ们ä¹ä¸ç大å¤æ°äººæè¿è¡çç½ç«å¹¶ä¸ä¸å®é½ææ°æ®åºæ¯æãæ们çç½ç«å¤§å¤æ°ç±éæ页é¢ææï¼ç¶åç¨PHPå¨è¿éãé£éå ä¸ä¸äºå¨æåè½ï¼æ¯å¦æç´¢å¼æãåé¦è¡¨åçãé£ä¹ï¼å¦ä½å¨è¿ç§ç½ç«ä¸åºç¨æ¨¡æ¿å¢ï¼
ãã æç®åçæ¹æ³æ¯ä¸ºæ¯ä¸ä¸ªé¡µé¢å¤å¶ä¸ä»½PHPæ件ï¼
ç¶åå¨æ¯ä¸ä¸ªé¡µé¢ä¸æPHP代ç é代表å 容çåé设置æåéç页é¢å 容ãä¾å¦ï¼å设æä¸ä¸ªé¡µé¢ï¼å®ä»¬åå«æ¯ä¸»é¡µï¼homeï¼ãå ³äºï¼aboutï¼å产åï¼productï¼ï¼æ们å¯ä»¥ç¨ä¸ä¸ªæ件åå«çæå®ä»¬ãè¿ä¸ä¸ªæ件çå 容é½ç±»å¦ï¼
// home.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );
$content = "
$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
ãã æ¾ç¶ï¼è¿ç§æ¹æ³æä¸ä¸ªé®é¢ï¼æä»¬å¿ é¡»ä¸ºæ¯ä¸ä¸ªé¡µé¢å¤å¶è¿äºå¤æçãçµæ¶å°æ¨¡æ¿çPHP代ç ï¼è¿ä¸éå¤å ¬å ±é¡µé¢å ç´ ä¸æ ·ä½¿å¾é¡µé¢é¾ä»¥ç»´æ¤ï¼ç°å¨æ件åæ··åäºHTMLåPHP代ç ï¼ä¸ºå 容åéèµå¼å°åå¾é常å°é¾ï¼å 为æä»¬å¿ é¡»å¤ç好大éçç¹æ®å符ã
ãã 解å³è¿ä¸ªé®é¢çå ³é®å°±å¨äºå离PHP代ç åHTMLå 容ï¼è½ç¶æ们ä¸è½ä»æ件ä¸å é¤ææçHTMLå 容ï¼ä½å¯ä»¥ç§»åºç»å¤§å¤æ°PHP代ç ã
éæç½ç«ç模æ¿æ¡æ¶
ãã é¦å ï¼æ们象åé¢ä¸æ ·ä¸ºææç页é¢å ¬ç¨å ç´ ä»¥å页é¢æ´ä½å¸å±ç¼å模æ¿æ件ï¼ç¶åä»ææç页é¢å é¤å ¬å ±é¨åï¼åªçä¸é¡µé¢å 容ï¼æ¥ä¸æ¥åå¨æ¯ä¸ªé¡µé¢ä¸å ä¸ä¸è¡PHP代ç ï¼å¦ä¸æ示ï¼
?>
ãã è¿ç§æ¹æ³åºæ¬ä¸è§£å³äºåé¢æå°çåç§é®é¢ãç°å¨æ件éåªæä¸è¡PHP代ç ï¼èä¸æ²¡æä»»ä½ä¸è¡ä»£ç ç´æ¥æ¶åå°æ¨¡æ¿ï¼å æ¤è¦æ¹å¨è¿äºä»£ç çå¯è½æ§æå°ãæ¤å¤ï¼ç±äºHTMLå 容ä½äºPHPæ è®°ä¹å¤ï¼æ以ä¹ä¸åå¨ç¹æ®å符çå¤çé®é¢ãæ们å¯ä»¥å¾å®¹æå°å°è¿ä¸è¡PHP代ç å å ¥å°ææéæHTML页é¢ä¸ã
ãã requireå½æ°å¼å ¥äºä¸ä¸ªPHPæ件ï¼è¿ä¸ªæ件å å«äºææå¿ éçä¸æ¨¡æ¿ç¸å ³çPHP代ç ãå ¶ä¸pageStartå½æ°è®¾ç½®æ¨¡æ¿å¯¹è±¡ä»¥å页é¢æ é¢ï¼pageFinishå½æ°è§£æ模æ¿ç¶åçæç»æåéç»æµè§å¨ã
ãã è¿æ¯å¦ä½å®ç°çå¢ï¼ä¸ºä»ä¹å¨è°ç¨pageFinishå½æ°ä¹åæ件ä¸çHTMLä¸ä¼åéç»æµè§å¨ï¼çæ¡å°±å¨äºPHP 4çä¸ä¸ªæ°åè½ï¼è¿ä¸ªåè½å 许æè¾åºå°æµè§å¨çå 容æªè·å°ç¼å²åºä¹ä¸ã让æ们æ¥ççprepend.phpçå ·ä½ä»£ç ï¼
require('class.FastTemplate.php');
function pageStart($title = '') {
GLOBAL $tpl;
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav'=> 'leftnav.htm' ) );
$tpl->assign('TITLE', $title);
ob_start();
}
function pageFinish() {
GLOBAL $tpl;
$content = ob_get_contents();
ob_end_clean();
$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
}
?>
ããpageStartå½æ°é¦å å建并设置äºä¸ä¸ªæ¨¡æ¿å®ä¾ï¼ç¶åå¯ç¨è¾åºç¼åãæ¤åï¼æææ¥èªé¡µé¢æ¬èº«çHTMLå 容é½å°è¿å ¥ç¼åãpageFinishå½æ°ååºç¼åä¸çå 容ï¼ç¶åå¨æ¨¡æ¿å¯¹è±¡ä¸æå®è¿äºå 容ï¼æå解æ模æ¿å¹¶è¾åºå®æåç页é¢ã
ãã è¿å°±æ¯æ´ä¸ªæ¨¡æ¿æ¡æ¶å ¨é¨çå·¥ä½è¿ç¨äºãé¦å ç¼åå å«äºç½ç«å个页é¢å ¬å ±å ç´ ç模æ¿ï¼ç¶åä»ææ页é¢ä¸å é¤å ¨é¨å ¬å ±ç页é¢å¸å±ä»£ç ï¼ä»£ä¹ä»¥ä¸è¡æ°¸è¿æ éæ¹å¨çPHP代ç ï¼åæFastTemplateç±»æ件åprepend.phpå å ¥å°å å«è·¯å¾ï¼è¿æ ·ä½ å°±å¾å°äºä¸ä¸ªé¡µé¢å¸å±å¯ä»¥éä¸æ§å¶çç½ç«ï¼å®æçæ´å¥½çå¯é æ§åå¯ç»´æ¤æ§ï¼èä¸ç½ç«çº§ç大èå´ä¿®æ¹ä¹åå¾ç¸å½å®¹æã
ãã æ¬æä¸è½½å å å«
äºä¸ä¸ªå¯è¿è¡ç示ä¾ç½ç«ï¼å®ç代ç 注éè¦æ¯åé¢ç代ç 注éæ´è¯¦ç»ä¸äºãFastTemplateç±»å¯ä»¥å¨http://www.thewebmasters.net/æ¾å°ï¼ææ°ççæ¬å·æ¯1.1.0ï¼é£éè¿æä¸ä¸ªç¨äºä¿è¯è¯¥ç±»å¨PHP 4ä¸æ£ç¡®è¿è¡çå°è¡¥ä¸ãæ¬æä¸è½½ä»£ç ä¸ç类已ç»ç»è¿è¯¥è¡¥ä¸çä¿®æ£ã
PHPç®æçæéæ页é¢
/*
* æ件åï¼index.php
*/
require "conn.php";
$query = "select * from news order by datetime desc";
$result = mysql_query($query);
?>
NEWS
/*
æ件åï¼AddNews.php
ç®æå¨ææ·»å çæéææ°é»é¡µé¢
#
# 表çç»æ `news`
#
CREATE TABLE `news` (
`newsid` int(11) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
`datetime` datetime NOT NULL default '0000-00-00 00:00:00',
KEY `newsid` (`newsid`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;
*/
?>
ç¨PHPçæéæç½é¡µç两个å½æ°
å¨æè¿å å¹´ï¼ä¸ç»´ç½ï¼ä¹ç§°ç¯çä¿¡æ¯ç½ï¼å³WWWï¼ä¸ææ¹åä¿¡æ¯å¤çææ¯çé¢è²ãWEBå·²ç»å¿«éå°æ为ä¸ç§ææçåªä»ï¼å¹¶éå人们ååä¸æ²éååä½ãå ä¹ææçä¿¡æ¯ææ¯é¢åé½æ®éåå°WEBçå½±åãWeb访é®å¸¦æ¥æ´å¤ç¨æ·åæ´å¤æ°æ®ï¼è¿æå³çç»æå¡å¨åæ°æ®åºæ´å¤åååæç»ç¨æ·å¾å°è¶æ¥è¶æ ¢çååºé度ãä¸ä¸æé å¢å CPUï¼ç£ç驱å¨å¨åå åæ¥è·ä¸è¿ç§å¢é¿çéæ±ç¸æ¯ï¼ WEBå¨æç½é¡µé¢éæååºè¯¥æ¯ä¸ä¸ªæ´å®ç¨ï¼æ´ç»æµçéæ©ã
ç¨PHPå®ç°WEBå¨æç½é¡µéæåçå ·ä½å®ç°å½æ°å¦function gen_static_file()æ示
function gen_static_file($program, $filename)
{
$program 1= "/usr/local/apache/htdocs/php/" . $program;
$filename1 = "/usr/local/apache/htdocs/ static_html/" . $filename;
$cmd_str = "/usr/local/php4/bin/php " . $program1 . " } " . $filename1 . " ";
system($cmd_str);
echo $filename . " generated.ãbrã";
}
è¿ä¸ªå½æ°æ¯å®ç°éæåçå ³é®ï¼å³PHPå¨æ页é¢ç¨åºä¸æ¯è¢«éå°æµè§å¨ä¸ï¼èæ¯è¾å ¥å°å为$filenameçæ件ä¸å»ï¼å¦å¾2ï¼ã两个åæ°ä¸$programæ¯PHPå¨æ页é¢ç¨åºï¼$filenameæ¯çæçéæ页é¢çååï¼å¯æ ¹æ®éè¦èªå·±å¶å®å½åè§åï¼è¿ä¸ç¹å¾éè¦ï¼è§ä¸æï¼ï¼/usr/local/php4/bin/phpæ¯PHPä¸å ·ææç¨åºè¾å ¥æ件åè½çé¨åï¼Systemæ¯PHPä¸æ§è¡å¤é¨å½ä»¤çå½æ°ãæ们è¿å¯ä»¥çåºææçæå¨æ页é¢çphpç¨åºéæ¾å¨/php/ç®å½ä¸ï¼æææ°äº§ççéæ页é¢åä¼åºç°å¨/static_html/ç®å½ä¸ï¼è¿äºè·¯å¾å¯ä»¥æ ¹æ®å ·ä½éè¦è®¾ç½®ï¼ã
ä¸é¢è®©æä»¬ä¸¾ä¸ªå ·ä½ä¾åï¼çä¸ä¸college_static.phpçéæ页é¢æ¯ææ ·çæçã
function gen_college_static ()
{
for ($i = 0; $i ã= 32; $i++ã
{
putenv("province_id=" . $i); //*.phpæ件ä»æ°æ®åºåæ°æ®æ¶è¦ç¨å°ã
$filename = " college_static". $i . ".html";
gen_static_file("college_static.php", $filename);
}
ä»è¿ä¸ªå½æ°æ们å¯ä»¥çå°éè¿è°ç¨å½æ°gen_static_fileï¼ï¼, college_static.phpç»è¿éæåï¼åæäº33个éæ页é¢college.static0.html~college.static33.htmlï¼å ¶ä¸$filenameä¼éç$Içååèååãå½ç¶ä¹å¯ä»¥ä»æ°æ®åºä¸ç´æ¥åå¼ï¼æ¥æ§å¶çæçéæ页é¢ç个æ°åååï¼å ¶ä»ç¨åºå¯¹çæçéæ页é¢çè°ç¨åºåéæ页é¢çå½åè§åä¸è´ã
ããPHPèæ¬æ¯ä¸ç§æå¡å¨ç«¯èæ¬ç¨åºï¼å¯éè¿åµå ¥çæ¹æ³ä¸HTMLæ件混åï¼ä¹å¯ä»¥ç±»ï¼å½æ°å°è£ çå½¢å¼ï¼ä»¥æ¨¡æ¿çæ¹å¼å¯¹ç¨æ·è¯·æ±è¿è¡å¤çãæ 论以ä½ç§æ¹å¼ï¼å®çåºæ¬åçæ¯è¿æ ·çãç±å®¢æ·ç«¯æåºè¯·æ±ï¼è¯·æ±æä¸é¡µé¢ã----->ãWEBæå¡å¨å¼å ¥æå®ç¸åºèæ¬è¿è¡å¤çã----->ãèæ¬è¢«è½½å ¥æå¡å¨ã----->ãç±æå¡å¨æå®çPHP解æå¨å¯¹èæ¬è¿è¡è§£æå½¢æHTMLè¯è¨å½¢å¼ã---->ãå°è§£æåçHTMLè¯å¥ä»¥å çæ¹å¼ä¼ åç»æµè§å¨ãç±æ¤ä¸é¾çåºï¼å¨é¡µé¢åéå°æµè§å¨åï¼PHPå°±ä¸åå¨äºï¼å·²è¢«è½¬å解æ为HTMLè¯å¥ã客æ·è¯·æ±ä¸ºä¸å¨ææ件ï¼äºå®ä¸å¹¶æ²¡æçæ£çæ件åå¨å¨é£éï¼æ¯PHP解æèæç¸å¯¹åºç页é¢ï¼ç¶ååéåæµè§å¨ãè¿ç§é¡µé¢å¤çæ¹å¼è¢«ç§°ä¸ºâå¨æ页é¢âã
äºï¼éæ页é¢ã
ããéæ页é¢æ¯æå¨æå¡å¨ç«¯ç¡®å®åå¨çä» å«HTML以åJSï¼CSSç客æ·ç«¯è¿è¡èæ¬ç页é¢ãå®çå¤çæ¹å¼æ¯ãç±å®¢æ·ç«¯æåºè¯·æ±ï¼è¯·æ±æä¸é¡µé¢ã---->ãWEBæå¡å¨ç¡®è®¤å¹¶è½½å ¥æä¸é¡µé¢ã---->ãWEBæå¡å¨å°è¯¥é¡µé¢ä»¥å çå½¢å¼ä¼ éåæµè§å¨ãç±è¿ä¸è¿ç¨ï¼æ们对æ¯ä¸ä¸å¨æ页é¢ï¼å³å¯æ¹ç°ãå¨æ页é¢éç±WEBæå¡å¨çPHP解æå¨è¿è¡è§£æï¼èä¸é常è¿éè¿æ¥æ°æ®åºï¼è¿è¡æ°æ®åºååæä½ï¼ç¶åæè½å½¢æHTMLè¯è¨ä¿¡æ¯å ï¼èéæ页é¢ï¼æ 须解æï¼æ é¡»è¿æ¥æ°æ®åºï¼ç´æ¥åéï¼å¯å¤§å¤§åè½»æå¡å¨ååï¼æé«æå¡å¨è´è½½è½åï¼å¤§å¹ æä¾é¡µé¢æå¼é度åç½ç«æ´ä½æå¼é度ãä½å ¶ç¼ºç¹æ¯ï¼ä¸è½å¨æå°å¯¹è¯·æ±è¿è¡å¤çï¼æå¡å¨ä¸å¿ 须确å®åå¨è¯¥æ件ã
ä¸ï¼æ¨¡æ¿å模æ¿è§£æã
ãã模æ¿å³å°æªå¡«å å 容htmlæ件ãä¾å¦ï¼
ãtemp.html
Code:
å¤å¶ä»£ç 代ç å¦ä¸:
this is a { file } file's templets
PHPå¤çï¼
ãtempletest.php
Code:
$title = "æè¿å½é æµè¯æ¨¡æ¿";
$file = "TwoMax Inter test templet,
authorï¼Matrix@Two_Max";
ã$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file }",$file,$content);
$content .= str_replace ("{ title }",$title,$content);
echo $content;
?>
ãã模æ¿è§£æå¤çï¼å³å°ç»PHPèæ¬è§£æå¤çåå¾åºçç»æå¡«å (content)è¿æ¨¡æ¿çå¤çè¿ç¨ãé常åå©äºæ¨¡æ¿ç±»ãç®åè¾æµè¡ç模æ¿è§£æç±»æphplibï¼smartyï¼fastsmartyççã模æ¿è§£æå¤ççåçé常为æ¿æ¢ãä¹æäºç¨åºåä¹ æ¯å°å¤æï¼å¾ªç¯çå¤çæ¾è¿æ¨¡æ¿æ件ä¸ï¼ç¨è§£æç±»å¤çï¼å ¸ååºç¨ä¸ºblockæ¦å¿µï¼ç®åæ¥è¯´å³ä¸ºä¸ä¸ªå¾ªç¯å¤çãç±PHPèæ¬æå®å¾ªç¯æ¬¡æ°ï¼å¦ä½å¾ªç¯ä»£å ¥çï¼åç±æ¨¡æ¿è§£æç±»å ·ä½å®æ½è¿äºæä½ã
ãã好äºï¼å¯¹æ¯è¿éæ页é¢ä¸å¨æ页é¢åèªçä¼å£ï¼ç°å¨æ们就æ¥è¯´è¯´ï¼å¦ä½ç¨PHPçæéææ件ã
ããPHPçæéæ页é¢å¹¶ä¸æ¯æPHPçå¨æ解æï¼è¾åºHTML页é¢ï¼èæ¯æç¨PHPå建HTML页é¢ãåæ¶å 为HTMLçä¸å¯åæ§ï¼æ们å建çHTMLè¥æä¿®æ¹ï¼åéå æéæ°çæå³å¯ã(å½ç¶ä½ ä¹å¯ä»¥éæ©ç¨æ£åè¿è¡ä¿®æ¹ï¼ä½ä¸ªäººè®¤ä¸ºé£æ ·ååä¸å¦å æéæ°çææ¥å¾å¿«æ·ï¼æäºå¾ä¸å¿å¤±ã)
ããè¨å½æ£ä¼ ãç¨è¿PHPæ件æä½å½æ°çPHP FANSç¥éï¼PHPä¸æä¸ä¸ªæ件æä½å½æ°fopenï¼å³æå¼æ件ãè¥æ件ä¸åå¨ï¼åå°è¯å建ãè¿å³æ¯PHPå¯ä»¥ç¨æ¥å建HTMLæ件çç论åºç¡ãåªè¦ç¨æ¥åæ¾HTMLæ件çæ件夹æåæé(å³æéå®ä¹0777)ï¼å³å¯å建æ件ãï¼é对UNIXç³»ç»èè¨ï¼Winç³»ç»æ é¡»èèãï¼ä»ä»¥ä¸ä¾ä¸ºä¾ï¼è¥æ们修æ¹æåä¸å¥ï¼å¹¶æå®å¨testç®å½ä¸çæä¸ä¸ªå为test.htmlçéææ件ï¼
Code:
å¤å¶ä»£ç 代ç å¦ä¸:
$title = "æè¿å½é æµè¯æ¨¡æ¿";
$file = "TwoMax Inter test templet,
authorï¼Matrix@Two_Max";
ã$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file }",$file,$content);
$content .= str_replace ("{ title }",$title,$content);
// echo $content;
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //æå¼æ件æéï¼å建æ件
/*
ãæ£æ¥æ件æ¯å¦è¢«å建ä¸å¯å
*/
if (!is_writable ($filename)){
die ("æ件ï¼".$filename."ä¸å¯åï¼è¯·æ£æ¥å ¶å±æ§åéè¯ï¼");
}
if (!fwrite ($handle,$content)){ //å°ä¿¡æ¯åå ¥æ件
die ("çææ件".$filename."失败ï¼");
}
fclose ($handle); //å ³éæé
die ("å建æ件".$filename."æåï¼");
?>
ããå®é åºç¨ä¸å¸¸è§é®é¢è§£å³æ¹æ¡åèï¼
ããä¸ï¼æç« å表é®é¢ï¼
ãã
ããå¨æ°æ®åºä¸å建å段ï¼è®°å½æ件åï¼æ¯çæä¸ä¸ªæ件ï¼å°èªå¨çæçæ件ååå ¥æ°æ®åºï¼å¯¹äºæ¨èæç« ï¼åªéæååæ¾éææ件çæå®æ件夹ä¸ç该页é¢å³å¯ãå©ç¨PHPæä½å¤çæç« å表ï¼å为å符串ï¼çæ页é¢æ¶æ¿æ¢æ¤å符串å³å¯ãå¦ï¼å¨é¡µé¢ä¸æ¾ç½®æç« å表çè¡¨æ ¼å å ¥æ è®°{ articletable }ï¼èå¨PHPå¤çæ件ä¸ï¼
Code:
å¤å¶ä»£ç 代ç å¦ä¸:
$title = "æè¿å½é æµè¯æ¨¡æ¿";
$file = "TwoMax Inter test templet,
authorï¼Matrix@Two_Max";
ã$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file }",$file,$content);
$content .= str_replace ("{ title }",$title,$content);
// çæå表å¼å§
$list = '';
$sql = "select id,title,filename from article";
$query = mysql_query ($sql);
while ($result = mysql_fetch_array ($query)){
$list .= ''.$result['title'].'';
}
$content .= str_replace ("{ articletable }",$list,$content);
//çæå表ç»æ
// echo $content;
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //æå¼æ件æéï¼å建æ件
/*
ãæ£æ¥æ件æ¯å¦è¢«å建ä¸å¯å
*/
if (!is_writable ($filename)){
die ("æ件ï¼".$filename."ä¸å¯åï¼è¯·æ£æ¥å ¶å±æ§åéè¯ï¼");
}
if (!fwrite ($handle,$content)){ //å°ä¿¡æ¯åå ¥æ件
die ("çææ件".$filename."失败ï¼");
}
fclose ($handle); //å ³éæé
die ("å建æ件".$filename."æåï¼");
?>
ããäºï¼å页é®é¢ã
ããå¦æ们æå®å页æ¶ï¼æ¯é¡µ20ç¯ãæåé¢éå表å æç« ç»æ°æ®åºæ¥è¯¢ä¸º45æ¡ï¼åï¼é¦å æ们éè¿æ¥è¯¢å¾å°å¦ä¸åæ°ï¼1ï¼æ»é¡µæ°ï¼2ï¼æ¯é¡µç¯æ°ã第äºæ¥ï¼for ($i = 0; $i Code:
å¤å¶ä»£ç 代ç å¦ä¸:
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$onepage = '20';
$sql = "select id from article where channel='$channelid'";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
for ($i = 0;$iif ($i == 0){
$indexpath = "index.html";
} else {
$indexpath = "index_".$i."html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
$query_for_page = mysql_query ($sql_for_page);
while ($result = $query_for_page){
$list .= ''.$title.'';
}
$content = str_replace ("{ articletable }",$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 ("çæå页æ件å®æï¼å¦çæä¸å®å ¨ï¼è¯·æ£æ¥æ件æéç³»ç»åéæ°çæï¼");
?>
ãã大è´æè·¯å¦æ¤ï¼å ¶ä¸å¦å ¶å®æ°æ®çæï¼æ°æ®è¾å ¥è¾åºæ£æ¥ï¼å页å 容æåçå¯é æ å¨é¡µé¢ä¸å å ¥ã
ããå¨å®é æç« ç³»ç»å¤çè¿ç¨å½ä¸ï¼è¿æ许å¤é®é¢æå¾ èèï¼ä¸å¨æ页é¢ä¸åä¹å¤ï¼é注æçå°æ¹è¿æå¾å¤ãä½å¤§è´æè·¯å³æ¯å¦æ¤ï¼å ¶å®æ¹é¢å¯ä¸¾ä¸åä¸èå¾ã
ç¨PHPå¶ä½éæç½ç«ç模æ¿æ¡æ¶
ã模æ¿è½å¤æ¹åç½ç«çç»æãæ¬æéè¿°å¦ä½éè¿PHP 4çä¸ä¸ªæ°åè½å模æ¿ç±»ï¼å¨ç±å¤§ééæHTML页é¢ææçç½ç«ä¸å·§å¦å°è¿ç¨æ¨¡æ¿æ§å¶é¡µé¢å¸å±ã
æ纲ï¼
===================================
å离åè½åå¸å±
é¿å 页é¢å ç´ éå¤
éæç½ç«ç模æ¿æ¡æ¶
===================================
å离åè½åå¸å±
ãã é¦å æ们æ¥ççåºç¨æ¨¡æ¿ç两个主è¦ç®çï¼
å离åè½ï¼PHPï¼åå¸å±ï¼HTMLï¼
é¿å 页é¢å ç´ éå¤
ãã 第ä¸ä¸ªç®çæ¯è°è®ºå¾æå¤çç®çï¼å®è®¾æ³çæ å½¢æ¯ï¼ä¸ç»ç¨åºåç¼åç¨äºçæ页é¢å 容çPHPèæ¬ï¼åæ¶å¦ä¸ç»è®¾è®¡äººå设计HTMLåå¾å½¢ä»¥æ§å¶é¡µé¢çæç»å¤è§ãå离åè½åå¸å±çåºæ¬ææ³å°±æ¯ä½¿å¾è¿ä¸¤ç»äººè½å¤åèªç¼åå使ç¨ç¬ç«çä¸ç»æ件ï¼ç¨åºååªéå ³å¿é£äºåªå å«PHP代ç çæ件ï¼æ éå ³å¿é¡µé¢çå¤è§
ï¼è页é¢è®¾è®¡äººåå¯ä»¥ç¨èªå·±æçæçå¯è§åç¼è¾å¨è®¾è®¡é¡µé¢å¸å±ï¼æ éæ å¿ç ´åä»»ä½åµå ¥å°é¡µé¢çPHP代ç ã
ãã å¦æä½ æ¾ç»çè¿å ä¸ªå ³äºPHP模æ¿çæç¨ï¼é£ä¹ä½ åºè¯¥å·²ç»æç½æ¨¡æ¿çå·¥ä½æºå¶ãèèä¸ä¸ªç®åç页é¢å±é¨ï¼é¡µé¢çä¸æ¹æ¯é¡µå¤´ï¼å·¦è¾¹æ¯å¯¼èªæ¡ï¼å ¶ä½é¨åæ¯å 容åºåãè¿ç§ç½ç«å¯ä»¥æ¥æå¦ä¸æ¨¡æ¿æ件ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
{HEADER} | |
{LEFTNAV} | {CONTENT} |
Foo
Bar
ãã å¯ä»¥çåºé¡µé¢å¦ä½ç±è¿äºæ¨¡æ¿æé èæï¼main模æ¿æ§å¶çæ´ä¸ªé¡µé¢çå¸å±ï¼header模æ¿åleftnav模æ¿æ§å¶ç页é¢çå ¬å ±å ç´ ãè±æ¬å·â{}âéé¢çæ è¯ç¬¦æ¯å 容å ä½ç¬¦ã使ç¨æ¨¡æ¿æ主è¦ç好å¤å¨äºçé¢è®¾è®¡è è½å¤æç §èªå·±çææ¿ç¼è¾è¿äºæ件ï¼æ¯å¦è®¾ç½®åä½ãä¿®æ¹é¢è²åå¾å½¢ï¼æè å®å ¨å°æ¹å页é¢çå¸å±ãçé¢è®¾è®¡è å¯ä»¥ç¨ä»»ä½æ®éHTMLç¼è¾å¨æè å¯è§åå·¥å ·ç¼è¾è¿äºé¡µé¢ï¼å 为è¿äºæ件é½åªå å«HTML代ç ï¼æ²¡æä»»ä½PHP代ç ã
ã PHP代ç å ¨é¨ä¿åå°åç¬çæ件ä¸ï¼è¿ä¸ªæ件ä¹å°±æ¯ç±é¡µé¢URLå®é è°ç¨çæ件ãWebæå¡å¨éè¿PHPå¼æ解æ该æ件ï¼ç¶åæç»æè¿åç»æµè§å¨ãä¸è¬å°ï¼PHP代ç æ»æ¯å¨æå°çæ页é¢å 容ï¼æ¯å¦æ¥è¯¢æ°æ®åºæè æ§è¡æç§è®¡ç®çãä¸é¢æ¯ä¸ä¸ªä¾åï¼
å¤å¶ä»£ç 代ç å¦ä¸:
// example.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );
// æ¤å¤çPHP代ç 设置$contentä½¿å ¶å å«åéç页é¢å 容
$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
ãã è¿éæ们使ç¨çæ¯æµè¡çFastTemplate模æ¿ç±»ï¼ä½å ¶åºæ¬æ路对äºå ¶ä»è®¸å¤æ¨¡æ¿ç±»æ¥è¯´é½ä¸æ ·ãé¦å ä½ å®ä¾åä¸ä¸ªç±»ï¼åè¯å®å°åªéå»å¯»æ¾æ¨¡æ¿æ件以ååªä¸ä¸ªæ¨¡æ¿æ件ä¸é¡µé¢çåªé¨å对åºï¼æ¥ä¸æ¥æ¯çæ页é¢å 容ï¼æç»æèµäºå 容çæ è¯ç¬¦ï¼ç¶åï¼ä¾æ¬¡è§£æå个模æ¿æ件ï¼æ¨¡æ¿ç±»å°æ§è¡å¿ è¦çæ¿æ¢æä½ï¼æåæ解æç»æè¾åºå°æµè§å¨ã
ãã è¿ä¸ªæ件å®å ¨ç±PHP代ç ææï¼ä¸å å«ä»»ä½HTML代ç ï¼è¿æ¯å®æ大çä¼ç¹ãç°å¨ï¼PHPç¨åºåå¯ä»¥éä¸ç²¾åç¼åçæ页é¢å 容ç代ç ï¼èä¸å¿ 为äºå¦ä½çæHTMLå»æ£ç¡®å°æ ¼å¼åæç»é¡µé¢èæ å¿ã
ãã ä½ å¯ä»¥ä½¿ç¨è¿ç§æ¹æ³åä¸é¢çæ件æé åºä¸ä¸ªå®æ´çç½ç«ãå¦æPHP代ç æ¯ä»¥URLä¸çæ¥è¯¢å符串为åºç¡çæ页é¢å 容ï¼ä¾å¦http://www.foo.com/example.php?article=099ï¼ä½ å¯ä»¥æ®æ¤æé åºä¸ä¸ªå®æ´çæå¿ç½ç«ã
ãã å¾å®¹æçåºéç¨æ¨¡æ¿è¿æ第äºä¸ªå¥½å¤ãå¦ä¸ä¾æ示ï¼é¡µé¢å·¦è¾¹ç导èªæ¡åç¬ä¿å为ä¸ä¸ªæ件ï¼æ们åªéç¼è¾è¿ä¸ä¸ªæ¨¡æ¿æ件就å¯ä»¥æ¹åç½ç«ææ页é¢å·¦è¾¹ç导èªæ¡ã
é¿å 页é¢å ç´ éå¤
ãã âè¿ç¡®å®ä¸éâï¼ä½ ä¹è®¸ä¼æ³ï¼âæçç½ç«ä¸»è¦å°±æ¯ç±å¤§éçéæ页é¢ææãç°å¨æå¯ä»¥ä»ææ页é¢ä¸å é¤å®ä»¬çå ¬å ±é¨åï¼è¦æ´æ°è¿äºå ¬å ±é¨åå®å¨å¤ªéº»ç¦äºã以åæå°±å¯ä»¥ç¨æ¨¡æ¿å¶ä½åºå¾å®¹æç»´æ¤çç»ä¸é¡µé¢å¸å±ãâä½äºæ 并éè¿ä¹ç®åï¼â大éçéæ页é¢âéåºäºé®é¢çæå¨ã
ãã 请èèä¸é¢çä¾åãè¿ä¸ªä¾åå®é ä¸åªæä¸ä¸ªexample.php页é¢ï¼å®ä¹æ以è½å¤çææ´ä¸ªç½ç«çææ页é¢ï¼æ¯å 为å®å©ç¨äºURLä¸çæ¥è¯¢å符串ä»æ°æ®åºä¹ç±»çä¿¡æ¯æºå¨æå°æé åºé¡µé¢ã
ãã æ们ä¹ä¸ç大å¤æ°äººæè¿è¡çç½ç«å¹¶ä¸ä¸å®é½ææ°æ®åºæ¯æãæ们çç½ç«å¤§å¤æ°ç±éæ页é¢ææï¼ç¶åç¨PHPå¨è¿éãé£éå ä¸ä¸äºå¨æåè½ï¼æ¯å¦æç´¢å¼æãåé¦è¡¨åçãé£ä¹ï¼å¦ä½å¨è¿ç§ç½ç«ä¸åºç¨æ¨¡æ¿å¢ï¼
ãã æç®åçæ¹æ³æ¯ä¸ºæ¯ä¸ä¸ªé¡µé¢å¤å¶ä¸ä»½PHPæ件ï¼
ç¶åå¨æ¯ä¸ä¸ªé¡µé¢ä¸æPHP代ç é代表å 容çåé设置æåéç页é¢å 容ãä¾å¦ï¼å设æä¸ä¸ªé¡µé¢ï¼å®ä»¬åå«æ¯ä¸»é¡µï¼homeï¼ãå ³äºï¼aboutï¼å产åï¼productï¼ï¼æ们å¯ä»¥ç¨ä¸ä¸ªæ件åå«çæå®ä»¬ãè¿ä¸ä¸ªæ件çå 容é½ç±»å¦ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
// home.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );
$content = "
欢è¿è®¿é®
å¸æä½ è½å¤å欢æ¬ç½ç«
";$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
ãã æ¾ç¶ï¼è¿ç§æ¹æ³æä¸ä¸ªé®é¢ï¼æä»¬å¿ é¡»ä¸ºæ¯ä¸ä¸ªé¡µé¢å¤å¶è¿äºå¤æçãçµæ¶å°æ¨¡æ¿çPHP代ç ï¼è¿ä¸éå¤å ¬å ±é¡µé¢å ç´ ä¸æ ·ä½¿å¾é¡µé¢é¾ä»¥ç»´æ¤ï¼ç°å¨æ件åæ··åäºHTMLåPHP代ç ï¼ä¸ºå 容åéèµå¼å°åå¾é常å°é¾ï¼å 为æä»¬å¿ é¡»å¤ç好大éçç¹æ®å符ã
ãã 解å³è¿ä¸ªé®é¢çå ³é®å°±å¨äºå离PHP代ç åHTMLå 容ï¼è½ç¶æ们ä¸è½ä»æ件ä¸å é¤ææçHTMLå 容ï¼ä½å¯ä»¥ç§»åºç»å¤§å¤æ°PHP代ç ã
éæç½ç«ç模æ¿æ¡æ¶
ãã é¦å ï¼æ们象åé¢ä¸æ ·ä¸ºææç页é¢å ¬ç¨å ç´ ä»¥å页é¢æ´ä½å¸å±ç¼å模æ¿æ件ï¼ç¶åä»ææç页é¢å é¤å ¬å ±é¨åï¼åªçä¸é¡µé¢å 容ï¼æ¥ä¸æ¥åå¨æ¯ä¸ªé¡µé¢ä¸å ä¸ä¸è¡PHP代ç ï¼å¦ä¸æ示ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
ä½ å¥½
欢è¿è®¿é®
å¸æä½ è½å¤å欢æ¬ç½ç«
?>
ãã è¿ç§æ¹æ³åºæ¬ä¸è§£å³äºåé¢æå°çåç§é®é¢ãç°å¨æ件éåªæä¸è¡PHP代ç ï¼èä¸æ²¡æä»»ä½ä¸è¡ä»£ç ç´æ¥æ¶åå°æ¨¡æ¿ï¼å æ¤è¦æ¹å¨è¿äºä»£ç çå¯è½æ§æå°ãæ¤å¤ï¼ç±äºHTMLå 容ä½äºPHPæ è®°ä¹å¤ï¼æ以ä¹ä¸åå¨ç¹æ®å符çå¤çé®é¢ãæ们å¯ä»¥å¾å®¹æå°å°è¿ä¸è¡PHP代ç å å ¥å°ææéæHTML页é¢ä¸ã
ãã requireå½æ°å¼å ¥äºä¸ä¸ªPHPæ件ï¼è¿ä¸ªæ件å å«äºææå¿ éçä¸æ¨¡æ¿ç¸å ³çPHP代ç ãå ¶ä¸pageStartå½æ°è®¾ç½®æ¨¡æ¿å¯¹è±¡ä»¥å页é¢æ é¢ï¼pageFinishå½æ°è§£æ模æ¿ç¶åçæç»æåéç»æµè§å¨ã
ãã è¿æ¯å¦ä½å®ç°çå¢ï¼ä¸ºä»ä¹å¨è°ç¨pageFinishå½æ°ä¹åæ件ä¸çHTMLä¸ä¼åéç»æµè§å¨ï¼çæ¡å°±å¨äºPHP 4çä¸ä¸ªæ°åè½ï¼è¿ä¸ªåè½å 许æè¾åºå°æµè§å¨çå 容æªè·å°ç¼å²åºä¹ä¸ã让æ们æ¥ççprepend.phpçå ·ä½ä»£ç ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
require('class.FastTemplate.php');
function pageStart($title = '') {
GLOBAL $tpl;
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav'=> 'leftnav.htm' ) );
$tpl->assign('TITLE', $title);
ob_start();
}
function pageFinish() {
GLOBAL $tpl;
$content = ob_get_contents();
ob_end_clean();
$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
}
?>
ããpageStartå½æ°é¦å å建并设置äºä¸ä¸ªæ¨¡æ¿å®ä¾ï¼ç¶åå¯ç¨è¾åºç¼åãæ¤åï¼æææ¥èªé¡µé¢æ¬èº«çHTMLå 容é½å°è¿å ¥ç¼åãpageFinishå½æ°ååºç¼åä¸çå 容ï¼ç¶åå¨æ¨¡æ¿å¯¹è±¡ä¸æå®è¿äºå 容ï¼æå解æ模æ¿å¹¶è¾åºå®æåç页é¢ã
ãã è¿å°±æ¯æ´ä¸ªæ¨¡æ¿æ¡æ¶å ¨é¨çå·¥ä½è¿ç¨äºãé¦å ç¼åå å«äºç½ç«å个页é¢å ¬å ±å ç´ ç模æ¿ï¼ç¶åä»ææ页é¢ä¸å é¤å ¨é¨å ¬å ±ç页é¢å¸å±ä»£ç ï¼ä»£ä¹ä»¥ä¸è¡æ°¸è¿æ éæ¹å¨çPHP代ç ï¼åæFastTemplateç±»æ件åprepend.phpå å ¥å°å å«è·¯å¾ï¼è¿æ ·ä½ å°±å¾å°äºä¸ä¸ªé¡µé¢å¸å±å¯ä»¥éä¸æ§å¶çç½ç«ï¼å®æçæ´å¥½çå¯é æ§åå¯ç»´æ¤æ§ï¼èä¸ç½ç«çº§ç大èå´ä¿®æ¹ä¹åå¾ç¸å½å®¹æã
ãã æ¬æä¸è½½å å å«
äºä¸ä¸ªå¯è¿è¡ç示ä¾ç½ç«ï¼å®ç代ç 注éè¦æ¯åé¢ç代ç 注éæ´è¯¦ç»ä¸äºãFastTemplateç±»å¯ä»¥å¨http://www.thewebmasters.net/æ¾å°ï¼ææ°ççæ¬å·æ¯1.1.0ï¼é£éè¿æä¸ä¸ªç¨äºä¿è¯è¯¥ç±»å¨PHP 4ä¸æ£ç¡®è¿è¡çå°è¡¥ä¸ãæ¬æä¸è½½ä»£ç ä¸ç类已ç»ç»è¿è¯¥è¡¥ä¸çä¿®æ£ã
PHPç®æçæéæ页é¢
å¤å¶ä»£ç 代ç å¦ä¸:
/*
* æ件åï¼index.php
*/
require "conn.php";
$query = "select * from news order by datetime desc";
$result = mysql_query($query);
?>
æ é¢ | åå¸æ¶é´ |
">= $re["title"]?> | = $re["datetime"]?> |
æ·»å æ°é» |
å¤å¶ä»£ç 代ç å¦ä¸:
/*
æ件åï¼AddNews.php
ç®æå¨ææ·»å çæéææ°é»é¡µé¢
#
# 表çç»æ `news`
#
CREATE TABLE `news` (
`newsid` int(11) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
`datetime` datetime NOT NULL default '0000-00-00 00:00:00',
KEY `newsid` (`newsid`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;
*/
?>
ç¨PHPçæéæç½é¡µç两个å½æ°
å¨æè¿å å¹´ï¼ä¸ç»´ç½ï¼ä¹ç§°ç¯çä¿¡æ¯ç½ï¼å³WWWï¼ä¸ææ¹åä¿¡æ¯å¤çææ¯çé¢è²ãWEBå·²ç»å¿«éå°æ为ä¸ç§ææçåªä»ï¼å¹¶éå人们ååä¸æ²éååä½ãå ä¹ææçä¿¡æ¯ææ¯é¢åé½æ®éåå°WEBçå½±åãWeb访é®å¸¦æ¥æ´å¤ç¨æ·åæ´å¤æ°æ®ï¼è¿æå³çç»æå¡å¨åæ°æ®åºæ´å¤åååæç»ç¨æ·å¾å°è¶æ¥è¶æ ¢çååºé度ãä¸ä¸æé å¢å CPUï¼ç£ç驱å¨å¨åå åæ¥è·ä¸è¿ç§å¢é¿çéæ±ç¸æ¯ï¼ WEBå¨æç½é¡µé¢éæååºè¯¥æ¯ä¸ä¸ªæ´å®ç¨ï¼æ´ç»æµçéæ©ã
ç¨PHPå®ç°WEBå¨æç½é¡µéæåçå ·ä½å®ç°å½æ°å¦function gen_static_file()æ示
å¤å¶ä»£ç 代ç å¦ä¸:
function gen_static_file($program, $filename)
{
$program 1= "/usr/local/apache/htdocs/php/" . $program;
$filename1 = "/usr/local/apache/htdocs/ static_html/" . $filename;
$cmd_str = "/usr/local/php4/bin/php " . $program1 . " } " . $filename1 . " ";
system($cmd_str);
echo $filename . " generated.ãbrã";
}
è¿ä¸ªå½æ°æ¯å®ç°éæåçå ³é®ï¼å³PHPå¨æ页é¢ç¨åºä¸æ¯è¢«éå°æµè§å¨ä¸ï¼èæ¯è¾å ¥å°å为$filenameçæ件ä¸å»ï¼å¦å¾2ï¼ã两个åæ°ä¸$programæ¯PHPå¨æ页é¢ç¨åºï¼$filenameæ¯çæçéæ页é¢çååï¼å¯æ ¹æ®éè¦èªå·±å¶å®å½åè§åï¼è¿ä¸ç¹å¾éè¦ï¼è§ä¸æï¼ï¼/usr/local/php4/bin/phpæ¯PHPä¸å ·ææç¨åºè¾å ¥æ件åè½çé¨åï¼Systemæ¯PHPä¸æ§è¡å¤é¨å½ä»¤çå½æ°ãæ们è¿å¯ä»¥çåºææçæå¨æ页é¢çphpç¨åºéæ¾å¨/php/ç®å½ä¸ï¼æææ°äº§ççéæ页é¢åä¼åºç°å¨/static_html/ç®å½ä¸ï¼è¿äºè·¯å¾å¯ä»¥æ ¹æ®å ·ä½éè¦è®¾ç½®ï¼ã
ä¸é¢è®©æä»¬ä¸¾ä¸ªå ·ä½ä¾åï¼çä¸ä¸college_static.phpçéæ页é¢æ¯ææ ·çæçã
å¤å¶ä»£ç 代ç å¦ä¸:
function gen_college_static ()
{
for ($i = 0; $i ã= 32; $i++ã
{
putenv("province_id=" . $i); //*.phpæ件ä»æ°æ®åºåæ°æ®æ¶è¦ç¨å°ã
$filename = " college_static". $i . ".html";
gen_static_file("college_static.php", $filename);
}
ä»è¿ä¸ªå½æ°æ们å¯ä»¥çå°éè¿è°ç¨å½æ°gen_static_fileï¼ï¼, college_static.phpç»è¿éæåï¼åæäº33个éæ页é¢college.static0.html~college.static33.htmlï¼å ¶ä¸$filenameä¼éç$Içååèååãå½ç¶ä¹å¯ä»¥ä»æ°æ®åºä¸ç´æ¥åå¼ï¼æ¥æ§å¶çæçéæ页é¢ç个æ°åååï¼å ¶ä»ç¨åºå¯¹çæçéæ页é¢çè°ç¨åºåéæ页é¢çå½åè§åä¸è´ã
推荐阅读
-
php无刷新利用iframe实现页面无刷新上传文件(1/2)_PHP教程
-
程序员 - 为什么我把Apache和PHP都安装到了D磁盘,在phpinfo()里面却发现配置文件在c:windows 里面
-
反射调用private方法实践(php、java)_php实例
-
关于PHP的多图下传和处理,麻烦高手写一段代码供研习
-
Php获取金书网的书名的实现代码
-
php ci框架验证码实例分析_PHP
-
æ¯è¾è¯¦ç»PHPçæéæ页é¢æç¨_PHPæç¨
-
phpMyAdmin 缺少 mysqli 扩展。请检查 PHP 配置 的解决方案_PHP教程
-
PHP中使用mktime获取时间戳的一个黑色幽默分析_PHP教程
-
php读xml