PHP Real-Life News management system to use the bootstrap framework

Source: Internet
Author: User

Just contacted PHP modeled a video on a news management system which also used the bootstrap framework

Write down and tidy up your thoughts.

This is a very simple system, the first is to build a database table.

Mysql>create Database Newsdb

Mysql> CREATE TABLE News (
-ID int unsigned NOT NULL Auto_increment primary key,//This is the ID of the news
Title varchar (+) Not null,//This is the headline of the News
-keywords varchar (+) Not null,//This is the news key word
Author varchar (+) Not null,//This is the author of the News.
-Addtime int unsigned not null,//This is the time to add news
Content text not NULL);//This is the contents of the news

In this way, the database table is built and the page is written below.

First wrote a database configuration file dbconfig.php:

<?php

Define (host, "localhost");//Host Name

Define (user, "root");//username

Define (PASS, "");//password

Define (DBNAME, "newsdb");//Database name

?>

Then there is a menu.php file

<! DOCTYPE html>
<meta charset= "Utf-8" >
<meta name= "viewport" content= "width=device-width,initial-scale=1.0" >
<title>HTML5&BootStrap</title>
<link href= "Bootstrap-3.2.0-dist/css/bootstrap.css" rel= "stylesheet" >
<!--[If ie]>
<script src= "Http://html5shiv.googlecode.com/svn/trunk/html5.js" ></script>
<! [endif]-->
<link href= "Prettify-4-mar-2013/prettify.css" rel= "stylesheet" >
<link href= "Style.css" rel= "stylesheet" >
<body onload= "Prettyprint ()" >
<style>
Body{background:orange;}
@media (max-width:997px) {body{background: #0FC;}}
</style>
<div class= "Container" >
<nav class= "NavBar navbar-default" role= "Navigation" >
<div class= "Container-fluid" >
<!--Collect the Nav links, forms, and other content for toggling
<div class= "Collapse navbar-collapse" id= "bs-example-navbar-collapse-1" >
<ul class= "Nav navbar-nav" >
<li><a href= "index.php" > Browse News </a></li>
<li><a href= "add.php" > Press releases </a></li>
</ul>
</div>
<!--/.navbar-collapse--
</div>
<!--/.container-fluid--
</nav>
</div>
<script type= "Text/javascript" src= "Bootstrap-3.2.0-dist/js/jquery-1.9.1.min.js" ></script>
<script type= "Text/javascript" src= "Bootstrap-3.2.0-dist/js/bootstrap.js" ></script>
<script type= "Text/javascript" src= "Prettify-4-mar-2013/prettify.js" ></script>
</body>

After two simple tasks above, it's time to write the homepage index.php:

First, import the navigation bar menu.php

<?php include ("index.php"); ?>

And then add a title and a table.

<table class= "Table Table-hover" >

<tr>

<th> News Id</th>

<th> title </th>

<th> keywords </th>

<th> author </th>

<th> Time </th>

<th> content </th>

<th> Operations </th>

</TR>

<?php//There are 5 parts here

1. Import the configuration file

Require ("dbconfig.php");

2. Link MySQL, select database

[Email Protected]_connect (Host,user,pass) or Die ("Link Database error! ");

mysql_select_db (DBNAME, $link);

3. Execute the query and return the result set

$sql = "SELECT * from News ORDER by addtime DESC";

$result =mysql_query ($sql, $link);

4. Parse the result set and traverse the output

While ($row =mysql_fetch_assoc ($result)) {

echo "<tr>";

echo "<td>{$row [' id ']}</td>";

echo "<td>{$row [' Tilte ']}</td>";

echo "<td>{$row [' keywords ']}</td>";

echo "<td>{$row [' Author ']}</td>";

echo "<td>{$row [' Addtime ']}</td>";

echo "<td>{$row [' content ']}</td> ';

echo "<td>

<a href= ' # ' > Delete </a>;//here the "#" is just a code name, the back will replace it, due to the complexity of adding and removing operations, so write a action.php file separately

<a href= ' # ' > Edit </a>;

</td> ";

echo "</tr>"

}

5. Releasing the result set

Mysql_free_result (&result);

Musql_close ($link);

?>

</table>


action.php:

<?php

This is a page of data deletion and modification

1. Import the configuration file

Require ("dbconfig.php");

2. Link MySQL and select the database

[Email Protected]_connect (Host,user,pass) or Die ("Database link failed");

mysql_select_db (DBNAME, $link);

3. According to the value of the action, to determine the operation, to execute the corresponding code

Switch ($_get["action"]) {

Case "Add":

1. Get the information you want to add, plus additional information

$tilte =$_post["title"];

$keywords =$_post["keywords"];

$author =$_post["Author"];

$content =$_post["Content"];

$addtime =time ();

2. Filtering of information

3. Stitching the SQL statements and performing the appropriate actions

$sql =insert into News value (NULL, ' ($title) ', ' ($keywords) ', ' ($author) ', $addtime, ' ($content) ');

mysql_query ($sql, $link);

4. Determine whether the success

$id =mysql_insert_id ($link);

if ($id >0) {

echo "

}

else{

echo "

}

Echo ("<a href= ' Javascript:window.history.back () ' > Return </a>");

Echo ("<a href= ' index.php ' > Browse News </a>");

Break

Case "Del":

1. Get the news ID you want to delete:

$id =$_get[' id '];

2. Assemble and delete the SQL statement, and perform the corresponding delete operation

$sql = "Delete from news where id= ($id)";

mysql_query ($sql, $link);

3. Automatically jump to the news browsing screen after deletion

Header ("location:index.php");

Break

Case "Update":

1. Get the information you want to modify
$title = $_post[' title '];
$keywords = $_post[' keywords ');
$author = $_post[' author ');
$content = $_post[' content '];
$id = $_post[' id '];
2. Filter the information to be modified (omitted here)
3. Assemble and modify SQL statements and perform modification actions
$sql = "Update news set title= ' ($title) ', keywords= ' ($keywords) ', author= ' ($author) ', content= ' ($content) ' where id= ($id )";
Echo $sql;
mysql_query ($sql, $link);
4. Skip to the Browsing interface
Header ("location:index.php");

Break

}

4. Close the database link

Mysql_close ("$link");

?>


The following page add.php files for adding news:

<?php include ("menu.php");//import navigation bar?>

<div class= "Container" >
<form action= "Action.php?action=add" method= "POST" >

<table class= "Table table-bordered table-hover table-responsive" >
<tr>
&LT;TD align= "center" > title </td>
<td><input class= "col-xs-10" type= "text" name= "title" ></td>
</tr>
<tr>
&LT;TD align= "center" > Keywords </td>
<td><input class= "col-xs-10" type= "text" name= "keywords" ></td>
</tr>
<tr>
&LT;TD align= "center" > Author </td>
<td><input class= "col-xs-10" type= "text" name= "author" ></td>
</tr>
<tr>
&LT;TD valign= "Top" align= "center" > Content </td>
<td><textarea class= "col-xs-10" name= "content" ></textarea></td>
</tr>
<tr>
&LT;TD colspan=2 align= "center" >
<input class= "btn btn-primary" type= "Submit" value= "Add" >
<input class= "btn btn-primary" type= "reset" value= "reset" >
</td>
</tr>
</table>
</form>
</div>


Then the edited page edit.php page:

<?php include ("menu.php");//import navigation bar
1. Import the configuration file
Require ("dbconfig.php");

2. Connect to MySQL and select a database
[Email Protected]_connect (Host,user,pass) or Die ("Database link failed");
mysql_select_db (DBNAME, $link);
3. Get the ID of the information you want to modify, and assemble to view the SQL statement, execute the query, get the information you want to modify
$sql = "SELECT * from news where id={$_get[' id '}";
$result =mysql_query ($sql, $link);
4. Determine if you have obtained the information you want to modify
if ($result && mysql_num_rows ($result) >0) {
$news =mysql_fetch_assoc ($result);
}else{
Die ("No information is found to be modified");
}

?>

<form action= "Action.php?action=update" method= "POST" >
<input type= "hidden" name= "id" value= "<?php echo $news [' id '];?>" >
<table class= "Table table-bordered table-hover table-responsive" >
<tr>
&LT;TD align= "center" > title </td>
<td><input class= "col-xs-10" type= "text" name= "title" Value= "<?php echo $news [' title '];?>" ></td >
</tr>
<tr>
&LT;TD align= "center" > Keywords </td>
<td><input class= "col-xs-10" type= "text" name= "keywords" value= "<?php echo $news [' keywords '];?>" > </td>
</tr>
<tr>
&LT;TD align= "center" > Author </td>
<td><input class= "col-xs-10" type= "text" name= "author" value= "<?php echo $news [' author '];?>" ></ Td>
</tr>
<tr>
&LT;TD valign= "Top" align= "center" > Content </td>
<td><textarea class= "col-xs-10" name= "content" ><?php echo $news [' content '];?></textarea> </td>
</tr>
<tr>
&LT;TD colspan=2 align= "center" >
<input class= "btn btn-primary" type= "Submit" value= "edit" >
<input class= "btn btn-primary" type= "reset" value= "reset" >
</td>
</tr>
</table>
</form>


Finally, mention, delete and modify the "#" with what instead of

Here in order to humanize some, with the JS code to give a hint

<script type= "Text/javascript" >
function Dodel (ID) {
if (Confirm ("OK to delete?")) {
window.location= "action.php?action=del&id=" +ID;
}
}
</script>

First "#", replaced with Javascript:dodel ({$row ["id"]})

A second "#", with edit.php? id={$row ["id"]} override

At this point, a complete PHP news management system has been basically completed, tomorrow to improve a bit.




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.