Forum Moderators: coopster
Here is the base HTML code from which I'd like to work from. It has minimal styling, minimal JS, and minimal validation requirements to give you a visual idea of how I will execute the comments script multiple times on a single file...
comments.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>News</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style type="text/css">
div.comment {
background: #aaa;
border: #f00 solid 2px;
height: 400px;
left: 100px;
position: absolute;
top: 100px;
width: 400px;
}
div.hidden {
display: none;
}
div.news {
border: #000 solid 1px;
}
</style>
<script type="text/javascript">
<!--
function change(id, newClass) { identity=document.getElementById(id); identity.className=newClass;}
-->
</script>
</head><body>
<div class="news" id="news-9-16-2005-2">
<p>News stuff here.</p>
<a href="#" onClick="change('comment', 'comment');">Post Comment</a>
<!--comments-->
</div><div class="news" id="news-9-16-2005-1">
<p>News stuff here.</p>
<a href="#" onClick="change('comment', 'comment');">Post Comment</a>
<!--comments-->
</div><div class="news" id="news-9-3-2005-1">
<p>News stuff here.</p>
<a href="#" onClick="change('comment', 'comment');">Post Comment</a>
<!--comments-->
</div><div class="news" id="news-8-21-2005-1">
<p>News stuff here.</p>
<a href="#" onClick="change('comment', 'comment');">Post Comment</a>
<!--comments-->
</div><div class="hidden" id="comment">
<p>Add a comment and please keep it clean!</p>
<span>Name</span>
<br>
<input type="text" name="name" size="40">
<br>
<br>
<span>Comments</span>
<br>
<textarea name="Comments" cols="42" rows="4" tabindex="4"></textarea>
<br>
<input type="submit" name="submit" value="Submit" tabindex="6" />
<br>
<br>
<br>
<a href="#" onClick="change('comment', 'hidden');">Close this Window</a>
</div></body>
</html>
I know EXACTLY how I want this to work though I'm not sure how to do this in PHP so the least I could do is post the HTML right?
First we want to be able to reuse <div id="comments" for every news/comments div instead of having to create a comments form for each news item (so we're making the div id=comments dynamic). To achieve this I figured we'd detect which div ID was clicked (possibly through the hyperlink?) and set it as a PHP variable.
Secondly we need to post the comment to the file itself. Remember comments.php is the ONLY file and there are no databases (mostly because this is how I know I will effectively understand what is posted and be able to use that in posts to follow and expand on what I know about PHP (and eventually help others myself:))).
So when we use the form to post the code the PHP will FIND the div ID in the variable...THEN it will post the comment directly underneath the HTML COMMENT in that DIV (which is at the bottom).
Maybe we'll have something like this? fopen('comments.php');
Once the file posts the comment to itself, we can just have the page reload in order for the user to see their post.
I am not worried right now about if a user posts HTML/JS or anything else, or other issues. I just want to get the base idea working and then perhaps create a new post about pondering what security issues could arise from such a script later on. However I want to keep this minimal so I can just learn. :)
So here's how I would do it.
- each news item is a separate file with an intelligent naming scheme.
news-8-21-2005-1.php
news-8-21-2005-2.php
news-8-21-2005-3.php
I would encourage you, however, to use names like
news-2005-08-21-1.php
news-2005-08-21-2.php
That is, increasing specificity, two-digit month and day. That way an alphabetical sort will also be a chronological sort. This will make it much easier for you to stay organized.
Then the comments should have a naming scheme that shadows that. It could be
news-2005-08-21-1.php.comments
news-2005-08-21-2.php.comments
or
comments-2005-08-21-1.php
comments-2005-08-21-2.php
Now this simplifies things quite a bit. You can move through your file system and find files by date and get only the most recent ones. The file name then becomes your link and your include and the key to your comments.
<?
$filename = 'news-2005-09-03-1'; // we obtain this by reading from the directory
?><div class="news" id="<?php echo $filename;?>">
<?php
include($filename);
?><a href="comment.php?news=news-2005-09-03-1">Post Comment</a>
<!--comments-->
<?php
if (file_exists($filename . '.comments')
{
include($filename . '.comments');
}</div>
Will that get you started? That will be much simpler to implement and much more scalable and maintainable.
div id="comments"
Remember, to be legal HTML, you can only use an ID once per page. It should be a class.