|
|
|
|
![]() ![]() |
Jun 23 2007, 08:56 AM
Post
#1
|
|
|
Premium Member Group: [HOSTED] Posts: 286 Joined: 17-June 07 From: Tasmania Member No.: 22,699 |
Hi everyone, I'm going to tell you how to make a simple shoutbox using PHP and MySQL.
To start off, open up mysql in the command line, or phpmyadmin, and create a database called shoutbox. Next, enter the following sql into the command line, or the phpmyadmin sql box, while using the shoutbox database: CODE create table messages(author varchar(30), message text, time timestamp, mid int auto_increment, primary key(mid)); This creates the table we need to store the messages, note the "mid" column, this gives each message a seperate id number, we'll see why that's useful later. Next, create a new PHP page on your server, lets call it shoutbox.php. Copy this code into it: CODE <html> <head> <title>My Shoutbox</title> </head> <body> <center><b>My Shoutbox</b></center> <center><table width="80%"> <!-- Shoutbox update --> <!-- Shoutbox update --> <!-- Shoutbox posts --> <!-- Shoutbox posts --> <!-- Shoutbox form --> <!-- Shoutbox form --> </table></center> </body> </html> Ok, now we start on the actual shoutbox. In between the "<!-- Shoutbox form -->" lines, type the following text, we're going to make the shoutbox form. CODE <tr><td><form name="shoutbox" action="?action=shout" method="POST"><label for="name">Name:</label><input type="text" name="name"><br> <textarea name="message" cols="20" rows="2"><br><input type="submit" value="Shout!"></form></td></tr> Ok, this gives us a place to write a name, and a message. Note that in the "action" attribute of the form, I've put "?action=shout", if the page is called shoutbox.php, it will take us to shoutbox.php?action=shout. This uses a GET variable, which we will talk about below. Now, we're going to make the section that updates the shoutbox. This is where we get into some actual PHP coding! Between the "<!-- Shoutbox update -->" lines, type the following text: CODE <?php mysql_connect("hostname","username","password"); mysql_select_db("shoutbox"); if ($_GET["action"]=="shout") { $update = "insert into messages values('".$_POST["name"]."','".$_POST["message"]."',NULL,NULL);"; mysql_query($update) or die("Failed to update database. The error returned was:<br>".mysql_error()); } ?> Not much, is it? But it does what we need. First, it connects to mysql. Change the text in bold to your own mysql hostname, username and password. Then, it selects the database. If your database isn't called shoutbox, change this bit. Then, it checks to see if the GET variable action is set to "shout." Get variables are those things you sometimes see in urls, you know, when there's a ?something=something&hello=whatever after the address. These are get variables. In this example, submitting the form takes you to shoutbox.php?action=submit, so the action GET variable is set to "shout." Next, it performs the MySQL query, which inserts the data from the form into the shoutouts table. There's also a bit of code to show us whet the problem is if it doesn't work. Ok, now for the final bit, the posts. In between the "<!-- Shoutbox posts -->" lines, write the following text: CODE <?php $query = "select * from messages order my mid desc"; $result = mysql_query($query); while ($array=mysql_fetch_array($result)) { echo "<tr><td>".$array["message"]."<br><font size=\"small\">Posted on ".date("D j/n/Y g:i:s a",$array["time"])." by ".$array["name"]."</font></td></tr>"; } ?> Ok, what this does, is it starts off by getting the data from the database, and ordering it by it's unique id, descending. It then writes the information into a table cell once for every entry in the table. The date function makes a readable date out of the time from the database. By now, the page should look like this: CODE <html> <head> <title>My Shoutbox</title> </head> <body> <center><b>My Shoutbox</b></center> <!-- Shoutbox update --> <?php mysql_connect("hostname","username","password"); mysql_select_db("shoutbox"); if ($_GET["action"]=="shout") { $update = "insert into messages values('".$_POST["name"]."','".$_POST["message"]."',NULL,NULL);"; mysql_query($update) or die("Failed to update database. The error returned was:<br>".mysql_error()); } ?> <!-- Shoutbox update --> <center><table width="80%"> <!-- Shoutbox posts --> <?php $query = "select * from messages order my mid desc"; $result = mysql_query($query); while ($array=mysql_fetch_array($result)) { echo "<tr><td>".$array["message"]."<br><font size=\"small\">Posted on ".date("D j/n/Y g:i:s a",$array["time"])." by ".$array["name"]."</font></td></tr>"; } ?> <!-- Shoutbox posts --> <!-- Shoutbox form --> <tr><td><form name="shoutbox" action="?action=shout" method="POST"><label for="name">Name:</label><input type="text" name="name"><br> <textarea name="message" cols="20" rows="2"><br><input type="submit" value="Shout!"></form></td></tr> <!-- Shoutbox form --> </table></center> </body> </html> And there you have it! Your very own shoutbox! Feel free to style is, and modify it anyway you want. You can add validation code, and functions to delete posts! - Jay |
|
|
|
Jul 2 2007, 01:16 PM
Post
#2
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 27 Joined: 1-July 07 Member No.: 23,058 |
Thankx a lot. But i have a doubt, i have seen many free shoutbox script available on the web, but while shouting the iframe containing the shoutbox is refreshed. While on some forums like here there is no refreshing done, rather it gets shouted in a cool way and instantly.
Is there any free script for such a cool shoutbox? |
|
|
|
Jul 3 2007, 12:49 AM
Post
#3
|
|
|
Teh Coder Group: Members Posts: 1,053 Joined: 18-April 06 From: Australia Member No.: 12,833 myCENTs:89.25 |
It would be good if you added an alternate explanation of how you would build the database/tables(s) through the interface method of phpMyAdmin (rather than executing a direct mySQL query). Just a thought
Ummm self refreshing shoutboxes, I think mine does that, or maybe it refreshes the page, I can't remember exactly but it updates pretty much on the fly. |
|
|
|
Jul 3 2007, 01:56 AM
Post
#4
|
|
|
NiGHTFoX - Hiding in the dark Group: Members Posts: 680 Joined: 3-April 05 Member No.: 3,584 |
It would be good if you added an alternate explanation of how you would build the database/tables(s) through the interface method of phpMyAdmin (rather than executing a direct mySQL query). Just a thought Ummm self refreshing shoutboxes, I think mine does that, or maybe it refreshes the page, I can't remember exactly but it updates pretty much on the fly. Yeah, those are very good points. Maybe even just setting up the tables in a text file [N]F |
|
|
|
Jul 3 2007, 05:56 AM
Post
#5
|
|
|
Premium Member Group: [HOSTED] Posts: 286 Joined: 17-June 07 From: Tasmania Member No.: 22,699 |
Yeah, I use JavaScript for mine, I'll dig it out of the code for you
Ok, place this anywhere on the page. CODE <script language="JavaScript"> var refreshinterval=120 var displaycountdown="yes" var starttime var nowtime var reloadseconds=0 var secondssinceloaded=0 function starttime() { starttime=new Date() starttime=starttime.getTime() countdown() } function countdown() { nowtime= new Date() nowtime=nowtime.getTime() secondssinceloaded=(nowtime-starttime)/1000 reloadseconds=Math.round(refreshinterval-secondssinceloaded) if (refreshinterval>=secondssinceloaded) { var timer=setTimeout("countdown()",1000) if (displaycountdown=="yes") { window.status="Page refreshing in "+reloadseconds+ " seconds" } } else { clearTimeout(timer) window.location.reload(true) } } window.onload=starttime </script> Change refreshinterval to however many seconds you want it to wait before it reloads, also, it tells you how many seconds left until it reloads in the status bar, so it doesnt catch you by surprise and make you write the message all over again. Also, I'll have a fiddle with PHPMyAdmin tonight, and write up something on how to create the tables in it, but for the meantime, you can just go to the "SQL" tab in phpmyadmin (It's there in every version I've used) and copy and paste the SQL code. This post has been edited by Habble: Jul 3 2007, 05:59 AM |
|
|
|
Jul 3 2007, 01:13 PM
Post
#6
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 27 Joined: 1-July 07 Member No.: 23,058 |
Thankx for the "self-refreshing" one. But can anyone provide me the script for a shoutbox like the one here. When we make a shout, it gets shouted like an im and the page(or iframe) don't need to get refreshed. The shoutbox in astahost is very cool. I want the script for a shoutbox for my site, which i expect, that will soon be hosted here
|
|
|
|
Jul 3 2007, 01:16 PM
Post
#7
|
|
|
Teh Coder Group: Members Posts: 1,053 Joined: 18-April 06 From: Australia Member No.: 12,833 myCENTs:89.25 |
Self refreshing is the really the only way to go I think?
It usually works out well because the content of the page will be cached anyway, so it's quick, I'm not sure exactly how I do it but it seems to be an immediate on screen update (I'll have to check when I have time), but if things like queries on pages are a problem, try using a form of caching pages to help that across the board if it's feasible. |
|
|
|
Jul 3 2007, 01:37 PM
Post
#8
|
|
|
the Q Group: [HOSTED] Posts: 1,094 Joined: 13-July 05 From: Lithuania, Vilnius Member No.: 7,059 myCENTs:70.96 |
This one here I think is fully made with AJAX technology, I mean the shoutbox on astahost forums.. and it is really a bit more advanced than this simple shoutbox.. I never really liked iframes or any frames, it is really better to use something else these days, especially than using AJAX you can do really a lot of good stuff, a lot can be done using a browser these days
|
|
|
|
Jul 12 2007, 03:15 AM
Post
#9
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 2 Joined: 12-July 07 Member No.: 23,307 |
whats the best use for a shoutbox, ive never used one before
|
|
|
|
Jul 12 2007, 06:35 AM
Post
#10
|
|
|
Premium Member Group: [HOSTED] Posts: 286 Joined: 17-June 07 From: Tasmania Member No.: 22,699 |
Shoutboxes are like chat-rooms. you can put it on a website, and people can chat with them. Like the one at the top of the forum.
|
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 22nd November 2008 - 02:20 PM |