tagwall.php//
<?php
include_once("tagwall.class.php");
$tagwall = new Tagwall;
$tagwall->setMethod(0);
$tagwall->setFile("tagwall.txt");
if ($action == "write") {
print '
<form action="' . basename($_SERVER['PHP_SELF']) . '" method="post">
<input type="hidden" name="action" value="dowrite">
<b>Navn:</b> <input type="text" name="name"><br>
<b>Dato:</b> <input type="text" name="email"><br>
<br>
<textarea name="text" rows="10" cols="76"></textarea><br>
<input type="submit" value="Gem">
</form>
';
} else {
if ($action == "dowrite") {
$tagwall->insertTagwallData($name, $email, $text);
}
print '<a href="' . basename($_SERVER['PHP_SELF']) . '?action=write">Skriv i tagwallen</a><br><br>';
$data = $tagwall->fetchTagwallData();
foreach ($data as $key => $value) {
print "<b>Navn:</b> " . $data[$key]["name"] . "<br>
<b>Dato:</b> " . $data[$key]["email"] . "<br>
<b>Besked:</b> " . $data[$key]["text"] . "<hr>";
}
}
?>
tagwall.class.php//
<?php
/**
Taggwall class for use with text file or mySQL database.
This tagwall class was developed by Logan Raarup 15. may 2002.
Please see example.php for a demonstration of this tagwall with
use of text file for saving tagwall data.
Feel free to contact me at: logan@logan.dk
Or visit my website at:
http://www.logan.dk */
class Tagwall {
var $ERRORS;
var $txtfile;
var $dbinfo = array();
var $method = 0;
/**
* This function sets the file where tagwall data will be saved
* REMEMBER TO GIVE THIS FILE WRITE PERMISSION (chmod 777)
*
* @param $file name of text file to save tagwall info
*/
function setFile($file) {
$this->txtfile = $file;
}
/**
* This function sets the database info for the tagwall data
*
* @param $server hostname of mySQL server
* @param $pass password for mySQL database
* @param $user mySQL username
* @param $db name of the database for the tagwall
*/
function setDBInfo($server, $user, $pass, $db) {
$this->dbinfo = array("server" => $server,
"user" => $user,
"pass" => $pass,
"db" => $db);
}
/**
* This function selects which method to access tagwall data
*
* @param $method set this parameter to 0 = use text file, and 1 = use mySQL database
*
* @return boolean true or false
*/
function setMethod($method = 0) {
if ($method == 0 || $method == 1) {
$this->method = $method;
return true;
} else {
$this->ERROR = "Invalid method.";
return false;
}
}
/**
* This function fetches tagwall data with selected method
*
* @return array tagwall entries
*/
function fetchTagwallData() {
if ($this->method == 0) {
$fp = fopen($this->txtfile, "r");
if ($fp) {
$contents = fread($fp, filesize($this->txtfile));
$contents = explode("|", $contents);
foreach ($contents as $key => $value) {
$contents[$key] = unserialize(&$contents[$key]);
if (!empty($contents[$key]["name"])) {
$data[] = $contents[$key];
}
}
$data = array_reverse($data);
fclose($fp);
return $data;
} else {
$this->ERRORS = "Could not open file.";
return false;
}
} else {
$link = mysql_connect($this->dbinfo["server"], $this->dbinfo["user"], $this->dbinfo["pass"]) or die("Could not connect to mySQL");
mysql_select_db($this->dbinfo["db"], $link);
$result = mysql_query("SELECT * FROM tagwall ORDER BY created_on DESC", $link);
while ($row = mysql_fetch_row($result)) {
$data[] = array("name" => $row["name"],
"email" => $row["email"],
"text" => $row["text"],
"date" => $row["created_on"]);
}
mysql_close($link);
return $data;
}
}
/**
* This function inserts an entry to the tagwall
*
* @param $name name of user
* @param $email users email address
* @param $text text message
*
* @return boolean true or false
*/
function insertTagwallData($name, $email, $text) {
$data = array("name" => $name,
"email" => $email,
"text" => $text,
"date" => time());
$data = serialize($data);
if ($this->method == 0) {
$fp = fopen($this->txtfile, "a");
if ($fp) {
$fp = fwrite($fp, $data . "|");
return true;
} else {
$this->ERRORS = "Could not open file.";
return false;
}
} else {
$link = mysql_connect($this->dbinfo["server"], $this->dbinfo["user"], $this->dbinfo["pass"]) or die("Could not connect to mySQL");
mysql_select_db($this->dbinfo["db"], $link);
mysql_query("INSERT INTO tagwall SET name = '$name', email = '$email', text = '$text', created_on = " . time(), $link);
mysql_close($link);
}
}
}
?>
og husk at oprette et tomt txt dokument.