Deeper nested comments

  php, web

I’m abit stuck. I’m trying to make nested posts in side existing nested posts. However the first set of replies show however any replies which go deeper do not work. (posts of posts to posts)

Here is the code for the entire web page…

<?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'];
 
?>
<?php
//$test = $_GET['reply'];
 
function comment($reply_to_id){
    global $db;
    $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>---[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']."]";
        comment($row['reply_to']->id);
 
        //echo "thing";
    }
}
?>
<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: ";
 
    //$topic_by_id = $row['topic_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>";
    }
    echo "Timestamp: ".$row['date']."<br>";
    echo "<a href='post_post.php?type='>Post reply</a><br>";
    echo "----------------------";
}
echo "<br>";
// now dealing with posts following
$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>";
    omment($row3['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'); 
?>

SQL is ..

CREATE TABLE `categories` (
  `cat_id` int(11) NOT NULL,
  `name` varchar(150) NOT NULL,
  `description` varchar(600) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `members` (
  `memberID` int(11) NOT NULL,
  `username` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `active` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `resetToken` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  `resetComplete` varchar(3) COLLATE utf8mb4_bin DEFAULT 'No',
  `user_ID` varchar(35) COLLATE utf8mb4_bin NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

INSERT INTO `members` (`memberID`, `username`, `password`, `email`, `active`, `resetToken`, `resetComplete`, `user_ID`) VALUES
(1, 'billy', '$2y$10$Sq7L.WhbkeTeWoawf86nFOdllWmykKb1cgu1hQAn4CbXnbGZfdPA2', '[email protected]', 'Yes', NULL, 'No', 'dwq3d33df'),
(2, 'tom', '$2y$10$Sq7L.WhbkeTeWoawf86nFOdllWmykKb1cgu1hQAn4CbXnbGZfdPA2', '[email protected]', 'Yes', NULL, 'No', 'dwq3d33df');

CREATE TABLE `posts` (
  `post_id` int(11) NOT NULL,
  `content` varchar(600) NOT NULL,
  `date` date NOT NULL,
  `vote_up` int(11) NOT NULL,
  `vote_down` int(11) NOT NULL,
  `post_by` varchar(35) NOT NULL,
  `reply_to` varchar(15) NOT NULL,
  `reply_id` varchar(15) NOT NULL,
  `comment_reply` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `posts` (`post_id`, `content`, `date`, `vote_up`, `vote_down`, `post_by`, `reply_to`, `reply_id`, `comment_reply`) VALUES
(23, 'Testing', '2021-06-08', 0, 0, '2', '2ewdd3d3', 'd3d3d3', ''),
(24, 'Testing 123', '2021-06-08', 0, 0, '2', '23223e2e', 'd3dfseddsq', ''),
(25, 'Testing', '2021-06-08', 0, 0, '2', 'd3d3d3', 'SDd3d3DD', ''),
(27, 'again', '2021-06-08', 0, 0, '1', '23223e2e', 'dw333d3', ''),
(28, 'Testing', '2021-06-08', 0, 0, '2', 'd3dfseddsq', 'dsdsddw3', ''),
(29, 'Testing', '2021-06-08', 0, 0, '2', 'dw333d3', 'sqwssw', ''),
(30, 'testing!!!!!!!!!!!!!!!!!!', '2021-06-08', 0, 0, '2', 'dw333d3', 'dsdsdsd', ''),
(31, 'testing!!!!!!!!!!!!!!!!!!sqsqsqqs', '2021-06-08', 0, 0, '1', 'dw333d3', 'dsdsdsd', ''),
(32, '1,2,3', '2021-06-08', 0, 0, '2', 'd3dfseddsq', 'sdsdsdsdsd', ''),
(33, 'again', '2021-06-08', 0, 0, '1', 'dsdsdsd', 'dsdsdsd', ''),
(34, 'W00T', '2021-06-08', 0, 0, '2', 'dsdsdsd', 'dwsdwwdwdw', ''),
(35, 'hey player', '2021-06-09', 1, 1, '1', 'dsdsddw3', 'sqwqssq', ''),
(36, 'boooooo the ghost', '2021-06-08', 0, 0, '2', 'sdsdsdsdsd', 'wdwdd', ''),
(37, 'testing!!!!!!!!!!!!!!!!!!sqsqsqqs', '2021-06-08', 0, 0, '1', 'dw333d3', 'wsdsdd', ''),
(38, 'twoooo', '2021-06-08', 0, 0, '1', 'dw333d3', 'ssss', ''),
(39, 'hate', '2021-06-08', 0, 0, '1', 'dw333d3', 'sdsdswdw3', ''),
(40, 'again again 1234', '2021-06-08', 0, 0, '1', '23223e2e', 'dwwxdw', ''),
(41, 'hater', '2021-06-08', 0, 0, '1', 'dwwxdw', 'sdsdswdw3', ''),
(42, 'FOOBAR', '2021-06-11', 1, 0, '2', 'ssss', 'dwewdwd', ''),
(43, 'tttttttttttt', '2021-06-11', 0, 1, '1', 'ssss', 'r34rf44frf', ''),
(44, 'dddddddddddddddddddddddddddddd', '2021-06-11', 1, 1, '2', 'dsdsdsd', 'sdwdwdww', '');


CREATE TABLE `tags` (
  `tag_id` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  `desciption` varchar(300) NOT NULL,
  `alias` varchar(15) NOT NULL,
  `date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tags` (`tag_id`, `name`, `desciption`, `alias`, `date`) VALUES
(1, 'tech', 'technology', 'tech', '2021-06-05'),
(2, 'fash', 'fashion', 'fash', '2021-06-05'),
(3, 'news', 'news', 'news', '2021-06-05'),
(4, 'art', 'art', 'art', '2021-06-05'),
(5, 'cyber sec', 'Cyber security discussions', 'cybsec', '2021-06-05');


CREATE TABLE `topics` (
  `topic_id` int(11) NOT NULL,
  `subject` varchar(250) NOT NULL,
  `date` date NOT NULL,
  `topic_by` int(35) NOT NULL,
  `vote_up` int(11) DEFAULT '0',
  `vote_down` int(11) NOT NULL DEFAULT '0',
  `contents` varchar(600) NOT NULL,
  `rando_id` varchar(18) NOT NULL,
  `tags` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `topics` (`topic_id`, `subject`, `date`, `topic_by`, `vote_up`, `vote_down`, `contents`, `rando_id`, `tags`) VALUES
(1, 'Hello world!!!', '2021-06-04', 1, 2, 0, 'This is a test topic to mark the creation of this forum! ', '2ewdd3d3', NULL),
(2, 'Another topic', '2021-06-03', 1, 3, 0, 'Testing again. thanks ', 'df33s22d34', 'art, news'),
(3, 'Anther another', '2021-06-04', 1, 7, 0, 'Another post', '23223e2e', NULL),
(8, 'Just another test post', '2021-06-07', 2, 0, 2, 'Testing testing one, two, three..', 'dwdwd3', 'art'),
(9, 'Again again', '2021-06-07', 2, 0, 2, '123...', '34343r3', 'fashion');

ALTER TABLE `categories` ADD PRIMARY KEY (`cat_id`);
ALTER TABLE `members` ADD PRIMARY KEY (`memberID`);
ALTER TABLE `posts` ADD PRIMARY KEY (`post_id`);
ALTER TABLE `tags` ADD PRIMARY KEY (`tag_id`);
ALTER TABLE `topics` ADD PRIMARY KEY (`topic_id`);
ALTER TABLE `categories` MODIFY `cat_id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `members` MODIFY `memberID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
ALTER TABLE `posts` MODIFY `post_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=45;
ALTER TABLE `tags` MODIFY `tag_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
ALTER TABLE `topics` MODIFY `topic_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

enter image description here

or see here:
https://comment-ag8272827827bbb.codeanyapp.com/one/ulti_thread.php?id=23223e2e

I’m designing the indenting to look like [https://mylittleforum.net/forum/]

Source: Ask PHP

LEAVE A COMMENT