Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 82 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
82
Dung lượng
1,81 MB
Nội dung
Display all the current comments for this article on this page: </form> <?php showComments($_GET[‘article’], FALSE); require_once ‘footer.php’; ?> And that’s it! They’re getting easier, aren’t they? Stay with us— only a couple more short ones. That last one was a doozy, huh? Additional CMS Features So far, you’ve created a system to create and manage users, and publish articles, but there are a couple additional features that can help make your CMS a lot more usable and manageable. What you’re going to add is the ability for users to update their information, and the ability to search published articles by keyword. Try It Out User Control Panel In this exercise, you’re going to create a page so users can maintain their own information. 1. Enter the following code, and save it as cpanel.php: <?php require_once ‘conn.php’; require_once ‘header.php’; $sql = “SELECT name, email “ . “FROM cms_users “ . “WHERE user_id=” . $_SESSION[‘user_id’]; $result = mysql_query($sql, $conn) or die(‘Could not look up user data; ‘ . mysql_error()); $user = mysql_fetch_array($result); ?> <form method=”post” action=”transact-user.php”> <p>Name:<br> <input type=”text” id=”name” name=”name” value=”<?php echo htmlspecialchars($user[‘name’]); ?>”></p> <p>Email:<br> <input type=”text” id=”email” name=”email” value=”<?php echo htmlspecialchars($user[‘email’]); ?>”></p> <p><input type=”submit” class=”submit” name=”action” value=”Change my info”></p> 472 Chapter 13 18_579665 ch13.qxd 12/30/04 8:14 PM Page 472 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com </form> <h2>Pending Articles</h2> <div class=”scroller”> <table> <?php $sql = “SELECT article_id, title, date_submitted “ . “FROM cms_articles “ . “WHERE is_published=0 “ . “AND author_id=” . $_SESSION[‘user_id’] . “ “ . “ORDER BY date_submitted”; $result = mysql_query($sql, $conn) or die(‘Could not get list of pending articles; ‘ . mysql_error()); if (mysql_num_rows($result) == 0) { echo “ <em>No pending articles available</em>”; } else { while ($row = mysql_fetch_array($result)) { echo “<tr>\n”; echo ‘<td><a href=”reviewarticle.php?article=’ . $row[‘article_id’] . ‘“>’ . htmlspecialchars($row[‘title’]) . “</a> (submitted “ . date(“F j, Y”, strtotime($row[‘date_submitted’])) . “)</td>\n”; echo “</tr>\n”; } } ?> </table> </div> <br> <h2>Published Articles</h2> <div class=”scroller”> <table> <?php $sql = “SELECT article_id, title, date_published “ . “FROM cms_articles “ . “WHERE is_published=1 “ . “AND author_id=” . $_SESSION[‘user_id’] . “ “ . “ORDER BY date_submitted”; $result = mysql_query($sql, $conn) or die(‘Could not get list of pending articles; ‘ . mysql_error()); if (mysql_num_rows($result) == 0) { echo “ <em>No published articles available</em>”; } else { while ($row = mysql_fetch_array($result)) { echo “<tr>\n”; echo ‘<td><a href=”viewarticle.php?article=’ . 473 Building a Content Management System 18_579665 ch13.qxd 12/30/04 8:14 PM Page 473 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com $row[‘article_id’] . ‘“>’ . htmlspecialchars($row[‘title’]) . “</a> (published “ . date(“F j, Y”, strtotime($row[‘date_published’])) . “)</td>\n”; echo “</tr>\n”; } } ?> </table> </div> <br> <?php require_once ‘footer.php’; ?> 2. On the navigation links, click Control Panel. You should see a screen similar to the one shown in Figure 13-11. Here you can change your user information (user name and e-mail), and see what articles you have written for the site. Figure 13-11 474 Chapter 13 18_579665 ch13.qxd 12/30/04 8:14 PM Page 474 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com How It Works Now let’s take a look at cpanel.php. The Control Panel page cpanel.php is used to allow users to change their usernames and e-mail addresses. They can also see all of the articles they have written, categorized by Pending and Published articles. As always, you need to connect to the database. And, as always, conn.php takes care of that. This time, you are displaying a Web page. You load up header.php, which is your standard header for all Web display pages. require_once ‘conn.php’; require_once ‘header.php’; You first go out to the database and get the user’s name and e-mail address. $sql = “SELECT name, email “ . “FROM cms_users “ . “WHERE user_id=” . $_SESSION[‘user_id’]; $result = mysql_query($sql, $conn) or die(‘Could not look up user data; ‘ . mysql_error()); $user = mysql_fetch_array($result); You will be entering and exiting PHP mode throughout this file and in the other Web pages. This gives you great flexibility and makes the page a lot easier to read. The next line exits PHP code: ?> The following form uses the post method. Notice the action. You already created that file. You know that when a submit button is clicked on this page, transact-user.php loads, the appropriate code runs, and the user is redirected to the control panel. <form method=”post” action=”transact-user.php”> Note the use of htmlspecialchars(). This prevents strange characters such as < or > from messing up the page. These characters will be displayed using their entity equivalents (such as < and >). <p>Name:<br> <input type=”text” id=”name” name=”name” value=”<?php echo htmlspecialchars($user[‘name’]); ?>”></p> The rest of the form is standard HTML. Now you need to display Pending and Published articles. Time to drop back into PHP: <p>Email:<br> <input type=”text” id=”email” name=”email” value=”<?php echo htmlspecialchars($user[‘email’]); ?>”></p> <p><input type=”submit” class=”submit” name=”action” value=”Change my info”></p> 475 Building a Content Management System 18_579665 ch13.qxd 12/30/04 8:14 PM Page 475 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com </form> <h2>Pending Articles</h2> <div class=”scroller”> <table> This code gets all pending articles written by this user. They are ordered by date_submitted, with the oldest being first. Please note that if there are no pending articles, this will not return a MySQL error. You will still get a result set, but there won’t be any rows. You handle that contingency next. <?php $sql = “SELECT article_id, title, date_submitted “ . “FROM cms_articles “ . “WHERE is_published=0 “ . “AND author_id=” . $_SESSION[‘user_id’] . “ “ . “ORDER BY date_submitted”; $result = mysql_query($sql, $conn) or die(‘Could not get list of pending articles; ‘ . mysql_error()); If there are no pending articles, you simply state so and move on. Otherwise . . . if (mysql_num_rows($result) == 0) { echo “ <em>No pending articles available</em>”; } else { You loop through each pending article and display the title (with a link to the article), along with the date/time it was submitted. while ($row = mysql_fetch_array($result)) { echo “<tr>\n”; echo ‘<td><a href=”reviewarticle.php?article=’ . $row[‘article_id’] . ‘“>’ . htmlspecialchars($row[‘title’]) . “</a> (submitted “ . date(“F j, Y”, strtotime($row[‘date_submitted’])) . “)</td>\n”; echo “</tr>\n”; } } Okay, this next code is almost identical to the code used to display pending articles. This time, display them using the date_published column. <?php $sql = “SELECT article_id, title, date_published “ . “FROM cms_articles “ . “WHERE is_published=1 “ . “AND author_id=” . $_SESSION[‘user_id’] . “ “ . “ORDER BY date_submitted”; $result = mysql_query($sql, $conn) or die(‘Could not get list of pending articles; ‘ . mysql_error()); 476 Chapter 13 18_579665 ch13.qxd 12/30/04 8:14 PM Page 476 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com if (mysql_num_rows($result) == 0) { echo “ <em>No published articles available</em>”; } else { while ($row = mysql_fetch_array($result)) { echo “<tr>\n”; echo ‘<td><a href=”viewarticle.php?article=’ . $row[‘article_id’] . ‘“>’ . htmlspecialchars($row[‘title’]) . “</a> (published “ . date(“F j, Y”,strtotime($row[‘date_published’])) . “)</td>\n”; echo “</tr>\n”; } } Then load the footer. It is only a few lines of HTML for now, but some day you will want to put a menu, or perhaps a copyright line. The great thing is, even if your site uses 300 pages, you will have to add that information in only one place: footer.php. ?> </table> </div> <br> <?php require_once ‘footer.php’; ?> Try It Out Search The final thing you are going to add is a simple search feature. Using the power of the full-text searching in MySQL, you can easily put a keyword search field on each page, and show the results here. 1. Create search.php: <?php require_once ‘conn.php’; require_once ‘outputfunctions.php’; require_once ‘header.php’; $result = NULL; if (isset($_GET[‘keywords’])) { $sql = “SELECT article_id FROM cms_articles “ . “WHERE MATCH (title,body) “ . “AGAINST (‘“ . $_GET[‘keywords’] . “‘ IN BOOLEAN MODE) “ . “ORDER BY MATCH (title,body) “ . “AGAINST (‘“ . $_GET[‘keywords’] . “‘ IN BOOLEAN MODE) DESC”; $result = mysql_query($sql, $conn) or die(‘Could not perform search; ‘ . mysql_error()); } echo “<h1>Search Results</h1>\n”; if ($result and !mysql_num_rows($result)) { echo “<p>No articles found that match the search terms.</p>\n”; } else { while ($row = mysql_fetch_array($result)) { 477 Building a Content Management System 18_579665 ch13.qxd 12/30/04 8:14 PM Page 477 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com outputStory($row[‘article_id’], TRUE); } } require_once ‘footer.php’; ?> 2. On any page with a search box on the top, enter a word that existed in the article you created. Once you submit the form, search.php will appear, and any matches should be shown. How It Works First, you include the necessary files: <?php require_once ‘conn.php’; require_once ‘outputfunctions.php’; require_once ‘header.php’; As long as you passed keywords, you do a search. In the following SQL statement, you’ll notice the MATCH and AGAINST keywords. This is the syntax used to do a text search in those fields. They must be full-text indexed fields in order to perform this search. Visit dev.mysql.com/doc/mysql/en/ Fulltext_Search.html for more information on full-text indexed fields. $result = NULL; if (isset($_GET[‘keywords’])) { $sql = “SELECT article_id FROM cms_articles “ . “WHERE MATCH (title,body) “ . “AGAINST (‘“ . $_GET[‘keywords’] . “‘ IN BOOLEAN MODE) “ . “ORDER BY MATCH (title,body) “ . “AGAINST (‘“ . $_GET[‘keywords’] . “‘ IN BOOLEAN MODE) DESC”; $result = mysql_query($sql, $conn) or die(‘Could not perform search; ‘ . mysql_error()); } If you didn’t find a match, say so: echo “<h1>Search Results</h1>\n”; if ($result and !mysql_num_rows($result)) { echo “<p>No articles found that match the search terms.</p>\n”; Otherwise, loop through the results and display them as snippets on the screen: } else { while ($row = mysql_fetch_array($result)) { outputStory($row[‘article_id’], TRUE); } } require_once ‘footer.php’; ?> 478 Chapter 13 18_579665 ch13.qxd 12/30/04 8:14 PM Page 478 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Summary Well, if you didn’t have writer’s cramp before, you probably do now. We hope this application has given you some insight into the separation of content and design. Because of the way the application was designed, you can easily modify the look and feel of the application by either directly altering your header and footer files, or using a CSS file to set up different styles. This won’t matter to your users; they will still be able to enter articles without ever having to worry about what the article will look like on the Web when it’s published. We also hope that you understand the importance of updating your site often enough to draw users back again and again. By adding an application like this to your site, and allowing users to add content for you, you create a dynamically changing site with fresh information. Just think about all the ways you could implement such a design: ❑ Create a message board. (This is examined in more detail in Chapter 16.) ❑ Add a daily comic. Perhaps you have an artist who can draw you a comic every day. You could create an application that allows him or her to upload comic strips and allows users to comment on them. ❑ Compile photo journals. A while back, there was a project in which photographers went all over the world, and in a 24-hour period, they took their pictures and uploaded the digital images, and people in the central office typed up descriptions and allowed people to view them online. It was a very ambitious project and a perfect example of a CMS application. The bottom line is that if you have content that you want to be able to update on a regular basis, you definitely want to implement a CMS application. And now, you have the basic tools to build one on your own! Perhaps you should send your users an e-mail to tell them of your improved functionality. You’ll do that in Chapter 14. Exercises After all that typing, are we so cruel that we’d include more work at the end? Yes. Yes we are. See how you might accomplish the following tasks: 1. Delete when appropriate. When reviewing an article, the delete button always shows up, regardless of what happens in the transaction page. Alter the code so the Delete button only shows up when the article is pending. 2. Find out about the author. Authors of articles might want the readers to know a little more about them. Add the ability to enter extra fields in a user’s profile, and provide a link on the article full-view page to the author’s information. 3. Notify the author. Authors might want to be automatically notified when their stories have been approved. Add an e-mail notification upon approval, and give the user the ability to toggle their notification on and off. 479 Building a Content Management System 18_579665 ch13.qxd 12/30/04 8:14 PM Page 479 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 18_579665 ch13.qxd 12/30/04 8:14 PM Page 480 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 14 Mailing Lists Ah, yes. Mailing Lists. Two simple, innocent words that never meant anyone any harm. That is, until a few years ago, when someone decided to put the two together, and junk mail was born. Oh sure, mailing lists are used for more than junk mail. After all, how else are you going to receive your Quilting Monthly magazine unless your name and address are on a list? What does this have to do with PHP? In the world of e-mail and your incredible new Web site, a mailing list is a perfect way for you to communicate with all of your users. You might need to let every user know that your site has moved to a new address. Maybe you want to send out a monthly newsletter. Whatever the reason, you will occasionally need to send e-mails to many people, and you will need a relatively easy way to do so. Specifically, this chapter discusses the following: ❑ Administering a mailing list ❑ Creating a mailing list ❑ Spam ❑ Opt-in and opt-out So, are you ready to learn how to spam your Web site users? We hope not, because that is not what you are going to do. Yes, you are going to use mailing lists. And yes, you are going to send out “mass” e-mails. However, you are going to be responsible, and send e-mails only to those people who have agreed to be recipients. Right? Let’s get started. What Do You Want to Send Today? Before you actually create the mailing list, you must have something that you intend to send to your recipients. The train of thought usually goes like this: You decide that you have some infor- mation you want to share with your users. Here are a few possibilities: 19_579665 ch14.qxd 12/30/04 8:17 PM Page 481 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... $res $res $res echo = mysql_ query($sql1) or die (mysql_ error()); = mysql_ query($sql2) or die (mysql_ error()); = mysql_ query($sql3) or die (mysql_ error()); “Done.”; 485 Chapter 14 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Try It Out Mailing List Administration Next, you will create the admin page, where the administrator (that’s you!) can create, delete, and rename mailing lists... default ‘’, PRIMARY KEY (user_id) ) EOS; $res = mysql_ query($sql1) or die (mysql_ error()); $res = mysql_ query($sql2) or die (mysql_ error()); $res = mysql_ query($sql3) or die (mysql_ error()); echo “Done.”; ?> 4 If you ran sql.php in your browser, it should display a “Done.” message, indicating the database tables were created 483 Chapter 14 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com... need to have a page that handles the transactions Enter the following, and save it as admin_transact.php: . terms.</p>
”; } else { while ($row = mysql_ fetch_array($result)) { 477 Building a Content Management System 18_ 579 665 ch13.qxd 12/30/04 8:14 PM Page 477 Simpo PDF Merge and Split Unregistered Version -. information (user name and e-mail), and see what articles you have written for the site. Figure 13-11 474 Chapter 13 18_ 579 665 ch13.qxd 12/30/04 8:14 PM Page 474 Simpo PDF Merge and Split Unregistered. approval, and give the user the ability to toggle their notification on and off. 479 Building a Content Management System 18_ 579 665 ch13.qxd 12/30/04 8:14 PM Page 479 Simpo PDF Merge and Split Unregistered