I have written a news/blog code for a flat-file database and I want to add a comments system. Each line of news looks like this:
postID | Date | Title | News
And each comment line looks like this:
postID | commentID | Date | Name | E-Mail | URL | Comment
Now, the code to view the comments is what I am having problems with. If you go here: http://junker.org/~zshornick/addcomment.php?postID=1 , you can see the news/blog item with $postID=1 on top and the add comment form on the bottom. In the middle, I want to display all of the comments with the same postID as the post, but right now I can only make it show all of the comments. I also wrote code that showed one comment at time where the $postID was equal to the $commentID of the comment, but I lost it. Here is the code if anyone can help me out. I apologize for it not being too clean or commented...
Also, ignore $content_table and $body_dark... they are for a skin/color chooser code I wrote and am using for the template...
addcomment.php:
<?
if($HTTP_POST_VARS['submit']) {
if(!$HTTP_POST_VARS['comment']) {
echo "You must enter a comment.";
exit;
}
if(strstr($HTTP_POST_VARS['name'],"|")) {
echo "Title cannot contain the pipe symbol: |";
exit;
}
if(strstr($HTTP_POST_VARS['comment'],"|")) {
echo "News cannot contain the pipe symbol: |";
exit;
}
$fp = fopen('private/comments.txt','a');
if(!$fp) {
echo "Error opening file!";
exit;
}
$line = $HTTP_POST_VARS['parentID'];
$line .= "|" . $HTTP_POST_VARS['commentID'];
$line .= "|" . date("m.d.y");
$line .= "|" . $HTTP_POST_VARS['name'];
$line .= "|" . $HTTP_POST_VARS['email'];
$line .= "|" . $HTTP_POST_VARS['url'];
$line .= "|" . $HTTP_POST_VARS['comment'];
$line = str_replace("\r\n","<BR>",$line);
$line .= "\r\n";
fwrite($fp, $line);
if(!fclose($fp)) {
echo "Error closing file!";
exit;
}
header("Location: addcomment.php?postID=" . $HTTP_POST_VARS['parentID']);
}
$data = file('private/news.txt');
$element = trim($data[$postID]);
$pieces = explode("|",$element);
echo ?>
<table border=0 cellpadding=0 cellspacing=0 width=550 frame=void rules=none>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'> <font face='Arial' size=3><b><i><? echo stripslashes($pieces[2]) ?></i></b></font>
<hr color="<? echo "$body_dark"; ?>" size="2" width="80%" align="left"></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 bgcolor='<? echo "$content_table"; ?>' valign="top" align="justify"><div align="justify">
<font face='Arial' size=2><? echo stripslashes($pieces[3]) ?></font> </div></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 valign=middle align=right bgcolor='<? echo "$content_table"; ?>'>
<font face='Arial' size=1><i>Post ID: <? echo $pieces[0] ?></i> <br>
<i>Posted by Zack on <? echo $pieces[1] ?></i></font></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10></td>
<td width=520 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
</table>
<br>
<?
;
$i=-1;
$fd = fopen ("private/comments.txt", "r");
while (!feof($fd)) {
$i++;
$buffer = fgets($fd, 4096);
}
?>
<hr color="<? echo "$body_dark"; ?>" size="2" width="80%" align="left">
<?
$data = file('private/comments.txt');
foreach($data as $element) {
$element = trim($element);
$pieces = explode("|", $element);
echo
?>
<table border=0 cellpadding=0 cellspacing=0 width=550 frame=void rules=none>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 bgcolor='<? echo "$content_table"; ?>' valign="top" align="justify"><div align="justify">
<font face='Arial' size=2><? echo stripslashes($pieces[6]) ?></font> </div></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 valign=middle align=right bgcolor='<? echo "$content_table"; ?>'>
<font face='Arial' size=1><i>Post ID: <? echo $pieces[0] ?></i> <br>
<i>Comment ID: <? echo $pieces[1] ?></i> <br>
<i>Posted by <? echo $pieces[3] ?> on <? echo $pieces[2] ?></i> <br>
<i>E-Mail Address: <? echo $pieces[4] ?></i> <br>
<i>Website URL: <? echo $pieces[5] ?></i></font></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=520 height=10 bgcolor='<? echo "$content_table"; ?>'>
<hr color="<? echo "$body_dark"; ?>" size="2" width="80%" align="left"></td>
<td width=10 height=10 bgcolor='<? echo "$content_table"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
<tr>
<td width=10 height=10></td>
<td width=520 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
<td width=10 height=10 bgcolor='<? echo "$body_dark"; ?>'></td>
</tr>
</table>
<br>
<?
;
}
?>
<hr color="<? echo "$body_dark"; ?>" size="2" width="80%" align="left">
<FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry">
Your Name:<BR>
<INPUT TYPE="text" SIZE="30" NAME="name"><BR>
E-Mail Address:<BR>
<INPUT TYPE="text" SIZE="30" NAME="email"><BR>
Website:<BR>
<INPUT TYPE="text" SIZE="30" NAME="url"><BR>
Comments:<BR>
<TEXTAREA NAME="comment" COLS="40" ROWS="5"></TEXTAREA><BR><BR>
<input type="hidden" name="parentID" value="<? print $postID; ?>">
<input type="hidden" name="commentID" value="<? print $i; ?>">
<INPUT TYPE="submit" NAME="submit" VALUE="Post Comment"><BR>
</FORM>
Thanks in advance...