首先,您需要创建一个表来存储所有数据。
让我们开始做类似的事情:
articles
--------
id_article (integer) (auto_increment) (primary key)
title (varchar 250)
author (varchar 250)
date (date)
content (text)
您可以在自己喜欢的数据库管理工具中轻松创建此表。
如果你没有,我强烈推荐 [mysql workbench][1],它很棒(你可以从一开始就考虑使用一个好的工具,而不是那个糟糕的 phpMyAdmin...)。
id_article 是一个号码,可以帮助您识别您的文章。
不用设置,从1开始,自动递增。
然后让我们专注于 php 脚本。
我建议3:articles.php显示所有文章,article_detail.php显示一篇完整文章,article_edit.php编辑一篇文章。
请注意,我不会给你 php 代码,否则你可能学不到很多东西。我只会给你伪代码,如果你遇到困难需要帮助,请告诉我。
//articles.php //<---this is a comment
//first let's prepare a basic html page
<html>
<head>
<title>Article list</title>
</head>
<body>
<h3>Articles list :</h3>
<ul>
<?php
//connect to mysql server + select database using [mysqli][2]
//execute the following query : "Select * from articles" with [mysqli_query][3]
//loop on your resultset using while and [mysqli_fetch_array()][4]
//in your loop, echo the <li></li> that will populate your <ul>
//Note, to create the links use : echo "<a href='./article_detail.php?id=".$row['id_article']."'>".$row['title']."</a>";
?>
</ul>
</body>
</html>
这是我们的第一个脚本。
您可以通过手动在表中创建一些行来尝试articles
//article_detail.php
//Note that there isn't much new in there
//First your html
<html>
<head>
<title>Article detail</title>
</head>
<body>
<?php
//a new thing are the http variables.
//we will be using [$_GET][5]
$id_article = (int) $_GET['id_article']; //remember to cast your integers as int in order to prevent SQL Injection
//connect to the database.
//use mysqli_query to execute : "Select * from articles where id_article='$id_article'"
//use fetch_array() to get the row.
//display the row the way you want using html.
</html>
测试完后,就可以继续编写最终脚本了。
这将采用整数作为$_GET参数:'id_article'(如果没有参数,那么我们将不得不使用mysql创建一篇新文章。
它还可以选择使用$POST 参数与title、author 和content。
//article_edit.php
<html><head><title>Edit an article</title></head>
<body>
<?
if(count($_POST)){
//POST request, get the parameters from it, and update the row using $_GET['id_article']
//remember to use htmlentities() and addslashes() on author, title and content, so your script is bullet-proof to XSS and SQL Injection
echo "<h3 style='color:green;text-align:center;'>Article updated!</h3>";
}
if(isset($_GET['id_article'])){
//We are editing an existing article : gather its content for edition
}else{
//New article, create an empty row, and select it.
}
//after this if statement you should have '$id_article' and an array '$article' which contains the article data
?>
<form method='post' action='./article_edit.php?id_article=<?=$id_article?>'>
Author : <input size='50' name='article_author' value='<?=$article['author']?>'/><br/>
Title : <input size='100' name='article_title' value='<?=$article['title']?>'/><br/>
Content : <textarea name='article_content'><?=$article['content']?></textarea><br/>
<input type='submit' value='Send'/>
</form>
</body>
</html>
你去。
[var_dump()][6] 是一个很好的 php 函数,如果我可以友好地命名的话,它基本上可以显示变量的内容。非常好的工具。
接下来要做什么?
-保存文章的创建日期。
-使用包含并集中您的公共源代码(例如:mysql_connect、html headers...)
-添加登录页面,并仅授权会员编辑。
-安装像 tiny_mce 这样的文本编辑器来编辑文章。
-...
玩得开心。
1:http://dev.mysql.com/downloads/tools/workbench/
2:http://php.net/manual/en/mysqli.construct.php
3:http://php.net/mysqli_query
4:http://php.net/manual/en/mysqli-result.fetch-array.php
5:http://php.net/manual/en/reserved.variables.get.php
6:http://php.net/manual/en/function.var-dump.php