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

Our Tag Board Script - 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

Our tag board is made up of three functions, four defined constants, a switch statement and a nifty little comparison operator to help with those of you using error-reporting E_ALL.

So let's have a look at the complete version of our script, and then we will go through and look at the script with greater detail. Our Tag Board Script can be found below:

<?
define('HOST', 'localhost');
define('USER', 'mysql_username');
define('PASS', 'mysql_pass);
define('DB', 'mysql_db');

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>
<?
}
}

function doBoard()
{
?>
<html>
<head>
<title>DevShift.com Tag Board</title>
<style>
body {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF;
}
td {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; color: #666666;
}
.inputform {
BORDER-RIGHT: #efefef 1px solid; BORDER-TOP: #000000 1px solid; FONT: 11px Verdana; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #efefef 1px solid; BACKGROUND-COLOR: #efefef
}
.inputformsub {
BORDER-RIGHT: 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; FONT: 11px Verdana; BORDER-LEFT: 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: 1px outset; BACKGROUND-COLOR: #dedede
}
</style>
</head>
<body>
<form action="board.php?do=Insert" target="tag_box" method="POST">
<table width="150" height="250" bgcolor="#FFFFFF" style="border: 1px solid #000000">
<tr>
<td height="150"><IFRAME name="tag_box" height="150" width="150" src="board.php?do=Tags" frameborder="0"></IFRAME></td>
</tr>
<tr>
<td height="100" align="center">Name:<br><input type="text" name="__name" size="20" maxlength="55" class="inputform"><br>URL:<br><input type="text" name="__url" size="20" maxlength="255" value="http://" class="inputform"><br>Your Entry:<br><input type="text" name="__entry" size="20" maxlength="400" class="inputform"><br><input type="submit" value="Submit Entry" class="inputformsub"></td>
</tr>
</table>
</form>
</body>
</html>
<?
}

function doInsert($__name, $__entry, $__url)
{
$conx = mysql_connect(HOST, USER, PASS) or die("Unable to connect to MySQL Server");
mysql_select_db(DB) or die("Unable to select Database");

$__name = addslashes($__name);
$__url = addslashes($__url);
$__entry = addslashes($__entry);

if (empty($__name))
{
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Name Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
} elseif (empty($__entry))
{
?>
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Entry Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
} else {

$stSql = "INSERT INTO tagboard(tagId, tag_name, tag_url, tag_entry, tag_date) values('', '$__name', '$__url', '$__entry', ".time().")";
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." Action Aborted"; exit(); }

if (mysql_affected_rows() == 1)
{
?>
<meta http-equiv="refresh" content="3;url='board.php?do=Tags'">
<?
} else {
echo "Unable to Add Entry to Database";
?>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
<?
}
}
}

$do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

switch ($do) {
case "Board":
doBoard();
break;
case "Tags":
doTags();
break;
case "Insert":
doInsert($_POST['__name'], $_POST['__entry'], $_POST['__url']);
break;
default:
doBoard();
}
?>




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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: