I'm new to PHP and I usually get by with the help of the manual. I've made a blog script based on a tutorial and added some of my own features. I''m currently working on the 'Add and View comments' feature. I just use a flatfile storage since I don't know mySQL yet and I am able to add comments to blog entries (I know coz I checked the actual txt file) but somehow, I cannot view them on the web.
Here are the PHP codes:
----------The Main Blog Script----------
Code: Select all
<?php
$no_comments = 0; //number of comments
$opFile = "blog.txt";
$fp = fopen($opFile,"r");
$data = fread($fp, filesize($opFile));
fclose($fp);
$line = explode("\n", $data);
$i = count($line);
$cmntfile = "comments.txt";
$opncfile = fopen($cmntfile, "r");
$cmntdata = fread($opncfile, filesize($cmntfile));
fclose($opncfile);
for ($n=$i-2 ; $n > -1 ; $n-- )
{
$blog = explode("|", $line[$n]);
if (isset($blog[0]))
{
echo "<h3>Date: ".$blog[0]."</h3>";
echo "<h3>Subject: ".$blog[1]."</h3>";
echo "<h3>Entry:</h3> <p>".$blog[2]."</p>";
echo "<h3>Mood: ".$blog[3]."</h3>";
echo "<img src='$blog[4]'/><br/><br/>";
$identity = $n;
$centries = explode("\n", $cmntdata);
$l = count($centries);
for ($m=$l-2 ; $m > -1 ; $m-- )
{
$comment = explode("|", $centries);
if ($comment[6]==$identity)
{$no_comments = $no_comments + 1;}
}
echo "<form name='viewcomments' action='viewcomments.php' method='POST'>
<input type='text' readonly='readonly' name='identifier' rows='1' cols='1' style='width:11px; font-family:verdana,sans-serif; font-size:10px; background-color:#000000; border-style:none;' value='$n'/>
<input type='submit' name='viewcomments' 'rows='1' cols='4' style='width:155px; font-family:verdana,sans-serif; font-size:10px; color:#999966; background-color:#000000; border-color:#999966;' value='$no_comments Voices Have Spoken'/></form>
<form name='addcomments' action='addcomment.php' method='POST'>
<input type='text' readonly='readonly' name='identifier2' rows='1' cols='1' style='width:11px; font-family:verdana,sans-serif; font-size:10px; background-color:#000000; border-style:none;' value='$n'/>
<input type='submit' name='addcomment' rows='1' cols='4' style='width:65px; font-family:verdana,sans-serif; font-size:10px; color:#999966; background-color:#000000; border-color:#999966;' value='Speak'/></form>";
echo "<br/><hr/><br/>";
}
$no_comments = 0;
}
?>Code: Select all
<?php
$blogfile="blog.txt";
$opnfile = fopen($blogfile,"r");
$contents = fread($opnfile, filesize($blogfile));
fclose($opnfile);
$entries = explode("\n", $contents);
$identity = $_POST['identifier2'];
$blog = explode("|", $entries[$identity]);
if (isset($blog[0]))
{
echo "<h3>Subject: ".$blog[1]."</h3>";
echo "<h3>Entry:</h3> <p>".$blog[2]."</p>";
}
echo "<form action='post_cmnt.php' method='POST' name='postcomment'>
<input type='text' readonly='readonly' name='identified' rows='1' cols='1' style='width:11px; font-family:verdana,sans-serif; font-size:10px; background-color:#000000; border-style:none;' value='$identity'/>
<h3>Name:<h3><input type='text' name='name' size='45' maxlength='40' style='width:350px' tabindex='1' value='' />
<h3>Message:<h3><textarea name='message' rows='10' cols='35' wrap='virtual' style='width:350px' tabindex='3'></textarea>
<h3>Site:<h3><input type='text' name='site' size='45' maxlength='60' style='width:350px' tabindex='4' value='http://' />
<p>Note: If you do not have a site, please leave the above field blank.</p>
<h3>E-mail:<h3> <p><input type='text' name='mail' size='45' maxlength='40' style='width:350px' tabindex='5' value='' /></p>
<p><input type='radio' name='hmail' value='1'/>Hide <input type='radio' name='hmail' value='2'/>Display</p>
<p class='close'><input type='submit' 'rows='1' cols='5' style='width:105px; font-family:verdana,sans-serif; font-size:10px; color:#999966; background-color:#000000; border-color:#999966;' value='I have spoken' name='addcmnt'/></p>
</form>";
?>Code: Select all
<?php
$cmntfile = "comments.txt";
if (!isset($message)) {
$marker = $_POST['identified'];
$name = $_POST['name'];
$message = $_POST['message'];
$site = $_POST['site'];
$mail = $_POST['mail'];
}
switch($_POST['hmail'])
{
case 1:
$check = "hide";
break;
case 2:
$check = "";
break;
}
$name = htmlspecialchars(trim(stripslashes($name)));
$message = htmlspecialchars(trim(stripslashes($message)));
$site = htmlspecialchars(trim(stripslashes($site)));
$mail = htmlspecialchars(trim(stripslashes($mail)));
$message = trim($message);
$postdate = date('d M Y');
$message = str_replace("\n", "<br/>", $message);
$message = str_replace("\r", "", $message);
$message = str_replace("|", "¦", $message);
$cmnt = $postdate."|".$name."|".$message."|".$site."|".$mail."|".$check."|".$marker."|\n" ;
$content = fopen($cmntfile, "a");
fwrite($content, $cmnt);
fclose($content);
echo "<br/>";
echo "<h1>Your comment has been added.</h1>";
?>Code: Select all
<?php
$blogfile = "blog.txt";
$opnbfile = fopen($blogfile,"r");
$contents = fread($opnbfile, filesize($blogfile));
fclose($opnbfile);
$cmntfile = "comments.txt";
$opncfile = fopen($cmntfile,"r");
$cmntcontents = fread($opncfile, filesize($cmntfile));
fclose($opncfile);
$identity = $_POST['identifier'];
$bentries = explode("\n", $contents);
$centries = explode("\n", $cmntcontents);
$i = count($centries);
$blog = explode("|", $bentries[$identity]);
if (isset($blog[0]))
{
echo "<h3>Date: ".$blog[0]."</h3>";
echo "<h3>Subject: ".$blog[1]."</h3>";
echo "<h3>Entry:</h3> <p>".$blog[2]."</p>";
echo "<h3>Mood: ".$blog[3]."</h3>";
echo "<img src='$blog[4]'/><br/><br/>";
echo "<h2>Of they who have spoken...</h2><br/>";
for ($n=0; $n<i; $n++) {
$comment = explode("|", $centries[$n]);
if (isset($comment[0])AND $comment[6]==$identity)
{ echo "<h3>Date: ".$comment[0]."</h3>";
echo "<h3>Name: ".$comment[1]."</h3>";
echo "<h3>Message:</h3> <p>".$comment[2]."</p>";
if ($comment[3]!="")
{echo "<h3>Site:</h3><p>".$comment[3]."</p>";}
if ($comment[5]!="hide")
{echo "<h3>E-mail:</h3><p>".$comment[4]."</p>";}
echo "<br/>";
}
}
}
?>I would also appreciate any helpful suggestions on how to make my script more efficient or just plain better.
Thanks in advance!