Hi,
I've taking scripts for a "shout box" type thing from a tutorial and customised it for my site, to be a news poster. It is just file based, and takes what ever is in the html post from and places it into a .db file.
When the news page is loaded, it takes each new line from the db file and posts it in a table on the page.
My problem is this:
Because the scripts take each new line in the DB file as a new entry, if the user creates a new line anywhere in the post, it is entered in to the file in several lines, meaning the entries get split up.
One work around I have found is to just use a <br> tag when posting the news, but for the people posting (who aren’t really very computer savvy) this is a big inconvenience.
Has anyone got any off-hand ideas?
Demo of code
User: Steve Pw: Pyramid
News Posting Script(s) Bug.
Moderator: General Moderators
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
add and strip slashes doesnt seem to work.
Ill past the source for the main parts of the script:
Plug.php: Processes Form and Writes to File
News.php: Reads file and displays news
Ill past the source for the main parts of the script:
Plug.php: Processes Form and Writes to File
Code: Select all
<?php
// Check to see if the user has already plugged...
if($plugged == 2){
echo "<font size='1' face='verdana'><center><br><br>You can't plug more than twice in a day!
</center></font>";
exit();
}else{ $num = $plugged + 1;
setcookie("plugged","$num",time()+86400);
}
// Get required data
$filename = 'plugs.db';
$sitename = stripslashes($_POST['sitename']);
$url = $_POST['url'];
//Set date
$meh = date("l dS of F Y");
echo "<center><font size='1' face='verdana'>";
// Make what is going to be written to the file
$theplug = stripslashes("<table cellpadding=4 cellspacing=0 border=1 width=600 bordercolor=black><tr><td bgcolor=#cccccc><font size=2>News Submited By: <b>$sitename</b> - on $meh</font></td></tr><tr><td>$url<br></td></tr></table>");
$content = "$theplug \n";
// Write it
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit();
}
if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit();
}
echo "<center>Thanks $sitename! You news has been added.<br>Please wait while we redirect you to the news page....</center><meta http-equiv='refresh' content='1; URL=news.php'>";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
echo "</font></center>";
?>Code: Select all
<?php
echo '<font face=verdana size=1>';
$lines = file("plugs.db");
$linesr = array_reverse($lines);
$pamount = '6';
$so = sizeof($linesr);
if ($so > $pamount) {
$so = $pamount;
}
foreach($linesr as $key => $val) {
$data[$key] = $val;
}
for($K = 0; $K < $so; $K++) {
echo '<b>'.$data[$K].'</a></b><br>';
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
probably at either the echo line with $data[$K] or during the foreach just prior...
personally, I'd toss it through array_map()
personally, I'd toss it through array_map()
Code: Select all
$linesr = array_map('stripslashes',array_reverse($lines));