PHP Tutorial- Part 3
|
|
|
|
|
Richard H. Nilsson, July 10, 2003 (Updated for PHP 5, Feburary, 2006) Display the guestbookIn Part 1, we created a database and built a table in it called "Messages". In Part 2, we constructed an interactive form for making entries in the guestbook, and now we will build a page to display the guestbook, a few entries at a time, sorted most-recent-first. At the bottom of the page, we'll show a "Prev" "Next" and a collection of page numbers in between, so visitors can "browse" the guestbook. When we load the guestbook page, the first thing we want to do is always ensure it is up to date - that is, if someone has just made an entry, we want that person's entry to immediatley pop into the screen, else she's likely to go back and make another entry, thinking hers didn't actually make it into the guestbook. The way we do that is to start the page with some special information for the browser that causes it not to cache the page, but always get a fresh copy from the server:
Look and feel...Here is an in-line stylesheet. For more on using CSS page design, see the CSS articles at A List Apart or this Introduction to CSS Layout. You can, of course save this out to a ".css" file and import it if you wish. The styles of all the guestbook pages are close enough to use a single external stylesheet:
As with the form, we use CSS to position and set the margins for the "maincontent" DIV. There are also a few miscellaneous styles for various objects on the page. I like to resize my guestbook so it is obvious it's a new window that can be closed, revealing the main page below. Here again is the in-line JavaScript to do that, which finishes out the HEAD of the page:
Beginning the display - the heading and database prepNow comes the definition of the main content block, the heading for the page, and a link for calling up the entry form, if disired. There is also a book icon, which I show here <body> <div id="maincontent"> <div class="sctn1hd">My Guestbook:</div> <div id="add"> <a class="new" href="add_entry.php"> <img src="images/add2bookIcon.gif" border="0"> Add an entry...</a> </div> In preparation for retrieving some records to display, we have to once more log in to the database. You have to do this on each PHP page which has separate query code.
OK, we've opened the PHP code block with "<?php". We could lump all our functions together at the bottom of the page and put the error function there too, but I like to keep all the database-associated code together. Just personal preference. Adjust the userName and passWord to agree with your account settings. Now we'll initialize some variables:
To get information back out of our database we build a simple query statement. The "*" means "gimmee all the fields". Since we haven't been choosy, we get all the fields of all the records. Then we instruct MySQL to arrange (sort) them in ORDER of DESCending Date (newest first), and within each date, sort by DESCending Time (also newest first). The next line defines a global variable "ROWS", and sets it equal to 10. That will show 10 messages on each page; if you'd like to show more or less, adjust that number. This page will be reloaded repeatedly and we pass the page URI in the variable $scriptName by setting it to the HTTP global $_SERVER["PHP_SELF"]. We will be selecting a new group of 10 messages from the Messages table each time, using a variable called "$offset" to determine how far to offset into the sorted stack of retrieved records. For instance, to start with the 21st message, we reload the page with "http://guestbook.php?offset=20" (the first record is record 1). The "?" is http query notation for passing variables to code on a page. When we come back into this page the PHP variable "$offset" equals "20". Now all we have to do is call the "browse function" with our accumulated variables to determine where to get the data, which ones to display, and what page to display with: browse($scriptName, $db, $query, $offset); Here is the browse function - :
Wow! That's a mouthful. Basically what all that does is generate one page of listings starting at [$offset] number of records into the database (we don't retrieve ALL of the records each time, that would be slow!) and collecting [ROWS] number of entries. We print them, then begin to generate a list of links to other offsets that will "window" into the database and display the rest of the entries, a page at a time. About a third of the way down that listing, notice the function call the "print_row()". Here's the code for that function:
We use "printf" (print formatted) statements to display the particular retrieved fields for the current entry record. The third line generates a link to our "edit_entry.php" page, for editing unsavory entries. Notice how we can use double quotes in the print strings if we "escape" them with backslashes ("\"). I leave it to you to figure how the remaining lines work from inspecting them and refering to the PHP Function Reference. This ends the PHP code, so all that's left is to close out the HTML: <p> </p> <div id="add"> <a class="new" href="add_entry.php"> <img src="images/add2bookIcon.gif" border="0"> Add an entry...</a> </div> </div> <br /> <!--provide a close button--> <p style="width:15%;margin:auto;border:1px solid #ccc;" align="center"> <a class="run2" href="javascript:window.close();">CLOSE<a/></p> <p> </p> </body> </html> Next - edit the guestbook: go to Page 4 >>. Copyright 2003 Richard Nilsson. Verbatim copying and redistribution of this entire article are permitted without royalty in any medium provided this notice is preserved. |
|