PHP技巧:分析利用PHP製作新聞系統的步驟

來源:互聯網
上載者:User

我們可以用新聞系統來儲存新聞,我們可以對新聞進行添加、刪除等操作,這樣減少了大家的工作量,為什麼不實驗一下。

首先,建立一個表。

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>";
}

說明:省略了格式方面的標籤,這樣看起來思路清晰些,你可要加上啊



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.