Developing Multipurpose Pages With PHP - Examining The Code
(Page 3 of 4 )
First, the script checks if there is a username and password being passed through a form via the HTTP POST method. If there is none, the script will display the code that will prompt for a username and password.
If they are present then the script will run queries on the database to check if a valid username and password combo was used. If it is, then the script will display the message "Welcome, $name!", otherwise it'll display an error and prompt the user again.
As I said earlier, multipurpose pages use control structures to choose one of many possible 'templates' to generate a specific, and yet dynamic, page. When using forms, which I have seen to be the most common element used with multipurpose, you can easily implement if/else to display either the form, or the result text.
Multipurpose can also be used to create forums with GET values instead of POST values. This enables you to create links instead of forms that will get you to a certain version of one page. Although not evident until forums are made, a multipurpose page can be in any number of parts, not just two.
Multiple (3+) parts allows for multiple forms, or in the case of forums, a forum ID, a thread ID, and a message ID. Other values can also be tagged on. This increases information-gathering capabilities, and continues to cut down on number of pages and overall space taken up on servers.
A Practical Example Here's an example of a multipurpose web page. See if you can figure out what it does:
<?PHP
$forum = $_GET['forum'];
if (!isset ($forum) ) {
$sql = "SELECT forums, ID FROM master_list";
$get_forums = mysql_query($sql);
while ($forums = mysql_fetch_array($get_forums) ) {
echo ('<a href="forums.php?forum'.$forums["ID"].'">'.$forums["forums"].'</a><br />');
}
}
else {
$thread = $_GET['thread'];
if (!isset ($thread) ) {
$sql = "SELECT thread, ID FROM forum".$forum;
$get_threads = mysql_query($sql);
while ($threads = mysql_fetch_array($get_forums) ) {
echo ('<a href="forums.php?forum='.$forum.'&thread='.$threads["ID"].'">');
echo ($threads["thread"].'</a><br />');
}
}
else {
$sql = "SELECT messages FROM thread".$thread;
$get_messages = mysql_query($sql);
while ($messages = mysql_fetch_array($get_messages) ) {
echo ('<table><tr><td>'.$messages["message"].'</td></tr></table>');
}
}
?> This example uses the same principals and the same overall concept as the first example, including the use of Superglobal arrays and control structures.
This example is easy to expand, because the main difference between one page and the next is the name of variables, the number of if/else statements, and the number of parts.
If/else control structures can be substituted with a select case structure if you'd prefer
Quick Tips - When designing a site, always do HTML first, then incorporate PHP
- Start with multipurpose pages -- don't code and then convert
- Think of the future and never use commands that have new alternates unless they produce different effects.
- GET data doesn't have to come from a form, it can be 'placed in' by links instead
Next: Conclusion >>
More PHP Articles
More By Unknown User