Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

daily bulletin

Status
Not open for further replies.

vietboy505

Technical User
Feb 20, 2006
56
US
I am new to PHP.
I want to start from stratch for a script to have box of Daily Bulletin like a shout box.

Any users can post message on the web page. Maybe like a box of daily bullentin, as time passed, it will gray out (strikethrough) like after couple of days or weeks.

There is an option of admin which can archive it, goes into a .txt file or mySQL database, or gray out. Any help would be appreciate. Thanks.

Some features:
-No registratin involved
-Any one can read/post it
-Specific password for admin
--Archive by pressing a button which embed to a TXT file or store in the mySQL database

Can I have some snipet codes to start?
 
Vietboy:
i had a little time this morning and have put something together to get you started.
the admin user and password is set in the body of the code but starts as being "user" and "password"
hope it helps
remember to set permissions ok in the directory where the textfiles are to be stored. the inet user (depends on OS who this is) must have read/write permissions on the files in question
sorry this code is so long!
Code:
<?
session_start();
$filename = "vietboy.csv";
$messages = array();
//first do the read
if (isset($_GET['admin'])):
	loginform();
	exit;
elseif(isset($_POST['login'])):
	$name="user";
	$pwd = "password";
	if (($_POST['name'] === $name) && ($_POST['pwd'] === $pwd)):
		$_SESSION['loggedin'] = TRUE;
		adminpage();
	endif;
	exit;
elseif (isset($_SESSION['loggedin'])):
	if (isset($_POST['archive'])):
		archive();
	endif;
	unset($_SESSION['loggedin']);
	unset($_POST);
	header("Location:".$_SERVER['PHP_SELF']);
elseif (isset($_GET['logout'])):
	unset($_SESSION['loggedin']);
	header("Location:".$_SERVER['PHP_SELF']);
	exit;
elseif(isset($_POST['submit'])):
	writeshout();
	unset($_POST);
	header("Location:".$_SERVER['PHP_SELF']);
else:
	shoutbox();
endif;     
	

function doctype() { ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Vietboy's Shout Box</title>
<style type="text/css">
/*General styles*/
body {
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:11px;
	color:#999999;
}
/*layout styles */
div.formcontainer {
	width: 460px; 
	padding: 5px;
	margin: 0px auto;
	background-color:#CCFFCC;
}
/* styles for the form */
div.row {
  clear: both;
  padding-top: 10px;
  }
div.row span.label {
  float: left;
  width: 100px;
  text-align: right;
  }
div.row span.formw {
  float: right;
  width: 335px;
  text-align: left;
  } 
div.row textarea {
	width:95%;
	height: 10ex;
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:10px;
	color:#006699;
}
div.row .textbox {
	width:95%;
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:10px;
	color:#006699;
}
form .shoutboxfieldset {
	border:1px dashed #CCCCCC;
}
form .formbutton {
	background-color:#ccffcc;
	border: 1px dashed #CCCCCC;
	color: #FF0000;
	cursor:pointer; 
}
form legend {
	font-size:larger;
	font-weight:bold;
	color:#FF0000;
}
/* end formstyles */

/*content*/
.contentwrapper {
	width: 460px; 
	padding: 5px;
	margin: 0px auto;
	background-color:#CCFFCC;
	margin-top: 50px;
}
.messagesectiontitle {
	font-size:larger;
	font-weight:bold;
	color:#FF0000;
}
.freshmessage, .inthelastweek, .intehlastmonth, .veryold {
	margin-top:5px;
	border-width:	1px;
	border-style:dashed;
}
.freshmessage {
	
}
.inthelastweek {
	background-color:#99CCCC;
}
.inthelastmonth {
	background-color:#99CCFF;
}
.veryold {
	background-color:#33CCFF;
}
.incontentmessagetitle {
	font-weight:bold;
}
.message {
	
}
</style>
</head>
<? } 
function shoutbox () { 
	global $filename;
	$fh = fopen($filename, "rt") or die("cannot open the file for reading");
	while (($messages[] = fgetcsv($fh, 2048)) !== FALSE):
	//do nothing
	endwhile;
doctype();
?>

<body>
<div class="formcontainer">
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="application/x-[URL unfurl="true"]www-form-urlencoded">[/URL]
		<fieldset class="shoutboxfieldset">
		<legend>Shout Box!</legend>
		<div class="row">
			<span class="label">
				Your Name:
			</span>
			<span class="formw">
				<input class="textbox" type="text" value="" name="name" />
			</span>
		</div>
		<div class="row">
			<span class="label">
				Your Message:
			</span>
			<span class="formw">
				<textarea name="message"></textarea>
			</span>
		</div>
		<div class="row">
			<span class="formw">
				<input class="formbutton" type="submit" name="submit" value="Shout!" />
				<input class="formbutton" type="reset" name="reset" value="Clear!" />
			</span>
		</div>
		</fieldset>
	</form>
</div>
<div class="contentwrapper">
<div class="messagesectiontitle">Messages:</div>
<? 	$cnt = count($messages); 
	for ($i=$cnt; $i>0; $i--):
	$message = $messages[$i];
	if ($message[0] > strtotime("yesterday")):
		$class="freshmessage";
	elseif ($message[0] > strtotime("one week ago")):
		$class="inthelastweek";
	elseif ($message[0] > strtotime("one month ago")):
		$class="inthelastmonth";
	else:
		$class="veryold";
	endif;
	
	if (!empty($message[2])):
?>

	<div class="<?=$class?>">
		<div class="incontentmessagetitle">
			Message left by <?=$message[1]?> on <?=date("d-m-Y h:m:s",$message[0])?>
		</div>
		<div class="message">	
			<?=nl2br($message[2])?>
		</div>
	</div>

<? 	
	endif;
	endfor; 
?>
</div>
<div class="contentwrapper">
	<div class="formcontainer">
		<form methd="get" action="<?=$_SERVER['PHP_SELF']?>">
			<div class="row">
				<span class="formw">
					<input class="formbutton" type="submit" name="admin" value="Admin Site!" />
				</span>
			</div>
			<div class="row">&nbsp;</div>
		</form>
	</div>
</div>
</body>
</html>
<? } 

function loginform() { 
doctype();?>
?>
<body>
<div class="formcontainer">
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="application/x-[URL unfurl="true"]www-form-urlencoded">[/URL]
		<fieldset class="shoutboxfieldset">
		<legend>Login to the Admin Site</legend>
		<div class="row">
			<span class="label">
				Your Name:
			</span>
			<span class="formw">
				<input class="textbox" type="text" value="" name="name" />
			</span>
		</div>
		<div class="row">
			<span class="label">
				Your Password:
			</span>
			<span class="formw">
				<input class="textbox" type="password" value="" name="pwd" />
			</span>
		</div>
		<div class="row">
			<span class="formw">
				<input class="formbutton" type="submit" name="login" value="Login!" />
				<input class="formbutton" type="reset" name="reset" value="Clear!" />
			</span>
		</div>
		</fieldset>
	</form>
</div>
</body>
</html>
<? exit; } 

function adminpage() {
	session_start();
	doctype();
	?>
	<body>
<div class="formcontainer">
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="application/x-[URL unfurl="true"]www-form-urlencoded">[/URL]
		<fieldset class="shoutboxfieldset">
		<legend>Archive the Shout Box!</legend>
		<div class="row">
			<span class="label">
				Archive From:
			</span>
			<span class="formw">
				<select name="day">
				<? for($i=1; $i<=31; $i++): ?>
				 	<option value=<?=$i?>><?=$i?></option>
				<? endfor; ?>
				</select>&nbsp;
				<select name="month">
				<? for($i=1; $i<=12; $i++): ?>
				 	<option value=<?=$i?>><?=date("M",strtotime("2000-$i-1"))?></option>
				<? endfor; ?>
				</select>&nbsp;
				<select name="year">
				<? for($i=2006; $i<=2010; $i++): ?>
				 	<option value=<?=$i?>><?=$i?></option>
				<? endfor; ?>
				</select>
			</span>
		</div>
		<div class="row">
			<span class="formw">
				<input class="formbutton" type="submit" name="archive" value="Archive!" />
				<input class="formbutton" type="reset" name="reset" value="Clear!" />
			</span>
		</div>
		</fieldset>
	</form>
</div>
</body>
</html>
<?
}
function archive() {
	global $archive;
	global $filename;
	$archivefilename = "archive_".$filename;
	$archivetime = strtotime($_POST['year']."-".$_POST['month']."-".$_POST['day']);
	$fh = fopen($filename, "rt") or die("cannot open the file for reading");
	while (($messages[] = fgetcsv($fh, 2048)) !== FALSE):
	//do nothing
	endwhile;
	fclose;
	$fcurrent = fopen($filename, "wt") or die("cannot open the file for writing");
	$fold = fopen($archivefilename, "at") or die("cannot open the file for writing");
	foreach ($messages as $message):
		if($message[0] > $archivetime):
			fwrite($fcurrent, "\"".$message[0]."\",\"".$message[1]."\",\"".$message[2]."\"\n");
		else:
			fwrite($fold, "\"".$message[0]."\",\"".$message[1]."\",\"".$message[2]."\"\n");
		endif;
	endforeach;
	fclose($fcurrent);
	fclose($fold);
}
function writeshout() {
	if (!get_magic_quotes_gpc()):
		$time = strtotime("now");
		$name = trim($_POST['name']);
		$message = trim($_POST['message']);
	else:
		$time = strtotime("now");
		$name = stripslashes(trim($_POST['name']));
		$message = stripslashes(trim($_POST['message']));
	endif;	
	//now do the write
	$fh = fopen($filename, "at") or die("cannot open the file for writing");
	fwrite($fh, "\"$time\",\"$name\",\"$message\"\n");
	fclose;
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top