Building a Quick and Easy Tag Board - Our Tag Board Functions! (
Page 5 of 11 )
We had to design our functions to achieve certain requirements. Those requirements are as follows:
• Retrieve tags
• Sort tags by date
• Report an error if one is encountered or if no tags exist in the database
• Display HTML formatted tags
• Create the HTML for the page with the inclusion of an inline frame (iframe)
• Include form for users to add a new tag
• Insert Posted data from form into database
• Add slashes to ensure database integrity
• Check for data in required fields (name, entry) and prompt user if null
• Insert into table and check whether successful
Now that we have identified our requirements, we can see a basic pattern of functions we are going to need. I have listed these below:
• doTags() – Retrieve, sort and report any errors encountered;
• doBoard() - Create the HTML page for the tag board including the iframe; and
• doInsert() – Insert posted data into database, adding slashes, checking for required fields and finally checking the success of the action.
So let's have a look at our function in greater detail. First off, doTags():
function doTags()
{
$conx = mysql_connect(HOST, USER, PASS) or die("Unable to connect to MySQL Server");
mysql_select_db(DB) or die("Unable to select Database");
$stSql = "SELECT tag_name, tag_url, tag_entry, tag_date FROM tagboard ORDER BY tag_date DESC";
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." – Action Aborted"; exit(); }
if (mysql_num_rows($arrResult) >= 1)
{
while ($arrRow = mysql_fetch_assoc($arrResult))
{
?>
<table align="center" width="100%" bgcolor="#FFFFFF">
<tr>
<td><a href="<? echo stripslashes($arrRow['tag_url']); ?>"><? echo stripslashes($arrRow['tag_name']); ?></a></td>
</tr>
<tr>
<td><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></td>
</tr>
<tr>
<td><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666"> <? echo date("F j, Y, g:i a", $arrRow['tag_date']); ?></span></td>
</tr>
</table>
<?
}
} else {
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">No entries to display</p>
<?
}
}
A reasonably easy to navigate function - I will run you through how this function went about achieving its requirements.