The idea of making news system using PHP

Source: Internet
Author: User
We can use the news system to store news, we can add to the news, delete, and so on, so as to reduce the workload of everyone, why not experiment.

First, create a table.
CREATE TABLE News (
n_id Int (255) NOT NULL auto_increment,
N_date datetime NOT NULL,
News text is not NULL,
PRimary Key (n_id)
);

The second step, set your login information
$database _user_name= "root";
$database _password= "";
$database _name= "News";
$time _offset= "0";

The third step, let's use the things behind the program as a function, save space Ah!

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;

}
Fourth step, let's consider how to show the news


Library of functions already defined above ...
The definition of a table ...
$title ";
if ($admin ==1)
{

echo "Admin";
}

echo "-News

";


$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 n EWS ORDER BY date Desc ";

}
Else
{

$link = "All News";


$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));


$show
$cleannews



";
}
echo "

";




}
if ($admin ==1)
{
echo "
Return
";
}

if ($link)
{
echo "

$link ";

}
Description: Omitted the format of the label, so it looks clear, you have to add AH

The above describes the use of PHP to create a news system, including the content of the news system, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.