i'm writing a joke page for a website. They have no mysql so i'm using flat .txt files to store the jokes. ex. redneck.txt, blonde.txt, etc.
Heres the code for the main page
<head>
<title>Joke Database - Tons of <?print("$category"); //category name?> Jokes!</title>
<basefont face="Verdana, Arial" size="2"><?// basefont for the database?>
</head>
<body bgcolor="#ffffff" text="#000000" link="#6495ED" vlink="#6495ED" alink="#6495ED" leftmargin="10" marginwidth="10" topmargin="10" marginheight="0">
<?
?>
<table border="0" width="400" cellpadding="2" cellspacing="1">
<tr><td bgcolor="#6495ED"><font size="-1">Category</font></td>
<td bgcolor="#6495ED"><font size="-1">Random</font></td>
<td bgcolor="#6495ED"><font size="-1">List All Jokes</font></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Little Johnny</font></td>
<td><a href="index.php?category=littlejohnny&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=littlejohnny&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Blonde</font></td>
<td><a href="index.php?category=blonde&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=blonde&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Mommy, Mommy!</font></td>
<td><a href="index.php?category=mommymommy&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=mommymommy&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Pickup Lines</font></td>
<td><a href="index.php?category=pickuplines&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=pickuplines&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Clinton</font></td>
<td><a href="index.php?category=clinton&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=clinton&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Yo' Mamma</font></td>
<td><a href="index.php?category=mamma&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=mamma&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Windows 98 Error Messages</font></td>
<td><a href="index.php?category=win98errors&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=win98errors&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Oxymorons</font></td>
<td><a href="index.php?category=oxymorons&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=oxymorons&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">101 Uses For Aol Disks</font></td>
<td><a href="index.php?category=aoldisks&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=aoldisks&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Computer</font></td>
<td><a href="index.php?category=computer&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=computer&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">Your A Redneck If...</font></td>
<td><a href="index.php?category=youraredneckif&style=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?category=youraredneckif&style=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
<tr>
<td bgcolor="#E6E6E6"><font size="-1">User Submitted</font></td>
<td><a href="index.php?test=test&type=random"><b><font size="-1">Random</font></b></a></td>
<td><a href="index.php?test=test&type=all"><b><font size="-1">List All Jokes</font></b></a></td>
</tr>
</table>
<br><hr>
<?
//CODE COPYRIGHT 2001; Mike Schroeder
if(69 == 69){ //allows error / instructions to be displayed
if($style == random) //selected random joke
{
$lj = file("$category.txt"); //external array file
srand ((float) microtime() * 10000000); //feed the random number generator
$random_joke = array_rand ($lj, 2); // pull a random joke out of the array
print $lj[$random_joke[0]]."\n"; // print random joke
}
if($style == all)//selected list all files
{
$lj = file("$category.txt"); //external array file
while(list($k, $v) = each($lj)) //loop - lists all jokes
{
print("<br><br>". $v); //prints loop
}
}
}
else //Beginning instructions and error message
{
print("Please Select A Joke Category And If You Want A Random Joke Or To See All The Jokes In That Particular Category, But Be Warned Some Of The Databases Are Very Big.");
}
?>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write('<form><input type=button value="Refresh Page" onClick="history.go()"></form>')
// End -->
</script>
</body>
that works fine... but i want to allow people to submit jokes to the category of thier choice. I made a form with a text area that add a line to a test database but the array reads newlines as entries and screws up the random feature. I was wondering how to strip all the new lines and format the submitted joke or read every other entry in the array.
here is the addjoke.php source
<head><title></title></head>
<body>
<font size="+2">Submit Joke</font>
<form action="addjoke.php" method="post">
<textarea name="joketext" cols="30" rows="5">
</textarea>
<input type="submit" value="Submit!" />
</form>
<?
$file_path = "test.txt"; // file path
$fileopen = @fopen($file_path, "r"); //read file into an array
if (!$fileopen) { echo "Can't open file"; }
for ($i=0; $buffer = fgets($fileopen, 16192); $i++) {
$arr_file[$i] = $buffer;
}
fclose($fileopen);
$str_toadd = "\"" . "$joketext" . "\","; //write this to database
$fileopen = @fopen($file_path, "w");//
for ($i=0; $i < sizeof($arr_file); $i++) { //
fwrite($fileopen, $arr_file[$i], 16192); // Does the writing
}//
fwrite($fileopen, $str_toadd, 16192); //
fclose($fileopen);//
?>
</body>