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 also used in the bootstrap framework

Write down and tidy up your thoughts.

It's a very easy system. The first is to create 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 join the news
Content text not NULL);//This is the contents of the news

In this way, the database table is built, and the following is the start of writing the page.

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 of all. Import 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>keyword</th>

<th> author </th>

<th> Time </th>

<th> content </th>

<th> Operations </th>

</TR>

<?

PHP//Here are 5 parts

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. Run 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, because adding and removing operations is more complex, so write a action.php file separately

<a href= ' # ' > Changes </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. Depending on the value of the action. To infer the owning operation. Run the corresponding code

Switch ($_get["action"]) {

Case "Add":

1. Access to the information to be added, additional information

$tilte =$_post["title"];

$keywords =$_post["keywords"];

$author =$_post["Author"];

$content =$_post["Content"];

$addtime =time ();

2. Filtering of information

3. Splicing the SQL statement, run the corresponding operation

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

mysql_query ($sql, $link);

4. Whether the inference is successful

$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 the Delete SQL statement and run the corresponding delete operation

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

mysql_query ($sql, $link);

3. After the deletion, I actively jump to the news browsing interface

Header ("location:index.php");

Break

Case "Update":

1. Get the information you want to change
$title = $_post[' title '];
$keywords = $_post[' keywords ');
$author = $_post[' author ');
$content = $_post[' content '];
$id = $_post[' id '];
2. Filter the information to be changed (omitted here)
3. Assemble and change the SQL statement and run the change operation
$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 to join the 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" >keyword</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= "join" >
<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 change, and assemble to view the SQL statement, run the query, get the information you want to change
$sql = "SELECT * from news where id={$_get[' id '}";
$result =mysql_query ($sql, $link);
4. Infer if you have obtained the information you want to change
if ($result && mysql_num_rows ($result) >0) {
$news =mysql_fetch_assoc ($result);
}else{
Die ("No information is found to be changed");
}

?

;

<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>
<td   align= "center" > title </td>
<td><input  class= " Col-xs-10 "type=" text "name=" title "Value=" <?

PHP echo $news [' title '];?> "></td>
</tr>
<tr>
<TD align= "center" >keyword</td>
<td><input class= "col-xs-10" type= "text" name= "keywords" value= "<?php echo $news [' keywords '];?>" > </td>
</tr>
<tr>
<TD align= "center" > Author </td>
<td><input class= "col-xs-10" type= "text" name= "author" value= "<?

PHP echo $news [' author '];?

> "></TD>
</tr>
<tr>
<td valign= "Top"  align= "Center" > Content </td>
<td><textarea   class= "col-xs-10" name= "content" ><?php echo $news [' content '];?

></textarea></td>
</tr>
<tr>
<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>


At last. Mention it. Deleted and Changed "#" with what to replace

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 is basically complete. Try to improve it tomorrow.




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

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.