Comments Example - lokothodida/DM_Matrix GitHub Wiki
This is a brief walk-through on how to set up a simple comments system.
Build a table with the following properties
- Name: comments
-
Fields:
- Name: slug, Type: text, Visibility: Invisible in form
- Name: name, Type: textlong
- Name: email, Type: email
- Name: date, Type: datetimelocal, Read Only: readonly, Visibility: Invisible in form
- Name: content, Type: bbcodeeditor
- Max Records: 0
The slug will take the value of any slug that we give it. The only reason we aren't making the type 'slug' is because it will allow for this comments script to be flexible and extendable to other content types besides just pages.
The identifier (i.e. the slug) will differ depending on the use of the comments script. In this case, let us just assume that it is the slug of the page, meaning that the slug can be identified just by using GetSimple's return_page_slug function.
<?php
// initialize slug
$slug = return_page_slug();
// POST query (done before the comments so that it gets shown as soon as its added)
if ($_SERVER['REQUEST_METHOD']=='POST') {
$_POST['slug'] = $slug;
$success = $var->createRecord('comments', $_POST);
if ($success) echo '<p class="success">Success! Your comment has been posted.</p>';
else echo '<p class="error">Error! Your comment has not been posted. Try again later.</p>';
}
// load comments
$comments = $var->query('SELECT * FROM comments WHERE slug = "'.$slug.'" ORDER BY date DESC');
$comments = $var->formatQuery($comments, 'comments');
?>
<script>
$(document).ready(function(){
$('#commentsbox').pajinate();
});
</script>
<style>
// comments box
#commentsbox {}
// paginated links
#commentsbox .page_navigation {}
// comments wrapper
#commentsbox .comments {}
// individual comments
#commentsbox .comment {}
#commentsbox .comment .name {}
#commentsbox .comment .email {}
#commentsbox .comment .date {}
#commentsbox .comment .content {}
// form
#commentsbox form {}
</style>
<div id="commentsbox">
<div class="page_navigation"></div>
<div class="comments">
<?php foreach ($comments as $comment) { ?>
<div class="comment">
<!--name-->
<span class="name"><?php echo $comment['name']; ?></span>
<!--email-->
<span class="email"><?php echo $comment['email']; ?></span>
<!--date-->
<span class="date"><?php echo $comment['date']; ?></span>
<!--content-->
<div class="content"><?php echo $comment['content']; ?></div>
</div>
<?php } ?>
</div>
<form method="post">
<?php $var->displayForm('comments'); ?>
<button>Submit Form</button>
</form>
</div>