«
分析利用PHP制作新闻系统的步骤

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  我们可以用新闻系统来存储新闻,我们可以对新闻进行添加、删除等操作,这样减少了大家的工作量,为什么不实验一下。</p><p>  首先,建立一个表。</p><code>create table news (<br />n_id int(255) not null auto_increment,<br />n_date datetime not null,<br />news text not null,<br />primary key(n_id)<br />);</code><p>  第二步,设置你的登陆信息</p><code>$database_user_name="root";<br />$database_password="";<br />$database_name="news";<br />$time_offset="0";</code><p>  第三步,让我们把后面程序用到的东西作成函数,节省篇幅啊!</p><code>function connect_db()<br />{<br />// connects to the database<br />global $database_user_name, $database_password;<br />$db=mysql_connect("localhost",$database_user_name,$database_password);<br />return $db;<br />}<br />function db_name()<br />{<br />// returns the name of the database<br />global $database_name;<br />$db_name=$database_name;<br />return $db_name;<br />}<br />function get_now()<br />{<br />// gets current date and time<br />$db=connect_db();<br />$db_name=db_name();<br />mysql_select_db($db_name,$db);<br />$sql="select now() as now";<br />$result=mysql_query($sql,$db);<br />$myrow=mysql_fetch_array($result);<br />$now=$myrow["now"];<br />return $now;<br />}</code><p>  第四步,让我们考虑如何将新闻显示出来</p><code>//上面已经定义的函数库...<br />//表格的定义......<br /><center><FONT SIZE=5 FACE="'georgia',verdana,Arial,Lucida Sans,Gill Sans">$title ";<br />if ($admin==1)<br />{<br />echo " Admin ";<br />}<br />echo " - News</font> </td></tr> ";<br />$db=connect_db();<br />$db_name=db_name();<br />mysql_select_db($db_name,$db);<br />if ($show_news_sub)<br />{<br />$show_news = $show_news_sub;<br />}<br />if (!ereg("([0-9]{".strlen($show_news)."})",$show_news))<br />{<br />$show_news="ALL";<br />}<br />if (strtoupper($show_news)=="ALL")<br />{<br />$sql="select date_format(n_date,'%m/%d/%Y') as n_date, date_format(n_date,'%H:%i') as n_time ,news , n_date as date from news order by date desc";<br />}<br />else<br />{<br />$link="<a href=$REQUEST_URL?show_news_sub=ALL><FONT SIZE=-2 FACE="'georgia',verdana,Arial,Lucida Sans,Gill Sans">All news</font></a>";<br />$sql="select date_format(n_date,'%m/%d/%Y') as n_date, date_format(n_date,'%H:%i') as n_time , news , n_date as date from news order by n_date desc limit $show_news";<br />}<br />$result=mysql_query($sql,$db);<br />$num_rows=mysql_num_rows($result);<br />if($num_rows!=0)<br />{<br />echo " ";<br />while($myrow=mysql_fetch_array($result))<br />{<br />$date=$myrow["n_date"];<br />$time=$myrow["n_time"];<br />$news=$myrow["news"];<br />$n_id=$myrow["n_id"];<br />if (strtoupper($show_date)=="Y" && strtoupper($show_time)=="Y")<br />{<br />$show=$date." ".$time;<br />}<br />else if (strtoupper($show_date)=="Y" && strtoupper($show_time)!="Y")<br />{<br />$show=$date;<br />}<br />if (strtoupper($show_date)!="Y" && strtoupper($show_time)=="Y")<br />{<br />$show=$time;<br />}<br />$cleannews=(nl2br($news));<br /><TD align=left><FONT SIZE=-2 FACE="'verdana',Arial,Lucida Sans,Gill Sans">$show<br>$cleannews</font</TD><br />";<br />}<br />echo " ";<br />}<br />if ($admin==1)<br />{<br />echo "<center><a href=admin/index.php><FONT SIZE=-2 FACE="'verdana',Arial,Lucida Sans,Gill Sans">返回</font></a></center>";<br />}<br />if ($link)<br />{<br />echo "<center><br><br>$link</center>";<br />}</code><p>  说明:省略了格式方面的标签,这样看起来思路清晰些,你可要加上啊</p>