The code is as follows |
Copy Code |
<?php Require_once ' config.php ';
$action = Empty ($_get[' action ')? ': $_get[' action ']; $id = Isset ($_get[' id ')? $_get[' id ': 0;
if (empty ($action)) {//List
$result = "SELECT * from Posts ORDER by PID DESC"; $items = 3; $page = isset ($_get[' page ')? Intval ($_get[' page '): 1; $page = ($page = = ' && $page < 0)? 1: $page; $total = Mysql_num_rows (mysql_query ($result));//Total number of data entries $pageall = Ceil ($total/$items); $offset = ($page-1) * $ITEMS;//Data start position $result. = "Limit {$offset},{$items}";//per page display $results = mysql_query ($result);
$prev = $page-1; $next = $page + 1; $goPrev = "<a href=" page= $prev "> Prev </a>"; $goNext = "<a href=" page= $next "> Next </a>";
if ($page = = $pageall) {$goNext = NULL;}
$pagestate = ';
if ($page = = 1 && $pageall > 1) { $pagestate. = $goNext; } elseif ($pageall > $page | | $page >1) { $pagestate. = $goNext. ' | '. $goPrev; } elseif ($page >1) { $pagestate. = $goPrev; } else { $pagestate. = ' Just one Page '; }
$pagelinks = ';
for ($i = 1; $i <= $pageall; $i + +) { $pagelinks. = ($i!= $page)? "<a href="? page= $i "> $i </a>": $i; $pagelinks. = ($i < $pageall)? '-': null; }
End Paging $data = Array ();
List articles while ($row = Mysql_fetch_array ($results)) {//mysql_fetch_array; take one row from the query result as an array; $data [] = $row;// }
List categories $recat = mysql_query ("SELECT * from category ORDER by CID DESC"); $cdata = Array ();
while ($catlist = Mysql_fetch_array ($recat)) { $cdata [$catlist [' cid ']] = $catlist; }
Include template (' Index ');
} elseif ($action = = ' Post ') {//Published
$title = $_post[' title ']; $date = Date ("y-m-d h:i:s"); $content = $_post[' content ']; $cid = $_post[' cat '];
if ($title = = ' | | $content = = ' | | $cid = = ') {
echo "Missing required fields Nnnn<a href=" Javascript:history.go (-1); > Return </a> ";
} else {
$insertPost = "INSERT into posts (title,time,post,cid) VALUES (' $title ', ' $date ', ' $content ', ' $cid ')";
if (!mysql_query ($insertPost, $sql)) { Die (' ERROR: '. mysql_error ()); }
Header (' location:index.php '); }
} elseif ($action = = ' View ') {//view
$view = mysql_query ("Select * from posts WHERE pid= ' $id '");
while ($row = Mysql_fetch_array ($view)) {
$title = $row [' title ']; $time = $row [' Time ']; $post = $row [' Post '];
}
if ($_server[' request_method '] = = ' POST ') {//comments
$name = isset ($_post[' name ')? $_post[' name ': 0; $review = Isset ($_post[' review '))? $_post[' review ': 0;
if ($name = = ' && $review = = ') {echo "Missing required fields"; Exit } $insertRview = "INSERT into review (Pid,name,review) VALUES (' $id ', ' $name ', ' $review ')";
if (!mysql_query ($insertRview, $sql)) { Die (' ERROR: '. mysql_error ()); }
Header ("Location: action=view&id= $id"); }
$rert = mysql_query ("SELECT * from Review WHERE pid= ' $id ' ORDER by RID ASC"); $data = Array ();
while ($relist = Mysql_fetch_array ($rert)) {
$data [] = $relist;
}
Include template (' View ');
} elseif ($action = = ' del ') {//delete
mysql_query ("DELETE from posts WHERE pid= ' $id '"); Header ("location:index.php");
} elseif ($action = = ' edit ') {//Edit
if ($_server[' request_method '] = = ' POST ') {
$title = $_post[' title ']; $content = $_post[' content ']; $date = Date ("y-m-d h:i:s");
if ($title = = ' or $content = = ') {echo missing required nnnn<a href= "Javascript:history.go (-1);" > Return </a> "; Exit;}
mysql_query ("UPDATE posts SET title= ' $title", time= ' $date ', post= ' $content ' WHERE pid= ' $id '); Header ("location:index.php");
} else {
$row = mysql_query ("Select * from posts WHERE pid= ' $id '"); $row = Mysql_fetch_array ($row);
Include template (' edit ');
}
} elseif ($action = = ' Newcat ') {//new category
$cat = isset ($_post[' category '))? $_post[' category ': 0;
if ($_server[' request_method '] = = ' POST ') {
if ($cat = = ") {echo missing required nnnn<a href=" Javascript:history.go (-1); " > Return </a> "; Exit;} $insertCat = "INSERT into category (category) VALUES (' $cat ')";
if (!mysql_query ($insertCat, $sql)) { Die (' ERROR: '. mysql_error ()); }
Header ("location:index.php");
} Include template (' Jioncat ');
} elseif ($action = = ' VCAT ') {
$cid = $_get[' cid '];
$revcats = "SELECT * from posts WHERE cid= ' $cid ' ORDER by CID DESC"; $items = 1; $page = isset ($_get[' page ')? Intval ($_get[' page '): 1; $page = ($page = = ' && $page < 0)? 1: $page; $total = Mysql_num_rows (mysql_query ($revcats));//Total number of data entries $pageall = Ceil ($total/$items); $offset = ($page-1) * $ITEMS;//Data start position $revcats. = "Limit {$offset},{$items}";//per page display $revcat = mysql_query ($revcats);
$prev = $page-1; $next = $page + 1; $goPrev = "<a href="? action=vcat&cid= $cid &page= $prev "> Prev </a>"; $goNext = "<a href="? action=vcat&cid= $cid &page= $next "> Next </a>";
if ($page = = $pageall) {$goNext = NULL;}
$pagestate = ';
if ($page = = 1 && $pageall > 1) { $pagestate. = $goNext; } elseif ($pageall > $page | | $page >1) { $pagestate. = $goNext. ' | '. $goPrev; } elseif ($page >1) { $pagestate. = $goPrev; } else { $pagestate. = ' Just one Page '; }
$pagelinks = ';
for ($i = 1; $i <= $pageall; $i + +) { $pagelinks. = ($i!= $page)? "<a href="? action=vcat&cid= $cid &page= $i "> $i </a>": $i; $pagelinks. = ($i < $pageall)? '-': null; }
$data = Array ();
while ($vclist = Mysql_fetch_array ($revcat)) { $data [] = $vclist; }
Include template (' cplist '); }
Mysql_close ($sql); |
An example of access converting to a MySQL database
Click to download: Http://file.111cn.net/download/2013/05/14/db2mysql.zip