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