简易新闻栏目

  这里是利用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);
     
$detailstr_replace "\n","<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>
显示详细新闻文件view_d.php,其内容如下,:
 ?php  include "common_news.php"; ?>
 <html>
 <head>
 <title>新闻详细内容</title>
 <body bgcolor=#cccccc>
 <center>新闻详细内容</center>
 <?php
 
if ($recid){
     
$query="select * from  news where id=".$recid;
     
$result=mysql_query($query,$db);
     if(
$result){
      
$title=mysql_result($result,0,'title');
      
$detail=mysql_result($result,0,'detail');
      echo 
"新闻标题:".$title."<br>";
      echo 
"新闻内容:<br>".$detail;
   }
    else
    {
    echo 
"该新闻可能被删除了!" ;}
 }
 
?>
 <br><p></p>
 <br><p></p>
 <center><a href="javascript:window.close();">关闭窗口</a></center>
 </body>
 </html>
 
对了,还有表news的结构,如下:
 id int auto_increment  用来自动编号
 pub_time datetime      记录发布时间  
 title   varchar(80)    新闻标题
 detail text            新闻详细内容 
 primary key(id)