Hi, first post here. I'm wanting to create a php script which will call itself in a recursive function so that nested comments are made ..
An example of a perfect hierarchy would be as bellow...
Comment #1
-- Comment reply
--- Comment reply
--- Comment reply
------ Comment reply
-------- Comment reply
---------- Comment reply
--- Comment reply
--- Comment reply
Comment #2
-- Comment reply
------ Comment reply
-- Comment reply
Note how they go deep into the comment replies (should support 7-25 comments in comments/deep) At the moment it only goes three deep as the recursive function doesn't work...
I have created the function, but for some reason the line "comment($row['id']);" in the recursive function isn't preforming anything, commented or un commented. However looks fine to me.
function comment($reply_to_id){
global $db;
echo $reply_to_id;
$stmt11 = $db->prepare('SELECT * FROM `posts` WHERE reply_to = :thing');
$stmt11->execute(['thing' => $reply_to_id]);
while ($row = $stmt11->fetch()) {
echo "<br>";
echo " //----------[UID: ".$row['post_by']."]----------".$row['date']."----------->>> ";
echo $row['content'];
// comment($row['id']);
}
}
The full code is ..
<?php require('includes/config.php');
//define page title
$title = 'Thread Page';
//include header template
require('layout/header.php');
echo "<h1>the Forum // make post </h1>";
$id = $_GET['id'];
$reply = $_GET['reply'];
$rando1;
$test = $_GET['test'];
$index = 1;
?>
<?php
function indent(){
echo "-";
}
function stars($index){
//example to demonstrate star pattern-2
for($i=1; $i<=$index; $i++) {
for($j=1; $j<=$i; $j++)
{
echo '*';
}
echo '*';
}
}
function comment($reply_to_id){
global $db;
echo $reply_to_id;
$stmt11 = $db->prepare('SELECT * FROM `posts` WHERE reply_to = :thing');
$stmt11->execute(['thing' => $reply_to_id]);
while ($row = $stmt11->fetch()) {
echo "<br>";
echo " //----------[UID: ".$row['post_by']."]----------".$row['date']."----------->>> ";
echo $row['content'];
// comment($row['id']);
}
}
?>
<ul>
<?php
$rando_id = null;
// dealing with inital thread
$stmt = $db->prepare('SELECT * FROM `topics` WHERE rando_id = :topic_id');
$stmt->execute(['topic_id' => $id]);
while ($row = $stmt->fetch())
{
echo "Contents:<br>";
echo $row['contents'];
echo "<br><br>By: ";
$stmt2 = $db->prepare('SELECT * FROM `members` WHERE memberID = :mem_id');
$stmt2->execute(['mem_id' => $row['topic_by']]);
while ($row2 = $stmt2->fetch())
{
echo "<a href='profile.php?id=".$row2['user_ID']."'>".$row2['username']."</a><br>";
comment($row2['reply_id']);
}
echo "Timestamp: ".$row['date']."<br>";
echo "<a href='post_post.php?type='>Post reply</a><br>";
echo "----------------------";
echo "<br>";
}
echo "<br>";
$stmt3 = $db->prepare('SELECT * FROM `posts` WHERE reply_to = :reply_to');
$stmt3->execute(['reply_to' => $id]);
while ($row3 = $stmt3->fetch())
{
echo "<br>";
echo "// UID: [".$row3['post_by']."] ---- ".$row3['date']."------->> ".$row3['content']." <a href='post_post.php'>Reply</a>";
// comment($row3['reply_id']);
$reply_to_id = $row3['reply_id'];
$stmt11 = $db->prepare('SELECT * FROM `posts` WHERE reply_to = :thing');
$stmt11->execute(['thing' => $reply_to_id]);
while ($row = $stmt11->fetch())
{
// ----------------------------- SEE HERE (sp[lice]) -----------------------------
echo "<br>---[UID:";
echo $row['post_by']."] -- ".$row['date'].": ";
echo $row['content'];
echo " <a href='reply.php'>Reply</a>";
echo " [Reply to: ".$row['reply_to']."]";
echo " [Reply ID: ".$row['reply_id']."]";
$reply_to_id=$row['reply_id'];
$stmt111 = $db->prepare('SELECT * FROM `posts` WHERE reply_to = :thing');
$stmt111->execute(['thing' => $reply_to_id]);
while ($row33 = $stmt111->fetch()) {
echo "<br>--------[UID:";
echo $row33['post_by']."] -------- ".$row['date'].": ";
echo $row33['content'];
echo " <a href='reply.php'>Reply</a>";
echo " [Reply to: ".$row33['reply_to']."]";
echo " [Reply ID: ".$row33['reply_id']."]";
echo "<br>";
comment($row33['reply_id']);
}} // ------------------------------------------------------
}
?>
</ul>
<?php
if ($user->is_logged_in()){
} else {
echo "<h2>Login to create post </h2>";
echo "<a href='login.php'>Click to login</a>";
}
?>
<?php
//include header template
require('layout/footer.php');
?>