The forumTest.php page
This lesson is part of an ongoing Foruml tutorial. The first part is here: Build your own Forum, along with all the files you need. The previous lesson is here.
Start your server, and then load up the forumTest.php page in your browser by typing the following address (again, you can use localhost instead of 127.0.0.1):
127.0.0.1/forum/forumTest.php
You should see the main page of the forum (assuming that you've downloaded the files and saved them to the correct place). This one:
The Basic Forum (Opens in a new window 80K).
The HTML is, of course, not something you'd want on your own site - that table looks far too basic! But the code is what we're interested in. Here's the coding strategy:
- Get the number of posts in each forum
- Get the number of replies for each post
- Get the Forum main topics
- Build up some hyperlinks
- Print out a HTML table
The problem here is that we need to gain access to more than one table in our database. The Forum main topics are in the table called forumsections, the posts are in the table called wpposts, and the replies are in the table called wpreplies.
But the SQL is not too difficult, because we only need a limited amount of information: how many forum sections there are, and what the link text should be; a count of how many posts there are in each forum section; and a count of how many replies there are in each forum section.
Bearing this in mind, open up the code for the forumTest.php page.
At first sight, it looks rather long and daunting. You'll see some comments in the code, so as to break it down into manageable chunks.
The first eight lines of the code are just variables to hold the table HTML. This sets the look and feel of the forum. In fact, you can make a start right away in changing this!
Exercise
Even if your table HTML skills are a bit patchy, you can still change the colour scheme. On line 7 of the code, you'll see this:
$tableHeaders = "<TR WIDTH = 200 height = 10 align = center valign = middle bgcolor =#00EBEB>"
Change the bgcolor to anything you like. Try these, and see what happens:
#F84EFC
#402C3E
#74283E
What happened with the darker colours? Try a few colours of your own, and see how you get on
Another thing you can change is the main cell colour. The cell colouring is done with this line:
$tdStart = "<TD WIDTH = 200 height = 100 align = centervalign = middle bgcolor =#F84EFC>";
Again, change the bgcolor to anything you like. Start with the same colours as above.
Suppose you decided to go for one of the darker colours for the heading. How would you change the font colour for the heading text? Can you see what you would need to change? If you wanted white text, for example, the HTML would be this:
<FONT COLOR = white>White Text</FONT>
Where in the PHP would you put the FONT tag?
Exercise
Using the colours above, change your table so that it looks like this one:
The Basic Forum (Opens in a new window 58K).
Back to the Code
Ok, let's move on and examine the code.
After the table HTML, we set up some HTML for the hyperlinks:
$hrefStart = "<A HREF = pageThread.php?sID";
$hrefEnd = "</A>";
The important part here is the page we want to take the user to when a link is clicked, and the part after php. The page we want to take the user to is pageThread.php. But we're adding a question mark, and a variable name, as well: ?sID. We'll put something into the sID variable later. This will be the GET data that we want hand to the page called pageThread.php. If you hold your mouse over each link, you'll see this sID variable change. It will be one of five values: secWP, secXL, secVB, secWD, and secPH. It's these values that we want to hand over to pageThread.php.
The next 15 lines in the code set up some arrays. We want five arrays. They are:
$secIDs[ ] = array();
$tblPosts[ ] = array();
$tblReply[ ] = array();
$numPosts[ ] = array();
$numReply[ ] = array();
You'll see what they all do as we go along. But notice the two arrays called $tblPosts[ ] and $tblReply[ ]. We've set these arrays up to hold the names of the tables for the posts, and the names of the tables for the replies:
$tblPosts[ ] = "wpposts";
etc
$tblReply[ ] = "wpreplies";
etc
The technique we'll use is to loop round these tables, and get information from each table.
The next six lines just get a connection to the database. You've met this code before, so we won't go into it.
An if statement comes next, to see if a connection to the database has been found:
if ($db_found) {
}
You can add an else part to this, if you like. This should say what to do if the database is not found. Print a simple "database not found" statement, for example.
The first code inside of the if statement is a for loop. We want to record how many posts there are in each forum section, so we need to loop round each table and count the number of rows in each post table. The for loop starts like this:
for ($i = 1; $i < 6; $i++) {
}
The value of 6 is a hard-coded one. But this is not a good idea. Suppose you wanted to add more sections to the forum. The code would break right here. A better way to do this is use code to get how many rows there are in the forumsections table. You would then use this number in the for loop. For teaching purposes, though, we've stuck to a hard-coded value. We know there are only five main sections in our forum, and we won't be adding any more!
To count how many rows there are in each of the post tables, the first line of the for loop is this:
$SQL = "SELECT * FROM " . $tblPosts[$i];
We need some SQL to hand to PHP, and this is what the line does. The first value we stored in the $tblPosts array was "wpposts". So the first time round the loop, the SQL variable will hold this:
$SQL = "SELECT * FROM wpposts"
The next time round the loop, the SQL will be this:
$SQL = "SELECT * FROM wdposts"
Each time round the loop, the only thing that changes about the SQL is the name of the table.
To count the number of rows in each table, we have this:
$result = mysql_query($SQL);
if ($result) {
$num_rows = mysql_num_rows($result);
$numPosts[$i] = $num_rows;
}
We pass the SQL to the inbuilt PHP function mysql_query( ). If any results are found then the variable called $result will be true. We're testing for this in the if statement.
If it is true, then next two lines will be executed:
$num_rows = mysql_num_rows($result);
$numPosts[$i] = $num_rows;
The first one just returns how many rows there are in a table. The second line puts the number of rows into the array we set up earlier - $numPosts. When we come to write our HTML for the hyperlinks, we'll use this array to print out how many posts there are in each forum section.
The next few lines of the code are for the replies. We want to record how many replies there are in each forum section. We do this in exactly the same was as for the posts - just loop round the $tblReply array and execute some SQL. When the loop is finished, the number of replies in each section is held in the array called $numReply:
$numReply[$i] = $num_rows;
The next part of the code is a little bit trickier. We want to get the forum main topics, and build up the links. We're doing both of those things inside of another loop, a while loop this time. Here's the code:
$SQL = "SELECT * FROM forumsections";
$result = mysql_query($SQL);
$loopCount = 1;
while ($db_field = mysql_fetch_assoc($result)) {
$secIDs[$loopCount] = $hrefStart . "=" . $db_field['sectionID']
. ">" .
$db_field['sections'] . $hrefEnd;
$loopCount++;
}
The first two lines set up some SQL, and then execute that against the database. The third line sets up a loop counter. We use this to access a different slot in the $secIDs array. Then we have the while loop (actually, we should be testing to see if $result is true, just like we did with the other two loops):
while ($db_field = mysql_fetch_assoc($result)) {
}
The variable $db_field will hold the array data that is brought back from mysql_fetch_assoc($result). This inbuilt PHP function, if you remember, returns an array. The array that is brought back is a row from our forumsections table. (There are only two columns in this table.) The function will bring back data in this format:
Column_Name => Data
To access the data in the array, you can then do this:
$data = $db_field[Column_Name];
That's what the first line of the while loop does. It's a long line that spills over into two on these pages:
$secIDs[$loopCount] = $hrefStart . "=" . $db_field['sectionID'] . ">" . $db_field['sections'] . $hrefEnd;
The part before the equals sign is this:
$secIDs[$loopCount]
$secIDs is the name of one of the arrays we set up at the top of the page. We want to store the hyperlinks in this array. The hyperlink in $hrefStart was this:
$hrefStart = "<A HREF = pageThread.php?sID";
We're adding the sectionID after ?sID in the hyperlink above:
$hrefStart . "=" . $db_field['sectionID']
After this code executes, it would give you something like this:
pageThread.php?sID=secWP
To add the text for the hyperlink, we also have this in the while loop:
">" . $db_field['sections'] . $hrefEnd;
The right pointy arrow ( > ) completes the first part of the "A Href" HTML code. The text for the link is then this:
$db_field['sections']
Finally, we add the rest of the HTML code for a hyperlink:
$hrefEnd
The last thing we do in the while loop is to increment the loop counter:
$loopCount++;
And that's the while loop! Yes, it's quite difficult. But study it for a while, and you'll get there. Remember: all we want to do is to build up an array of hyperlinks. Each hyperlink will be in this format:
<A HREF = pageThread.php?sID=secWP>Link Text</A>
The last thing we do in the forumTest.php code is to print out the HTML table. Here it is:
print "<CENTER>";
print $TableStart;
print $tableHeaders;
for ($i = 1; $i < 6; $i++) {
print $RowStart;
print $tdStart . $secIDs[$i] . $tdEnd;
print $tdStart . $numPosts[$i] . $tdEnd;
print $tdStart . $numReply[$i] . $tdEnd;
print $RowEnd;
}
print $TableEnd;
print "</CENTER>";
The first line just prints out the HTML code to centre things (note the American spelling). Then we have our two variables we set up at the top of the code. This prints out the HTML for the start of a table, and prints out the formatted headings we set up (these include the colour changes you made earlier).
Next, we have a for loop. What the loop does is to print out table code. It prints out the row start tag (<TR>), and then some table data tags (<TD>). Inside of each table data tag is the data from our three arrays $secIDs, $numPosts, and $numReply.
The last line in the for loop prints out the row end tag (</TR>). After the loop has finished, we print out the HTML for table end, and end the centre tag.
And that completes the code for the main page of our forum, forumTest.php. Here's a summary of what we did:
- Set up some variables to hold HTML table information
- Set up some arrays to hold information from the database tables
- Set up an array to hold all the hyperlinks
- Accessed the database, and returned the number of rows in the posts and replies tables
- Returned the information about each forum section, and built up a hyperlink
- Looped round and printed out the table, the hyperlinks, the number of posts, and the numbers of replies
Exercise
Play around with the HTMl Table code, and see if you can improve things.
If you know any CSS, you can try to add that too.
Next, we'll take a look at the code for that page printed out in all the hyperlinks: pageThread.php.