Idea of using PHP to create a news system

Source: Internet
Author: User
The idea of using PHP to create a news system, and reading the idea of using PHP to create a news system, we can use a 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. Createtablenews (n_idint (255) notnullauto_incremen "> <LIN: we can use a news system to store News. we can add or delete News, which reduces the workload, 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 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 ......
$ 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 news 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 ";

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


[This article is copyrighted by the author and osuo. 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.