Shoutbox

Page 1 of 1

1 Replies - 421 Views - Last Post: 17 December 2009 - 10:01 PM

#1 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Shoutbox

Posted 09 July 2007 - 04:41 PM

Description: Create the following SQL table:
CREATE TABLE shoutbox (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50),
website VARCHAR(150),
content VARCHAR(255),
PRIMARY KEY (id)
);A simple shoutbox script.
<?php
//------ Edit Below This Line ------//

define('SHOUTBOX', 'shoutbox');

$host = 'localhost';
$dbuser = 'username'; // Username to access database.
$dbpass = 'password'; // Password to access database.
$dbname = 'database'; // Database name.

$allowed_tags = '<b><i><u><a>';

//------ Edit Above This Line ------//

mysql_connect($host, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

function clean_string($string, $tags = '')
{
  $string = trim($string);
  $string = str_replace("'", "''", $string);
  return $string;
}

?>
<style type="text/css">
.shoutbox {
margin: 0px;
padding: 0px;}

.shoutbox a, a:visited {
color: 6E7A8A;
font-size: 12px;
font-weight: bold;
text-decoration: none;}

.shoutbox a:hover {
color: 6E7A8A;
text-decoration: underline;}

.shoutbox input, select, textarea {
border: 1px solid silver;}

.form {
font-size: 12px;}

#name1 {
background-color: E0E0E0;
color: 666666;
font-size: 12px;}
#content1 {
background-color: E0E0E0;
font-size: 12px;}

#name2 {
background-color: F3F3F3;
color: 666666;
font-size: 12px;}
#content2 {
background-color: F3F3F3;
font-size: 12px;}
</style>
<?php

switch($_GET['p'])
{
  case 'box':
    echo "<body class="shoutbox">n<table cellpadding="2">";

    $result = mysql_query("SELECT * FROM " . SHOUTBOX . " ORDER BY id DESC") or die(mysql_error());
    for ($i = 1; $i <= ($row = mysql_fetch_assoc($result)); $i++)
    {
      $nameid = (!($i % 2)) ? 'name1' : 'name2';
      $contentid = (!($i % 2)) ? 'content1' : 'content2';

      $name = strip_tags(stripslashes($row['name']), $allowed_tags);
      $website = strip_tags(stripslashes($row['website']), $allowed_tags);
      $content = strip_tags(stripslashes($row['content']), $allowed_tags);

      echo '<tr><td id="' . $nameid . '" width="50" align="right">',
      ((!empty($row['website'])) ? "<a href="$website">$name</a>" : $name),
      '</td><td id="' . $contentid . '" width="170">' . $content . "</td></tr>n";
    }

    echo "</table>n</body>";
  break;

  default:
    echo '<div class="shoutbox">';

    if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['name']) && !empty($_POST['content']))
    {
      $name = clean_string($_POST['name']);
      $website = clean_string($_POST['website']);
      $website = (!empty($website) && $website != 'http://') ? (!eregi('^http://', $website) ? 'http://' . $website : $website) : '';
      $content = nl2br(clean_string($_POST['content'], '<a>'));

      mysql_query("INSERT INTO " . SHOUTBOX . " (name, website, content) VALUES ('$name', '$website', '$content')") or die(mysql_error());
    }

    echo '<iframe src="' . basename($_SERVER['PHP_SELF']) . '?p=box" width="260" height="285"></iframe>
    <form method="post"><table class="form">
    <tr><td align="right">Name:</td><td><input type="text" name="name" size="10"></td></tr>
    <tr><td align="right">Website:</td><td><input type="text" name="website" value="http://" size="10"></td></tr>
    <tr><td align="right">Message:</td><td><textarea name="content" rows="3" cols="20"></textarea></td></tr>
    <tr><td colspan="2" align="center"><input type="submit" value="Submit"></td></tr>
    </table></form>', "n</div>";
  break;
}

/*
CREATE TABLE shoutbox (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50),
website VARCHAR(150),
content VARCHAR(255),
PRIMARY KEY (id)
);
*/
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Shoutbox

#2 jagdish.k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 17-December 09

Re: Shoutbox

Posted 17 December 2009 - 10:01 PM

Excellent code dude(s)!. Fantastic
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1