Welcome, Guest

Please login or register

TUTORIALS SUBMENU ---->

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING

Related Links

The Complete Shoutbox Tutorial

pages (3): [1] 2 3


Now that you know the basics of PHP and MySQL connectivity, its time to add a little interaction with this cool little applet that allows your users to leave short messages - a system commonly referred to as a shoutbox.  In this tutorial I will detail everything you need to know about making your very own shoutbox, right down from the HTML form, to the smilie emoticon configuration and anti-spam measures.

You will need the following to be able to complete this tutorial:

  • A PHP enabled webserver.
  • A mySQL database.
  • An hour of free time.
  • Sessions enabled.
  • File uploads enabled.

Getting Started
As a first step we should think about the layout and size of our shoutbox - 150px wide and 500px high is just about right.  I'll refrain from using an iframe in my example because I really don't like them.   We'll also give the shoutbox a 10 entry display limit (i.e. the latest 10 entries will display - the rest will be hidden).   With these considerations aside, we can start by setting up two mySQL tables in our database: shouts and smilies.

shouts has 4 fields:
- id, int(11), auto_increment, PRIMARY
- Name, TEXT
- Shout, TEXT

- Contact, TEXT

smilies has 4 fields:
- id, int(11, auto_increment, PRIMARY
- Symbol, VARCHAR(4)
- URL, TEXT
- Alt, TEXT

If you don't know what this formatting means, int is the type of field, the number in brackets is the size, auto_increment is a feature, and PRIMARY is a type, defining it as unique. Text is a field type, as is VARCHAR, and the number in brackets again is the field length.

With these tables in place you should create your shoutbox directory, and then a subdirectory off it called smilies. CHMOD it to 777 (i.e. all permissions). If you don't know what this means or can't manage it, contact me using the information at the end of this tutorial.

Breakdown of shout.php
There are two pages in our script, shout.php and admin.php - both of which are downloadable here. You can, of course, just grab the scripts and get the shoutbox working without reading the rest of this tutorial, but that would be no fun at all.   For all those people actually interested in learning the workings of this script, though, I'll first break down shout.php and I’ll explain how it works.

// calling session_start() the function which starts our authentication session
session_start();

// connecting to mysql server
$l = mysql_connect ( "localhost" , "user" , "pass" ) or die("Error connecting:<BR><BR>".mysql_error());
mysql_select_db( "user" ) or die("Error getting db:<BR><BR>".mysql_error());

This is the main building block of both scripts. It starts all session registering or management with session_start(), and then selects and connects to our database. You should, of course, edit the mySQL connectivity lines to reflect your own mySQL settings.

function getShouts()
{

echo '<div align="center">
<table width="150" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
';

$query = mysql_query("SELECT * FROM shouts ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_array($query) )
{

$name = stripslashes($row['Name']);
$contact = stripslashes($row['Contact']);
$shout = stripslashes($row['Shout']);

if(empty($contact))
{

echo '<p><span class="author">'.$name.'</span> - <span class="shout">'.$shout.'</span></p>';

} else {

echo '<p><span class="author"><a href="'.$contact.'" target="_blank">'.$name.'</a></span> - <span class="shout">'.$shout.'</span></p>';

} // if empty contact

} // while row mysqlfetcharray query

echo '<br><br>';

echo '
</td>
</tr>
<tr>
<td height="10">&nbsp;</td>
<form name="shout" method="post" action="shout.php">
<div align="center">
<input name="name" type="text" id="name" value="Name" size="25" maxlength="10"><br>
<input name="contact" type="text" id="contact" value="http://" size="25"><br>
<input name="message" type="text" id="message" value="Message" size="25"><br>
<input name="shout" type="submit" id="shout" value="Shout!">
</div>
</form>
</td>
</tr>
</table>
</div>
';

} // function getshouts

This is probably the most important function for the whole script, as it processes and displays the shouts. First of all, this function selects the newest 10 shouts by ordering them by id in descending order. It then cycles through and displays the records. The author and message tags have SPAN classes around them, but I haven’t defined the applicable CSS tags. If you wish to define them, go to line 87 of shout.php where it says /* STARTING THE MAIN SCRIPT NOW */, and add the following code below it:

echo ‘<link href="yourfile.css" rel="stylesheet" type="text/css">’;

In yourfile.css add two classes: author and shout, which are self explanatory as to where they will appear. This will be very useful when you want to format your shouts cohesively.

Now, back to our function. In addition to what I said earlier, it removes the slashes from our database results (because we added some when we posted them), and it checks to see whether or not the contact variable was empty when the user posted. If the user did not specify any contact information, a plain name is displayed without a hyperlink.  If the opposite happens, their name is enclosed within a nice big hyperlink instead.

- Tutorial written by Scrowler

Pages (3): [1] 2 3 Next>
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese
Featured Content

Realistic Water Dr...
Realistic Water Dr...
- Adobe Photoshop -
Type Along A Path
Type Along A Path
- Adobe Photoshop -
Creating Warm Light
Creating Warm Light
- Adobe Photoshop -
Thermoptic Camoufl...
Thermoptic Camoufl...
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Special Options
Download Source File
Printer Friendly Version
Forum Threads

Competition Discussion - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:48pm
Activity: 0 replies, 53 views
 Competition - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:46pm
Activity: 0 replies, 54 views
 PM Spamming
Author: Tamlin
Posted: Feb 06th, 1:24pm
Activity: 7 replies, 115 views
Vector Clipart Bank
Author: Crapoun
Posted: Feb 06th, 11:29am
Activity: 2 replies, 93 views
How did ...
Author: MoodsR4Cattle
Posted: Feb 05th, 6:09pm
Activity: 6 replies, 26 views
Tips and trick for Texturing/Materials
Author: noorjan
Posted: Feb 05th, 4:59am
Activity: 2 replies, 108 views
 A Billion Styles - Please Help Me!!
Author: Angelz
Posted: Feb 03rd, 6:36pm
Activity: 2 replies, 133 views
101 Things you didnt know in 3DS Max ...in fact...
Author: noorjan
Posted: Jan 31st, 6:04pm
Activity: 0 replies, 160 views
Pee Wee get's an IPad
Author: MoodsR4Cattle
Posted: Jan 30th, 4:25pm
Activity: 2 replies, 163 views
Spam :: Online hotel reservations for Hotels in...
Author: kieulinh
Posted: Jan 28th, 6:39am
Activity: 0 replies, 204 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 3 replies, 17 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 27 replies, 727 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    415