Home arrow PHP arrow Page 6 - Building a Quick and Easy Tag Board

Retrieval and Sorting of Tags - PHP

Tag boards enable users to leave a short message on your site without having to go through the trouble of registering. From a development point of view, they are actually rather simple to develop. In this article we will create a quick and easy tag board for any web site. We will be taking advantage of the php and MySQL technologies.

TABLE OF CONTENTS:
  1. Building a Quick and Easy Tag Board
  2. Did Anyone Say MySQL?
  3. Our Tag Board Script
  4. What Makes Our Tag Board Tick?
  5. Our Tag Board Functions!
  6. Retrieval and Sorting of Tags
  7. Results
  8. doBoard() Function
  9. doInsert() Function
  10. Ensuring Data Submitted Does Not Fail
  11. Inserting into the Tag Board
By: Haiden Taylor
Rating: starstarstarstarstar / 120
November 03, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The retrieval and sorting of the tags is done with the following code:

/* Connect to Database */
$conx = mysql_connect(HOST, USER, PASS) or die("Unable to connect to MySQL Server");
mysql_select_db(DB) or die("Unable to select Database");

/* The MySQL Query */
$stSql = "SELECT tag_name, tag_url, tag_entry, tag_date FROM tagboard ORDER BY tag_date DESC";

/* Submit Query to MySQL and check results */
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." – Action Aborted"; exit(); }

The first section we are connecting to our MySQL Database, taking advantage of our defined constants.

Secondly we set the SQL query we will use to retrieve the name of the user who posted the tag “tag_name”, the users website URL “tag_url”, the tag itself “tag_entry” and the date the tag was entered into the database “tag_date”. We then ask MySQL to retrieve the results from the “tagboard” table and ask MySQL to order the results by the date submitted “tag_date”.

And finally we submit the result to MySQL for processing. If an error is encountered it will be picked up, displayed to the user in a controlled fashion and then the script will be aborted.

Now we will look at the displaying of the tags in a formatted fashion:

/* Check wether MySQL returned 1 or more rows */
if (mysql_num_rows($arrResult) >= 1)
{
/* loop through rows – strip any slashes – display formatted tags */
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>
<?
}
/* If no rows were returned by the query, let the user know */
} else {
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">No entries to display</p>
<?
}



 
 
>>> More PHP Articles          >>> More By Haiden Taylor
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: