phpææ¯çæéæ页é¢çå®ç°
ä¸ï¼PHPèæ¬ä¸å¨æ页é¢ã
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
åï¼åæï¼æç« çæï¼é½å¨æ¤å¾ªç¯ä¸æ§è¡ãä¸åçæ¯ï¼die ("å建æ件".$filename."æåï¼";è¿å¥å»æ
ï¼æ¾å°å¾ªç¯åçæ¾ç¤ºï¼å 为该è¯å¥å°ä¸æ¢ç¨åºæ§è¡ãä¾ï¼
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;$i
if ($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} |
å¯ä»¥çåºé¡µé¢å¦ä½ç±è¿äºæ¨¡æ¿æé èæï¼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 ;
*/
if(isset($_POST["title"])){
$title = $_POST["title"];
$content = $_POST["content"];
//å®ä¹æ¨¡çæ件çå 容ï¼å¯ç¨å ¶å®æ¹æ³
$filecontent = '
{title} |
{content} |
{datetime} |
';
$datetime = date("Y-m-d H:i:s");
$query = "insert into news values('','".$title."','".$content."','".$datetime."')";
require "conn.php";//è¿æ¥æ°æ®åº
$result = mysql_query($query) or die(mysql_error());
if($result){
$id = mysql_insert_id();
$filename = $id.".html";
if($fp = fopen($filename, "w")){//æ°å»ºæ件ï¼æååæ·»å å 容
$filecontent = str_replace("{title}", $title, $filecontent);
$filecontent = str_replace("{content}", $content, $filecontent);
$filecontent = str_replace("{datetime}", $datetime, $filecontent);
if(!fwrite($fp, $filecontent)){//æå 容åå ¥æ件
$query = "delete from news where newsid=".$id;
$result = mysql_query($query) or die(mysql_error());
fclose($fp);
unlink($filename);
echo "
failed!');location.href='index.php';";
exit;
}else {
echo "
successed!');location.href='index.php';";
}
}
}
}
?>
PHPçæéæ页é¢è¯¦è§£
åºç¨å®ä¾ï¼
http://xjtusky.net/down
说æï¼æä¸é¨ååè½è¿æ²¡æå®å ¨å好ï¼ä¿®æ¹ä¸ï¼æå ´è¶£çæåå¯ä»¥ççä¸é¢è¿ç¯æç« ï¼ç
å®äºä¸å®è¦è¯´å¥½ï¼çæ页é¢çé度快çï½ï½ï½åµåµãä¸é¢æç« ä¸ä¸äºå°æ¹è¯·èªè¡ä¿®æ¹ï¼
PHPçæéæ页é¢è¯¦è§£
http://xjtusky.net/article/htmld ... /article_594_1.html
çå°å¾å¤æåå¨å个å°æ¹åå¸é®PHPçæéææç« ç³»ç»çæ¹æ³ï¼ä»¥åæ¾åè¿è¿æ ·ä¸ä¸ªç³»
ç»ï¼éè°äºçæ³ï¼ä»¥ä¾åä½åèã好äºï¼æ们å å顾ä¸äºåºæ¬çæ¦å¿µã
ä¸ï¼PHPèæ¬ä¸å¨æ页é¢ã
PHPèæ¬æ¯ä¸ç§æå¡å¨ç«¯èæ¬ç¨åºï¼å¯éè¿åµå ¥çæ¹æ³ä¸HTMLæ件混åï¼ä¹å¯ä»¥ç±»ï¼å½
æ°å°è£ çå½¢å¼ï¼ä»¥æ¨¡æ¿çæ¹å¼å¯¹ç¨æ·è¯·æ±è¿è¡å¤çãæ 论以ä½ç§æ¹å¼ï¼å®çåºæ¬åçæ¯è¿
æ ·çãç±å®¢æ·ç«¯æåºè¯·æ±ï¼è¯·æ±æä¸é¡µé¢ã----->ãWEBæå¡å¨å¼å ¥æå®ç¸åºèæ¬è¿è¡å¤ç
----->ãèæ¬è¢«è½½å ¥æå¡å¨ã----->ãç±æå¡å¨æå®çPHP解æå¨å¯¹èæ¬è¿è¡è§£æå½¢æH
TMLè¯è¨å½¢å¼ã---->ãå°è§£æåçHTMLè¯å¥ä»¥å çæ¹å¼ä¼ åç»æµè§å¨ãç±æ¤ä¸é¾çåºï¼å¨
页é¢åéå°æµè§å¨åï¼PHPå°±ä¸åå¨äºï¼å·²è¢«è½¬å解æ为HTMLè¯å¥ã客æ·è¯·æ±ä¸ºä¸å¨ææ件
ï¼äºå®ä¸å¹¶æ²¡æçæ£çæ件åå¨å¨é£éï¼æ¯PHP解æèæç¸å¯¹åºç页é¢ï¼ç¶ååéåæµè§å¨
ãè¿ç§é¡µé¢å¤çæ¹å¼è¢«ç§°ä¸º"å¨æ页é¢"ã
äºï¼éæ页é¢ã
éæ页é¢æ¯æå¨æå¡å¨ç«¯ç¡®å®åå¨çä» å«HTML以åJSï¼CSSç客æ·ç«¯è¿è¡èæ¬ç页é¢ã
å®çå¤çæ¹å¼æ¯ãç±å®¢æ·ç«¯æåºè¯·æ±ï¼è¯·æ±æä¸é¡µé¢ã---->ãWEBæå¡å¨ç¡®è®¤å¹¶è½½å ¥æä¸
页é¢ã---->ãWEBæå¡å¨å°è¯¥é¡µé¢ä»¥å çå½¢å¼ä¼ éåæµè§å¨ãç±è¿ä¸è¿ç¨ï¼æ们对æ¯ä¸ä¸
å¨æ页é¢ï¼å³å¯æ¹ç°ãå¨æ页é¢éç±WEBæå¡å¨çPHP解æå¨è¿è¡è§£æï¼èä¸é常è¿éè¿æ¥
æ°æ®åºï¼è¿è¡æ°æ®åºååæä½ï¼ç¶åæè½å½¢æHTMLè¯è¨ä¿¡æ¯å ï¼èéæ页é¢ï¼æ 须解æï¼
æ é¡»è¿æ¥æ°æ®åºï¼ç´æ¥åéï¼å¯å¤§å¤§åè½»æå¡å¨ååï¼æé«æå¡å¨è´è½½è½åï¼å¤§å¹ æä¾é¡µ
é¢æå¼é度åç½ç«æ´ä½æå¼é度ãä½å ¶ç¼ºç¹æ¯ï¼ä¸è½å¨æå°å¯¹è¯·æ±è¿è¡å¤çï¼æå¡å¨ä¸å¿
须确å®åå¨è¯¥æ件ã
ä¸ï¼æ¨¡æ¿å模æ¿è§£æã
模æ¿å³å°æªå¡«å å 容htmlæ件ãä¾å¦ï¼
Code: [ç¹å»è¿éæ以ä¸å 容å¤å¶å°åªè´´æ¿]
temp.html
this is a {file} file's templets
PHPå¤çï¼
templetest.php
$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
lpages; $i++)ï¼é¡µé¢å ç´ è·åï¼åæï¼æç« çæï¼é½å¨æ¤å¾ªç¯ä¸æ§è¡ãä¸åçæ¯ï¼die (
"å建æ件".$filename."æåï¼";è¿å¥å»æï¼æ¾å°å¾ªç¯åçæ¾ç¤ºï¼å 为该è¯å¥å°ä¸æ¢ç¨åº
æ§è¡ãä¾ï¼
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;$i
if ($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 .= ''.$titl
e.'
';
}
$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 ("çæå页æ件å®æï¼å¦çæä¸å®å ¨ï¼è¯·æ£æ¥æ件æéç³»ç»åéæ°çæï¼");
?>
大è´æè·¯å¦æ¤ï¼å ¶ä¸å¦å ¶å®æ°æ®çæï¼æ°æ®è¾å ¥è¾åºæ£æ¥ï¼å页å 容æåçå¯é æ å¨
页é¢ä¸å å ¥ã
å¨å®é æç« ç³»ç»å¤çè¿ç¨å½ä¸ï¼è¿æ许å¤é®é¢æå¾ èèï¼ä¸å¨æ页é¢ä¸åä¹å¤ï¼é注
æçå°æ¹è¿æå¾å¤ãä½å¤§è´æè·¯å³æ¯å¦æ¤ï¼å ¶å®æ¹é¢å¯ä¸¾ä¸åä¸èå¾ã
ç®åç½ç»ä¸å¥½å¤ç½ç«çæ°é»åå¸ç³»ç»é½éç¨äº å¨ææå¡å¨ææ¯çæéæHTMLçåæ³ï¼è¿æ ·åç好å¤æ¯ï¼ä¸æ¯è½åè½»å ¶æå¡å¨çè´æ ï¼äºæ¯å 为çæäºHTMLéæ页é¢ï¼æä»¥å ¶ç½ç«è¢«æç´¢å¼ææç´¢å°çæºç æ´å¤§ä¸äºãç¬è çç½ç«æ¾ç»ä½¿ç¨PHPè¿ä¸å¨æææ¯æ¥æ建æ°é»åå¸ç³»ç»ï¼å ¶åçä¹å°±æ¯åºç¨äºPHPçæHTMLéæ页é¢çææ¯ï¼ç¸å ³å¹³å°æ¯ Windows XP Sp2+php4.32+mysqlï¼å æ¤ï¼å¨è¿éï¼æ³ç®åå°è°ä¸ä¸è¿ç§åæ³çæè·¯ãè¿ç¯æç« éåäºå¯¹PHP+MYSQLæ°æ®åºæä½ï¼SQLè¯å¥ä»¥åç½é¡µ 设计æç¹åºç¡çæåï¼å¦ææ¨æ¯ä¸ä¸ªä»å¤´å¼å§å¦çæåï¼é£ä¹è¯·å æ好åºç¡å§ï¼å°è¿éå°±ä¸ç¨å¾ä¸çäºãå¦ææ¨é½ç¬¦åä¸è¿°æ¡ä»¶çè¯ï¼é£ä¹æåæ¨ï¼è¯·æ¥çå¾ä¸çã ä½æ¯ï¼å¨å ·ä½å¨ææ建ä¹åï¼æ¨è¿è¦å好以ä¸å ç¹åå¤å·¥ä½ã
ä¸ã å ·å¤æ¬å°è°è¯PHPçåè½
å¨WINDOWS XPæä½ç³»ç»ä¸ï¼ç¬è å»ºè®®ä½ å¯ä»¥å°ç½ä¸ä¸è½½ä¸ä¸ªPHP+MYSQL+APHCHEçæå¡å¨å¥è£ ï¼å¦åå软件åï¼å»é£éæç´¢ä¸ä¸å°±å¯ä»¥ä¸è½½å°ãä¸è½½å®åé»è®¤ å®è£ å³å¯ï¼è¿æ ·æ¨å°±å ·å¤äºå¨æ¬å°æµè¯PHPçåè½äºï¼çå»äºå¾å¤æå¨é ç½®ç麻ç¦ï¼æä¹æ ·ï¼ç®åå§ï¼OKï¼è¿ä» ä» åªæ¯ç¬¬ä¸æ¥ã
äºã æææ°é»åå¸ç³»ç»æå ·å¤çåè½
é¦é¡µçæ°é»åå¸å¾å¾æ¯éè¿åå°æ´æ°çï¼åå°çæ´æ°æ éæ¯ç±æ·»å ãç¼è¾ãå é¤æ°æ®çåºæ¬çåè½æå®ç°çãå¨è¿éï¼ä½ å¯ä»¥ä½¿ç¨ç½é¡µè®¾è®¡è½¯ä»¶æ¥æå»ºä½ çæ³ è¦çåå°çé¢ï¼å ¶åè½çå®ç°å½ç¶æ¯è¦ç¨å°PHPå½ãè¿ä¸æ¥å»ºè®®æ¨å æ³å¥½æ°é»åå¸ç³»ç»è¦æçåè½ãå¨è¿éï¼å¦ä½ç¨PHPæ·»å ãç¼è¾ãå é¤æ°æ®å°±ä¸åéå¤ï¼å 为éç¹æ¯å¦ä½å¨è¿åºç¡ä¸çæéæææ¯ã
ä¸ã PHPçæHTMLçææ¯åçã
ååãè´¹è¯è¯´äºé£ä¹å¤ï¼ç»äºå°äºè¯¥è®²çå°æ¹äºãå ¶å®ï¼è¿ä¸åç并ä¸å¤æï¼ç»¼åå°æ¥è®²çè¯ï¼åºè¯¥æ¯PHPä¸ä¸ä¸ªæ¿æ¢æ°æ®è¯æ³çåºç¨ãOKï¼è®²ä¸ä¸ªç®å èä¾å§ï¼ä¸æ¥ä¸æ¥å°åæï¼ç¸ä¿¡èªæçä½ è½çå¾æç½çå¯ï¼ä»ç»ç好æ¯ä¸æ¥å³å¯ï¼å¨è¿éï¼åªæ¯å¼å¯¼å¤§å®¶å¦ä½åèå·²ï¼å ·ä½çå¯ä»¥å®è·µä¸ä¸ï¼
ï¼1ï¼å¨MYSQLéæ°å»ºä¸æ°æ®åºï¼å½å为 database ï¼å¯èªå®ä¹ï¼ï¼æ°å»ºä¸è¡¨ï¼å½å为 newsï¼å 为æ¯æ°é»åå¸åï¼å个好记çååå³å¯ï¼å¯èªå®ä¹ï¼ï¼ç¶å建ç«è¿å 个å段åï¼
id (èªå¨éå¢ï¼è¿æ¯å ³é®ï¼ç±»åï¼INT)
title (顾åæä¹ï¼æ°é»æ é¢ï¼ç±»åå¯å TEXT)
content (æ°é»å 容ï¼ç±»åå¯å TEXT)
path (HTMLæ件路å¾ï¼ç±»åå¯å TEXT)
ï¼2ï¼å»ºç« conn.php
è¿æ¯è¿æ¥æ°æ®åºçPHPæ件ï¼ä½ å¯ä»¥æè¿æ¥æ°æ®çè¯å¥åç¬æ¾å¨è¿ä¸æ件éï¼ä»¥åå¤ä¸ªéè¦è¿æ¥æ°æ®åºçæ件ç´æ¥å¼ç¨è¿ä¸ªæ件å³å¯ã
ï¼3ï¼è®¾è®¡æ·»å æ°é»çè¡¨æ ¼ add.form ç®åçæºä»£ç å¦ä¸:
ï¼4ï¼å»ºç«ä¸ä¸ª HTML ç模æ¿,å¦å为model.htm,å add.phpå¯ä»¥å¨åä¸ç®å½ä¸ã
示ä¾æºä»£ç :
æ¤æ°é»çæ é¢:{title}
æ¤æ°é»çå 容:{content}
{ }大æ¬å·å çå 容å³æ¯è¦è¢«æ¿æ¢çå 容ï¼æ´ä¸ªéæ模æ¿ç设计å¯ä»¥æ ¹æ®èªå·±çæè·¯ï¼ä½{ }å 被æ¿æ¢çå å®¹å¿ é¡»å å«å¨å ï¼å¦ä¸é¢ç{title}ï¼{content}ï¼åå~ç®åå°è¯´ï¼è®¾è®¡å¥½ä¸ä¸ªå¾å¥½ççæ°é»æ¨¡æ¿åï¼æè¦è¢«æ¿æ¢çå¦ {title},{content}çæ è®°æ¾å°éè¦çå°æ¹å°±å¯ä»¥äºæã
ï¼5ï¼è¯¦è§£ add.php æºç
require_once("conn.php")ï¼ //å¼ç¨conn.phpï¼è¿æ¥æ°æ®åº
$title=$_POST["title"];
$content=$_POST["content"]; //è·å¾è¡¨ååé
//以ä¸å»ºç«ä¸ææ¬ææ¡£ï¼å ¶å¼èªå¨è®¡æ°
$countfile="count.txt";
if(!file_exists($countfile))
{
fopen($countfile,"w"); //å¦ææ¤æ件ä¸åå¨ï¼åèªå¨å»ºç«ä¸ä¸ª
}
$fp=fopen($countfile,"r");
$num=fgets($fp,20);
$num=$num+1; //æ¯æ¬¡å ¶å¼èªå¨å ä¸
fclose($fp);
$fp=fopen($countfile,"w");
fwrite($fp,$num); //æ´æ°å ¶å¼
fclose($fp);
//å©ç¨ä¸é¢èªå¨è®¡æ°çå¼è·å¾HTMLçè·¯å¾$path
$houzui=".html";
$path=$num.$houzui;
//è¿æ ·å½¢æçè·¯å¾æ¯èªå¨å¢é¿çï¼å¦1.html,2.html,3.htmlâ¦â¦â¦.æ·»å ä¸æ¡æ°é»ä¾¿èªå¨å ä¸1
//以ä¸ç¨SQLè¯å¥æ·»å æ°æ®è³è¡¨ news
$sql="insert into news (title,content,path) values (â".$title."â,â".$content."â,â".$path."â)";
$query=mysql_query($sql);
//以ä¸ä¸ºå ³é®ä¹å¤ï¼æä»è¡¨åè·å¾çæ°æ®æ¿æ¢æ¨¡æ¿ä¸ç{title},{content}æ è®° $fp=fopen("model.htm","r") //åªè¯»æå¼æ¨¡æ¿
$str=fread($fp,filesize("mode.htm"));//读å模æ¿ä¸å 容
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content}",$content,$str);//æ¿æ¢å 容
fclose($fp);
$handle=fopen($path,"w"); //åå ¥æ¹å¼æå¼æ°é»è·¯å¾
fwrite($handle,$str); //æåææ¿æ¢çå 容åè¿çæçHTMLæ件
fclose($handle);
//æ¶å°¾å·¥ä½:
echo "æ¥çåææ·»å çæ°é»";
?>
OK,æ´ä¸ªçæHTMLç示ä¾æºç å°±å°è¿éï¼å ¶å ³é®æ¯ç¨äºæ¿æ¢çæ¹æ³ã
$str=str_replace("{被æ¿æ¢çå 容}",$æ¿æ¢çå 容,$str);
å æ¤ï¼æ»ç»ä¸ä¸ä»¥ä¸çåæ³ï¼å 设计好æ°é»æ¨¡æ¿ï¼æéè¦è¢«æ¿æ¢çå 容ç¨{ }æ¾å°æ¨¡æ¿ä¸ç¸åºçä½ç½®ï¼ç¶å设计表åï¼åæ¯æåç表åå¤çç¨åºï¼æä»è¡¨åä¸è·åçåéæ¿æ¢æ¨¡æ¿ä¸ç¸åºçå 容å³å¯ï¼è¿æ ·æ¯æ¬¡é½ä¼çæä¸åçHTMLï¼å¦æ éè¦ä¿®æ¹HTMLçå 容ä¹æ¯ä¸æ ·çï¼è·å¾ä¿®æ¹åç表åå 容åï¼å ç¨ update è¯å¥æ´æ°æ°æ®åºï¼åéæ°æ¿æ¢ä¸ä¸æ¨¡æ¿ä¸çå 容å³å¯ï¼å é¤çè¯ï¼å delete表ä¸è¦å é¤çå 容ï¼åç¨unlink($path) æ¥å é¤HTMLçç©çæ件å³å¯ã
æ¬æå°åï¼
转载éæï¼ä½è¯·éä¸æç« å°å:-)