Idea of using PHP to create a news system

Source: Internet
Author: User
We can use a news system to store News. we can add or delete News, which reduces the workload of everyone. First, create a table. Createtablenews (n_idint (255) notnullauto_increment, n_datedatetimenotnull, newstextnotnull, primarykey (n_id we can use the news system to store news, and we can add or delete news, this reduces your workload and does not allow you to experiment.

First, create a table.
Create table news (
N_id int (255) not null auto_increment,
N_date datetime not null,
News text not null,
Primary key (n_id)
);

Step 2: Set your login information
$ Database_user_name = "root ";
$ Database_password = "";
$ Database_name = "news ";
$ Time_offset = "0 ";

Step 3: Let's turn the things used by the subsequent programs into functions, saving space!

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;

}
Step 4: Let's consider how to display news


// The function library defined above...
// Table definition ......
<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"> Return </font> </a> </center>
";
}

If ($ link)
{
Echo "<center> <br> $ link </center> ";

}
Note: The format label is omitted, so it looks clearer. you can add it.

[This article is copyrighted by the authors xgdzy and Osso. if you need to reprint it, please indicate the author and its source]

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.