技术开发 频道

PHP应用实例---编写RSS系统

点和点击

   随着新闻显示已不再是问题,所剩下的就是增加一个简单的管理工具来管理SQLite数据库的内容。这里的代码和你在PHP 101第14章所看到的将会十分相似:一个提供了当前数据库的快照以及增加新条目的表单的被称为admin.php的起始页面。此处为其全文:

<html> <head><basefont face = 'Arial'></head> <body> <h2>Feed Manager</h2> <h4>Current Feeds:</h4> <table border = '0' cellspacing = '10'> <?php // PHP 5 // include configuration file include('config.php'); // open database file $handle = sqlite_open($db) or die('ERROR: Unable to open database!'); // generate and execute query $query = "SELECT id, title, url, count FROM rss"; $result = sqlite_query($handle, $query) or die("ERROR: $query. ".sqlite_error_string(sqlite_last_error($handle))); // if records present if (sqlite_num_rows($result) > 0) { // iterate through result set // print article titles while ($row = sqlite_fetch_object($result)) { ?> <tr> <td><?php echo $row->title; ?> (<?php echo $row->count; ?>)</td> <td><font size = '-2'><a href="delete.php?id=<?php echo $row->id; ?>">delete</a></font></td> </tr> <?php } } // if there are no records present, display message else { ?> <font size = '-1'>No feeds currently configured</font> <?php } // close connection sqlite_close($handle); ?> </table> <h4>Add New Feed:</h4> <form action = 'add.php' method = 'post'> <table border = '0' cellspacing = '5'> <tr> <td>Title</td> <td><input type = 'text' name = 'title'></td> </tr> <tr> <td>Feed URL</td> <td><input type = 'text' name = 'url'></td> </tr> <tr> <td>Stories to display</td> <td><input type = 'text' name = 'count' size = '2'></td> </tr> <tr> <td colspan = '2' align = 'right'><input type = 'submit' name = 'submit' value = 'Add RSS Feed'></td> </tr> </table> </form> </body> </html>

   下面是结果显示的样子:
 
   正如你所看到的那样,该脚本中有两个部分。前半部分连接至数据库然后打印所有当前配置的新闻信源的列表,每个新闻信源后面紧跟着“删除”连接。后半部分包含了一个用于管理员增加一个新的信源及其属性的表单。
一旦表单被递交,数据就被递交给脚本add.php,该脚本验证数据然后将其保存到数据库中,下面是add.php脚本中的代码:

<html> <head><basefont face = 'Arial'></head> <body> <h2>Feed Manager</h2> <?php // PHP 5 if (isset($_POST['submit'])) { // check form input for errors // check title if (trim($_POST['title']) == '') { die('ERROR: Please enter a title'); } // check URL if ((trim($_POST['url']) == '') || !ereg("^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\._\?\,\'/\\\+&%\$#\=~\-])*$", $_POST['url'])) { die('ERROR: Please enter a valid URL'); } // check story number if (!is_numeric($_POST['count'])) { die('ERROR: Please enter a valid story count'); } // include configuration file include('config.php'); // open database file $handle = sqlite_open($db) or die('ERROR: Unable to open database!'); // generate and execute query $query = "INSERT INTO rss (title, url, count) VALUES ('".$_POST['title']."', '".$_POST['url']."', '".$_POST['count']."')"; $result = sqlite_query($handle, $query) or die("ERROR: $query. ".sqlite_error_string(sqlite_last_error($handle))); // close connection sqlite_close($handle); // print success message echo "Item successfully added to the database! Click <a href = 'admin.php'>here</a> to return to the main page"; } else { die('ERROR: Data not correctly submitted'); } ?> </body> </html>

   脚本的下半部分对你来说应该是熟悉的:它包含了常用的函数调用来打开SQLite数据库然后执行一条插入查询语句来保存用户数据到数据库中。然而有趣的是脚本的上半部分,它包含了一些输入测试以确保正在被保存的数据不包含无用数据。

   这里有三个测试。一个检查描述性题目是否存在,另外一个使用is_numeric()函数来验证用于信息数目的输入值是有效的数字,而第三个使用ereg()函数来检查URL的格式。如果你阅读了第十三章,那么你将会完全了解验证用户输入的重要性;在这里,该理论在起作用。

   上面的代码负责增加新的RSS信源。现在,怎样去除它们呢?

   回忆一下在admin.php文件中列表中显示的每个信源如何拥有一个“删除”链接,该链接指向了脚本delete.php。假定给出一个信源ID号(通过链接传递),该delete.php脚本负责从表格中删除一个新闻信源。请看代码然后事情会变得更加清楚:

<html> <head><basefont face = 'Arial'></head> <body> <h2>Feed Manager</h2> <?php // PHP 5 if (isset($_GET['id']) && is_numeric($_GET['id'])) { // include configuration file include('config.php'); // open database file $handle = sqlite_open($db) or die('ERROR: Unable to open database!'); // generate and execute query $query = "DELETE FROM rss WHERE id = '".$_GET['id']."'"; $result = sqlite_query($handle, $query) or die("ERROR: $query. ".sqlite_error_string(sqlite_last_error($handle))); // close connection sqlite_close($handle); // print success message echo "Item successfully removed from the database! Click <a href = 'admin.php'>here</a> to return to the main page"; } else { die('ERROR: Data not correctly submitted'); } ?> </body> </html>

   通过URL GET方法传递的记录ID号由delete.php脚本获得,然后使用DELETE SQL查询来删除对应的记录。请亲自试验然后查看结果。

   那就是我为你准备的一切。我希望你喜爱这总共15章的PHP 101旅程,而且希望你觉得它既有教育意义也有趣(我知道,我是这么觉得的,而且有你在前行中伴随是一种乐趣)。如果你期望阅读更多的关于PHP的特定方面内容,请访问www.melonfire.com然后浏览部分更多我的教程和文章。到那时… 编码快乐!

0
相关文章