這裡我的例子是利用MySQL數據庫的方式制作的,代碼如下:
一:發布新聞文件pub.php和pub_ok.php,代碼如下:
文件pub.php:
<form name="form1" method="post" action="pub_ok.php" onsubmit="javascript:return Juge(this);" >
<table width="90%" border="0" cellspacing="1" cellpadding="1" align="center">
<tr> <td> 標題: </td></tr>
<tr> <td> <input type="text" name="title" size="80"> </td> </tr>
<tr> <td> 詳細內容:</td></tr>
<tr> <td> <textarea name="detail" style="width=80%;height=160px;">>/textarea<
</td></tr>
<tr><td><input type="submit" name="pub" value="發布"></td></tr>
</table>
</form>
文件pub.php:
<?php include "common_news.php"; ?>
<html>
<head>
<title>新聞發布</title>
</head>
<body bgcolor=#cccccc><?php
if ($pub){ $title=htmlspecialchars($title);
$detail=htmlspecialchars($detail);
$detail= str_replace ( "
","<br>", $detail); $pub_time=date("Y")."-".date("m")."-".date("d")." ".date("H").":".date("i").":".date("s");
$query="insert into news (title,detail,pub_time) values ";
$query.="('$title','$detail','$pub_time')";
$result=mysql_query($query,$db);
echo "<div align='center'>";
if ($result){
echo "<p>發布成功!<a href='view.php?page=1'>查看</a>、<a href='pub.php'>發布</a></p>";
}
else{
echo mysql_error();
echo "<p>發布失敗!</p>";
}
echo "</div>";
}
?>
</body>
</html> 說明:上面用了一個包含文件common_news.php,其中$page_size變量為一頁最多顯示的新聞數,其余的大家應該一看就明白。<?php
$id=mysql_connect('localhost','root','cgreen');
$db=mysql_select_db('db_news',$id);
$page_size=5;
?>
顯示新聞條目文件:view.php
<?php include "common_news.php"; ?><html>
<head>
<title>新聞發布</title>
<SCRIPT language=JavaScript>
function view(Url)
{
popup=window.open(Url,"Displaywindow","left=30,top=30,width=600,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes, resizable=no");
}
</SCRIPT>
</head><body bgcolor=#cccccc>
<center>
新聞列表
</center>
<?php
if ($page){ $query="select count(*) as total from news";
$result=mysql_query($query,$db);
$message_count=mysql_result($result,0,"total");
$page_count=ceil($message_count/$page_size);
$offset=($page-1)*$page_size;
$query="select * from news order by id desc limit $offset, $page_size";
$result=mysql_query($query,$db);
if($result){
$rows=mysql_num_rows($result);
$i=1;
?>
<?php
while($myrow=mysql_fetch_array($result)){
?>
<table width="422" border="0" cellspacing="1" cellpadding="1" align="center">
<tr>
<td width="277"><li><a href="javascript:view('view_d.php?recid=<?php echo $myrow[id] ?>');"><?php echo "$myrow[title]</a>($myrow[pub_time])"; ?></td>
</tr>
</table>
<?php
$i++;
if ($i<=$rows){
echo "";
}
}
?>
<?php
$prev_page=$page-1;
$next_page=$page+1;
?>
<center>
<?php
if ($page<=1){
echo "第一頁";
}
else{
echo "<a href='$PATH_INFO?page=1'>第一頁</a>";
}
echo " ";
if ($prev_page<1){
echo "上一頁";
}
else{
echo "<a href='$PATH_INFO?page=$prev_page'>上一頁</a>";
}
echo " ";
if ($next_page>$page_count){
echo "下一頁";
}
else{
echo "<a href='$PATH_INFO?page=$next_page'>下一頁</a>";
}
echo " ";
if ($page>=$page_count){
echo "最後一頁";
}
else{
echo "<a href='$PATH_INFO?page=$page_count'>最後一頁</a>";
}
}
else{
echo "<p align='center'>現在還沒有新聞!/p>";
}
}
?>
<p> </p>
</body>
</html>