-
Posts
169 -
Joined
-
Last visited
Everything posted by oz11
-
PHP comment system, handling replies of replies using a loop of some sort.
oz11 replied to oz11's topic in PHP Coding Help
<?php require_once "../config.php"; ?> <?php $news_id = $_GET['id']; $parent = 0; $reply_commid = $_GET['reply']; if (!empty($_POST['comment']) && isset($_POST['reply_to'])){ echo "sent!"; if (isset($_POST['anon'])){ $anon = 1; } elseif (!isset($_POST['anon'])) { $anon = 0; } else { $anon = 0; } // default "Hicks" user (for testing) $sql = "INSERT INTO `comment` (`comment_id`, `parent`, `news_id`, `user_id`, `content`, `karma`, `removed`, `reference`, `date`, `last_update`, `aproved`, `anonymous`, `image_upld`) VALUES (NULL, ?, ?, '2', ?, '0', '0', '', CURRENT_TIMESTAMP, NULL, '1', ?, NULL);"; $stmt= $pdo->prepare($sql); $stmt->execute([$_POST['reply_to'], $news_id, $_POST['comment'], $anon]); //$stmt->execute([$news_id, $_POST['reply_to'], $_POST['comment']]); } ?> <form action="" method="POST"> <label for="comment">Comment: </label><br> <textarea id="comment" name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea><br> <label for="reply_to">Reply to (comment id): </label> <input name="reply_to" id="reply_to" value="<?php if(isset($_GET['reply'])){ echo $_GET['reply']; } else { echo "0"; } ?>"><br> <label for="anon">Post as Anonymous?</label> <input type="checkbox" id="anon" name="anon" value="0"><br> <input type="submit" value="Submit"> </form><br> <?php $comment_parents = []; $count = 0; echo "<h3>".countComments($pdo, $news_id)." Comments</h3><a href='?id=".$news_id."&reply=0'>Reply to thread</a><br><br>"; getAllComments($pdo, $news_id); ?> <?php function getAllComments($pdo, $news_id){ $stmt = $pdo->prepare("SELECT `comment`.*, users.name FROM `comment` LEFT JOIN `users` ON `comment`.`user_id` = `users`.`user_id` WHERE news_id = ? AND parent = '0' ORDER BY comment_id DESC"); $stmt->execute([$news_id]); while ($row = $stmt->fetch()) { echo "<div style='padding-bottom: 7px;'>"; if($row['anonymous'] !=1) { echo " ".countComments($pdo, $row['comment_id'])." <-- number of replies to reply <br><b>"; echo $row['content'] . "</b>, by <a href='profile.php?id=".$row['name']."'>".$row['name']."</a> [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies:<br>"; grabReplies($pdo, countComments($pdo, $row['comment_id']), $row['comment_id'], $news_id); echo "<hr>"; } elseif ($row['anonymous'] ==1) { echo " ".countComments($pdo, $row['comment_id'])." <-- number of replies to reply<br><b>"; echo $row['content'] . "</b>, by <u>Anonymous</u> [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies:<br>"; grabReplies($pdo, countComments($pdo, $row['comment_id']), $row['comment_id'], $news_id); echo "<hr>"; } } } function countComments($pdo, $comment_id){ $stmt = $pdo->prepare("SELECT count(*) FROM comment WHERE parent = ?"); $stmt->execute([$comment_id]); $counter = $stmt->fetch(); return $counter['count(*)']; } function grabReplies($pdo,$numberOfReplies, $comment_id, $news_id) { $stmt = $pdo->prepare("SELECT users.name, `comment`.* FROM `users` LEFT JOIN `comment` ON `comment`.`user_id` = `users`.`user_id` WHERE parent = ?"); $stmt->execute([$comment_id]); while ($row = $stmt->fetch()) { if($row['anonymous'] == 0) { echo "<div style='padding-left: 20px;'>"; echo $row['content']." by <a href='profile?id=".$row['name']."'>".$row['name']."</a> // ".$row['date']; echo " -- <a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a><br>"; echo countComments($pdo, $row['comment_id']); echo "<-- number of replies</div>"; } else if($row['anonymous'] ==1) { echo "<div style='padding-left: 20px;'>"; echo $row['content']." by <u>Anonymous</u> // ".$row['date']; echo " -- <a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a><br>"; echo countComments($pdo, $row['comment_id']); echo "<-- number of replies</div>"; } } } function getRecursive($numberOfReplies){ for ($x = 0; $x <= $numberOfReplies; $x++) { echo "-- >The number of replies is: $x <br>"; } } ?> Bit more chipping away.. now it shows a integer for the number of replies to replies. Still stuck with the loop though, thanks. I think i just need one loop to go through the remaining comments i have, using using the counters i have, not sure if this is straight forward as i think it is though...? I could go on forever adding more levels of comments in sequence, but obviously it shouldn't be done that way. - That actually brings up a good word, "levels"... still stuck. Maybe I could .. erm . idk. Help? -
PHP comment system, handling replies of replies using a loop of some sort.
oz11 replied to oz11's topic in PHP Coding Help
Sorry to be rude and spam, but i'm making progress but cannot manage the loop yet... <?php require_once "../config.php"; ?> <?php $news_id = $_GET['id']; $parent = 0; $reply_commid = $_GET['reply']; if (!empty($_POST['comment']) && isset($_POST['reply_to'])){ echo "sent!"; if (isset($_POST['anon'])){ $anon = 1; } elseif (!isset($_POST['anon'])) { $anon = 0; } else { $anon = 0; } // default "Hicks" user (for testing) $sql = "INSERT INTO `comment` (`comment_id`, `parent`, `news_id`, `user_id`, `content`, `karma`, `removed`, `reference`, `date`, `last_update`, `aproved`, `anonymous`, `image_upld`) VALUES (NULL, ?, ?, '2', ?, '0', '0', '', CURRENT_TIMESTAMP, NULL, '1', ?, NULL);"; $stmt= $pdo->prepare($sql); $stmt->execute([$_POST['reply_to'], $news_id, $_POST['comment'], $anon]); //$stmt->execute([$news_id, $_POST['reply_to'], $_POST['comment']]); } ?> <form action="" method="POST"> <label for="comment">Comment: </label><br> <textarea id="comment" name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea><br> <label for="reply_to">Reply to (comment id): </label> <input name="reply_to" id="reply_to" value="<?php if(isset($_GET['reply'])){ echo $_GET['reply']; } else { echo "0"; } ?>"><br> <label for="anon">Post as Anonymous?</label> <input type="checkbox" id="anon" name="anon" value="0"><br> <input type="submit" value="Submit"> </form><br> <?php $comment_parents = []; $count = 0; echo "<h3>".countComments($pdo, $news_id)." Comments</h3><a href='?id=".$news_id."&reply=0'>Reply to thread</a><br><br>"; getAllComments($pdo, $news_id); ?> <?php function getAllComments($pdo, $news_id){ $stmt = $pdo->prepare("SELECT `comment`.*, users.name FROM `comment` LEFT JOIN `users` ON `comment`.`user_id` = `users`.`user_id` WHERE news_id = ? AND parent = '0' ORDER BY comment_id DESC"); $stmt->execute([$news_id]); while ($row = $stmt->fetch()) { echo "<div style='padding-bottom: 7px;'>"; if($row['anonymous'] !=1) { echo " ".countComments($pdo, $row['comment_id'])." <-- number of replies to reply <br><b>"; echo $row['content'] . "</b>, by <a href='profile.php?id=".$row['name']."'>".$row['name']."</a> [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies:<br>"; grabReplies($pdo, countComments($pdo, $row['comment_id']), $row['comment_id'], $news_id); echo "<hr>"; } elseif ($row['anonymous'] ==1) { echo " ".countComments($pdo, $row['comment_id'])." <-- number of replies to reply<br><b>"; echo $row['content'] . "</b>, by <u>Anonymous</u> [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies:<br>"; grabReplies($pdo, countComments($pdo, $row['comment_id']), $row['comment_id'], $news_id); echo "<hr>"; } } } function countComments($pdo, $comment_id){ $stmt = $pdo->prepare("SELECT count(*) FROM comment WHERE parent = ?"); $stmt->execute([$comment_id]); $counter = $stmt->fetch(); return $counter['count(*)']; } function grabReplies($pdo,$numberOfReplies, $comment_id, $news_id) { $stmt = $pdo->prepare("SELECT users.name, `comment`.* FROM `users` LEFT JOIN `comment` ON `comment`.`user_id` = `users`.`user_id` WHERE parent = ?"); $stmt->execute([$comment_id]); while ($row = $stmt->fetch()) { if($row['anonymous'] == 0) { echo "<div style='padding-left: 20px;'>"; echo $row['content']." by <a href='profile?id=".$row['name']."'>".$row['name']."</a> // ".$row['date']; echo " -- <a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a><br>"; echo "</div>"; } else if($row['anonymous'] ==1) { echo "<div style='padding-left: 20px;'>"; echo $row['content']." by <u>Anonymous</u> // ".$row['date']; echo " -- <a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a><br>"; echo "</div>"; } } } ?> Output: I feel like I've hit a dead end. Help plz. -
PHP comment system, handling replies of replies using a loop of some sort.
oz11 replied to oz11's topic in PHP Coding Help
<?php require_once "../config.php"; ?> <?php $news_id = $_GET['id']; $parent = 0; $reply_commid = $_GET['reply']; if (!empty($_POST['comment']) && isset($_POST['reply_to'])){ echo "sent!"; if (isset($_POST['anon'])){ $anon = 1; } elseif (!isset($_POST['anon'])) { $anon = 0; } else { $anon = 0; } // default "Hicks" user (for testing) $sql = "INSERT INTO `comment` (`comment_id`, `parent`, `news_id`, `user_id`, `content`, `karma`, `removed`, `reference`, `date`, `last_update`, `aproved`, `anonymous`, `image_upld`) VALUES (NULL, ?, ?, '2', ?, '0', '0', '', CURRENT_TIMESTAMP, NULL, '1', ?, NULL);"; $stmt= $pdo->prepare($sql); $stmt->execute([$_POST['reply_to'], $news_id, $_POST['comment'], $anon]); //$stmt->execute([$news_id, $_POST['reply_to'], $_POST['comment']]); } ?> <form action="" method="POST"> <label for="comment">Comment: </label><br> <textarea id="comment" name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea><br> <label for="reply_to">Reply to (comment id): </label> <input name="reply_to" id="reply_to" value="<?php if(isset($_GET['reply'])){ echo $_GET['reply']; } else { echo "0"; } ?>"><br> <label for="anon">Post as Anonymous?</label> <input type="checkbox" id="anon" name="anon" value="0"><br> <input type="submit" value="Submit"> </form><br> <?php $comment_parents = []; $count = 0; echo "<h3>".countComments($pdo, $news_id)." Comments</h3><a href='?id=".$news_id."&reply=0'>Reply to thread</a><br><br>"; getAllComments($pdo, $news_id); ?> <?php function getAllComments($pdo, $news_id){ $stmt = $pdo->prepare("SELECT `comment`.*, users.name FROM `comment` LEFT JOIN `users` ON `comment`.`user_id` = `users`.`user_id` WHERE news_id = ? AND parent = '0' ORDER BY comment_id DESC"); $stmt->execute([$news_id]); while ($row = $stmt->fetch()) { echo "<div style='padding-bottom: 7px;'>"; if($row['anonymous'] !=1) { echo " ".countComments($pdo, $row['comment_id'])." <-- number of replies to reply <br><b>"; echo $row['content'] . "</b>, by <a href='profile.php?id=".$row['name']."'>".$row['name']."</a> [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies:<br>"; grabReplies(countComments($pdo, $row['comment_id'])); echo "<hr>"; } elseif ($row['anonymous'] ==1) { echo " ".countComments($pdo, $row['comment_id'])." <-- number of replies to reply<br><b>"; echo $row['content'] . "</b>, by Anonymous [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies:<br>"; grabReplies(countComments($pdo, $row['comment_id'])); echo "<hr>"; } } } function countComments($pdo, $comment_id){ $stmt = $pdo->prepare("SELECT count(*) FROM comment WHERE parent = ?"); $stmt->execute([$comment_id]); $counter = $stmt->fetch(); return $counter['count(*)']; } function grabReplies($numberOfReplies) { for ($x = 0; $x <= $numberOfReplies; $x++) { echo "Reply #: $x <br>"; } } ?> I added a loop which outputs something or nothing. What can I do next? -
PHP comment system, handling replies of replies using a loop of some sort.
oz11 replied to oz11's topic in PHP Coding Help
Just not sure what to literally put there at the comment. -
PHP comment system, handling replies of replies using a loop of some sort.
oz11 replied to oz11's topic in PHP Coding Help
Is there any way of doing this by counting a loop instead of an Array, because i would be more familiar with that approach. I was thinking I could count the rows (replies) for each comment and then loop through that number, recursively... My current/recent code: <?php require_once "../config.php"; ?> <?php $news_id = $_GET['id']; $parent = 0; $reply_commid = $_GET['reply']; if (!empty($_POST['comment']) && isset($_POST['reply_to'])){ echo "sent!"; if (isset($_POST['anon'])){ $anon = 1; } elseif (!isset($_POST['anon'])) { $anon = 0; } else { $anon = 0; } // default "Hicks" user (for testing) $sql = "INSERT INTO `comment` (`comment_id`, `parent`, `news_id`, `user_id`, `content`, `karma`, `removed`, `reference`, `date`, `last_update`, `aproved`, `anonymous`, `image_upld`) VALUES (NULL, ?, ?, '2', ?, '0', '0', '', CURRENT_TIMESTAMP, NULL, '1', ?, NULL);"; $stmt= $pdo->prepare($sql); $stmt->execute([$_POST['reply_to'], $news_id, $_POST['comment'], $anon]); //$stmt->execute([$news_id, $_POST['reply_to'], $_POST['comment']]); } ?> <form action="" method="POST"> <label for="comment">Comment: </label><br> <textarea id="comment" name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea><br> <label for="reply_to">Reply to (comment id): </label> <input name="reply_to" id="reply_to" value="<?php if(isset($_GET['reply'])){ echo $_GET['reply']; } else { echo "0"; } ?>"><br> <label for="anon">Post as Anonymous?</label> <input type="checkbox" id="anon" name="anon" value="0"><br> <input type="submit" value="Submit"> </form><br> <?php $comment_parents = []; $count = 0; echo "<h3>".countComments($pdo, $news_id)." Comments</h3><a href='?id=".$news_id."&reply=0'>Reply to thread</a><br><br>"; getAllComments($pdo, $news_id); ?> <?php function getAllComments($pdo, $news_id){ $stmt = $pdo->prepare("SELECT `comment`.*, users.name FROM `comment` LEFT JOIN `users` ON `comment`.`user_id` = `users`.`user_id` WHERE news_id = ? AND parent = '0' ORDER BY comment_id DESC"); $stmt->execute([$news_id]); while ($row = $stmt->fetch()) { echo "<div style='padding-bottom: 7px;'><b>"; if($row['anonymous'] !=1) { echo "--> ".countComments($pdo, $row['comment_id']); echo $row['content'] . "</b>, by <a href='profile.php?id=".$row['name']."'>".$row['name']."</a> [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies: <ul style='list-style-type: katakana-iroha;'> <li>1</li><li>2</li><li>3, etc.</li> </ul><hr>"; } elseif ($row['anonymous'] ==1) { echo "--> ".countComments($pdo, $row['comment_id']); echo $row['content'] . "</b>, by Anonymous [Comment ID:".$row['comment_id']."] [Parent ID: ".$row['parent']. "] [Date: ".$row['date']."] [Anon?: ".$row['anonymous']."] <br> <span style='font-size: 13px;'><a href='?id=".$news_id."&reply=".$row['comment_id']."'>Reply</a></span></div> <br> Replies: <ul style='list-style-type: katakana-iroha;'> <li>1</li><li>2</li><li>3, etc.</li> </ul><hr>"; } } } function countComments($pdo, $comment_id){ $stmt = $pdo->prepare("SELECT count(*) FROM comment WHERE parent = ?"); $stmt->execute([$comment_id]); $counter = $stmt->fetch(); return $counter['count(*)']; } ?> For a start, my count doesn't count the correct number of replies for each post?? - Edit: count now works!! Just used comment_id as a parameter to fetch the number of replies. Screenshot: When amending your code it looks like this: <?php require_once "../config.php"; ?> <?php $news_id = $_GET['id']; $parent = 0; $reply_commid = $_GET['reply']; if (!empty($_POST['comment']) && isset($_POST['reply_to'])){ echo "sent!"; if (isset($_POST['anon'])){ $anon = 1; } elseif (!isset($_POST['anon'])) { $anon = 0; } else { $anon = 0; } // default "Hicks" user (for testing) $sql = "INSERT INTO `comment` (`comment_id`, `parent`, `news_id`, `user_id`, `content`, `karma`, `removed`, `reference`, `date`, `last_update`, `aproved`, `anonymous`, `image_upld`) VALUES (NULL, ?, ?, '2', ?, '0', '0', '', CURRENT_TIMESTAMP, NULL, '1', ?, NULL);"; $stmt= $pdo->prepare($sql); $stmt->execute([$_POST['reply_to'], $news_id, $_POST['comment'], $anon]); //$stmt->execute([$news_id, $_POST['reply_to'], $_POST['comment']]); } ?> <form action="" method="POST"> <label for="comment">Comment: </label><br> <textarea id="comment" name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea><br> <label for="reply_to">Reply to (comment id): </label> <input name="reply_to" id="reply_to" value="<?php if(isset($_GET['reply'])){ echo $_GET['reply']; } else { echo "0"; } ?>"><br> <label for="anon">Post as Anonymous?</label> <input type="checkbox" id="anon" name="anon" value="0"><br> <input type="submit" value="Submit"> </form><br> <?php $comment_parents = []; $count = 0; echo "<h3>".countComments($pdo, $news_id)." Comments</h3><a href='?id=".$news_id."&reply=0'>Reply to thread</a><br><br>"; getAllComments($pdo, $news_id); ?> <?php function getAllComments($pdo, $news_id){ $stmt = $pdo->prepare("SELECT `comment`.*, users.name FROM `comment` LEFT JOIN `users` ON `comment`.`user_id` = `users`.`user_id` WHERE news_id = ? AND parent = '0' ORDER BY comment_id DESC"); $stmt->execute([$news_id]); $comment_parents = []; $count = 0; while ($row = $stmt->fetch()) { if (!isset($comment_parents[$row['parent']])) { $comment_parents[$row['parent']] = []; } $comment_parents[$row['parent']][] = $row; $count++; } } function showComments(array $comment_parents, array $comment, int $level = 0) { /* show the $comment */ foreach ($comment_parents[$comment['id']] ?? [] as $child) { showComments($comment_parents, $child, $level + 1); } } foreach ($comment_parents[0] as $comment) { showComments($comment_parents, $comment); } function countComments($pdo, $news_id){ $stmt = $pdo->prepare("SELECT count(*) FROM comment WHERE news_id = ?"); $stmt->execute([$news_id]); $counter = $stmt->fetch(); return $counter['count(*)']; } ?> Sorry I'm not super in php yet. I'm stuck here, maybe you could look at the code and tell me what to do next to sort it out? This is the hardest thing i have coded after turtle graphics. -
PHP comment system, handling replies of replies using a loop of some sort.
oz11 replied to oz11's topic in PHP Coding Help
Hello requinix! The code has been edited and looks a lot better now. Big thanks! However I'm stuck on the line "<---- not sure what to put here" not sure what i'm doing with Arrays as I'm a little foggy. Could you check my code again and see whether i have it in order and describe what i need to do on this line? Thanks! <?php require_once "../config.php"; ?> <?php if (isset($_POST['comment'])){ echo true; //$sql = "INSERT INTO users (name, surname, sex) VALUES (?,?,?)"; //$stmt= $pdo->prepare($sql); //$stmt->execute([$name, $surname, $sex]); } ?> <form action="" method="POST"> <label for="comment">Comment</label><br> <textarea id="comment" name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea> <input type="submit" value="Submit"> </form><br><br> <?php $comment_parents = []; $count = 0; $news_id = $_GET['id']; $parent = 0; $stmt = $pdo->prepare("SELECT * FROM comment WHERE news_id = ? ORDER BY `date` DESC"); $stmt->execute([$news_id]); while ($row = $stmt->fetch()) { echo "<br>"; if (!isset($comment_parents[$row['parent']])) { $comment_parents[$row['parent']] = []; } $comment_parents[$row['parent']][] = $row; $count++; foreach ($comment_parents[0] as $comment) { showComments($comment_parents, $comment); } } function showComments(array $comment_parents, array $comment, int $level = 0) { // <---- not sure what to put here foreach ($comment_parents[$comment['id']] ?? [] as $child) { showComments($comment_parents, $child, $level + 1); } } ?> -
Hey. Today I've been working on an important part of my website, the Comments section! It handles replies, but not sure how to handle replies of replies etc..I know that it involves a loop, but I'm really scratching my head with this one. <?php require_once "../config.php"; ?> <?php if (isset($_POST['comment'])){ echo true; //$sql = "INSERT INTO users (name, surname, sex) VALUES (?,?,?)"; //$stmt= $pdo->prepare($sql); //$stmt->execute([$name, $surname, $sex]); } ?> <form action="" method="POST"> <label for="comment">Comment</label><br> <textarea id="comment" name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea> <input type="submit" value="Submit"> </form><br><br> <?php $news_id = $_GET['id']; $parent = 0; $stmt = $pdo->prepare("SELECT * FROM comment where news_id = ? AND parent = ? ORDER BY `comment`.`date` DESC"); $stmt->execute([$news_id, $parent]); while ($row = $stmt->fetch()) { echo "<br>"; echo $row['content']; echo getReplies($pdo, $news_id, $row['comment_id']); echo getRepliesOfReplys($pdo, $news_id, $row['comment_id']); //getCountReplies($pdo, $parent); } ?> <br><br> <?php function getReplies($pdo, $news_id, $parent){ $stmt2 = $pdo->prepare("SELECT comment_id, news_id, parent, user_id, content, karma, removed, reference, date, aproved, anonymous, image_upld FROM `comment` WHERE news_id = ? AND parent = ? GROUP BY comment_id ORDER BY `comment`.`date` DESC"); $stmt2->execute([$news_id, $parent]); while ($row2 = $stmt2->fetch()) { echo "<br> - ".$row2['content']." [Comment ID:".$row2['comment_id']."], [Parent: ".$row2['parent']."]\n"; } } function getCountReplies($pdo, $parent){ $stmt = $pdo->prepare("SELECT COUNT(*) FROM `comment` where parent = ?"); $stmt->execute([$parent]); $count = $stmt->fetch(); echo $count['COUNT(*)']; } function getRepliesOfReplys($pdo, $news_id, $parent){ $stmt = $pdo->prepare("SELECT comment_id, news_id, parent, user_id, content, karma, removed, reference, date, aproved, anonymous, image_upld FROM `comment` WHERE news_id = ? AND parent = ? GROUP BY comment_id ORDER BY `comment`.`date` DESC"); $stmt->execute([$news_id, $parent+1]); while ($row = $stmt->fetch()) { echo "<br> -- ".$row['content']." [Comment ID:".$row['comment_id']."], [Parent: ".$row['parent']."]\n"; } } ?> Any free help, advice or code writers appreciated.
-
Hey, simple question here. How do i generate a random number daily between 1 and 10? It needs to say the same throughout the day and change he next, etc. Can't figure it out myself. Thanks.
-
I updated the code to... <?php $host = '127.0.0.1'; $db = 'news'; $user = 'root'; $pass = ''; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); } catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode()); } function banCheck($pdo, $id) { $stmt = $pdo->prepare("SELECT suspended FROM `users` WHERE user_id=? LIMIT 1"); $stmt->execute([$id]); $check = $stmt->fetch(); if($check['suspended'] == 1){ //header("Location: banned.php"); echo "banned"; } else { echo "not banned"; } } banCheck($pdo, 14); ?> Thanks requinix pal
-
$host = '127.0.0.1'; $db = 'news'; $user = 'root'; $pass = ''; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); } catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode()); } function banCheck($id) { $stmt = $pdo->prepare("SELECT suspended FROM `users` WHERE user_id=? LIMIT 1"); $stmt->execute([$id]); $check = $stmt->fetch(); if($check['suspended'] == 1){ //header("Location: banned.php"); echo "banned"; } else { echo "not banned"; } } banCheck("4"); I'm not sure what's happening or no happening at the moment. "Undefined variable '$pdo'.intelephense(1008) " - line 24
-
<h1>Search</h1> <?PHP if ($_GET['type'] == "userid"){ echo "userid"; } if ($_GET['type'] == "userstring"){ echo "user_string"; } if ($_GET['type'] == "postid"){ echo "post_id"; } if ($_GET['type'] == "poststring"){ echo "post_string"; } ?> <a href="index.php?page=all_users">View all users</a> // <a href="index.php?page=search&type=all_posts">View all posts</a> <br><br><br> <form method="GET" action="index.php?page=search"> <h3>User ID</h3> <label for="lname">Username ID:</label><br> <input type="text" id="userid" name="userid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="GET" action="index.php?page=search"> <h3>User string</h3> <label for="lname">Username String:</label><br> <input type="text" id="userstring" name="userstring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="GET" action="index.php?page=search"> <h3>Post ID</h3> <label for="lname">Post ID:</label><br> <input type="text" id="postid" name="postid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="GET" action="index.php?page=search"> <h3>Post string: </h3> <label for="lname">Post String:</label><br> <input type="text" id="poststring" name="poststring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> #2 <h1>Search</h1> <?PHP if ($_GET['type'] == "userid"){ echo "userid"; } if ($_GET['type'] == "userstring"){ echo "user_string"; } if ($_GET['type'] == "postid"){ echo "post_id"; } if ($_GET['type'] == "poststring"){ echo "post_string"; } ?> <a href="index.php?page=all_users">View all users</a> // <a href="index.php?page=search&type=all_posts">View all posts</a> <br><br><br> <form method="GET" action="index.php?page=search&userid=1"> <h3>User ID</h3> <label for="lname">Username ID:</label><br> <input type="text" id="userid" name="userid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="GET" action="index.php?page=search&userstring=1"> <h3>User string</h3> <label for="lname">Username String:</label><br> <input type="text" id="userstring" name="userstring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="GET" action="index.php?page=search&postid=1"> <h3>Post ID</h3> <label for="lname">Post ID:</label><br> <input type="text" id="postid" name="postid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="GET" action="index.php?page=search&poststring=1"> <h3>Post string: </h3> <label for="lname">Post String:</label><br> <input type="text" id="poststring" name="poststring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> I've ran both of these codes, anyway.. they are not doing what I expect. Instead of being processed on the page and running the GET data through the conditionals, instead it takes me to "index.php" every time. I've tried numerous "actions", though these never work. The file I'm dealing with is named "index.php?page=search" (it's an "include" used for CMS or somthing similar). Edit: for example, being forwarded to "index.php?postid=1", not "index.php?page=search&postid=1" .
-
<h1>Search</h1> <?php if (isset($_POST['userid'])){ $keyword = trim ($_POST['userid']); echo $keyword; $stmt = $pdo->prepare("SELECT * FROM `users` WHERE name LIKE '%?%'"); $stmt->execute([$keyword]); while ($row = $stmt->fetch()) { echo $row['user_id']."<br />\n"; } } if (isset($_POST['userstring'])){ $keyword = trim ($_POST['userstring']); echo $keyword; } if (isset($_POST['postid'])){ $keyword = trim ($_POST['postid']); echo $keyword; } if (isset($_POST['poststring'])){ $keyword = trim ($_POST['poststring']); echo $keyword; } ?> <form method="POST" action="index.php?page=search&type=user_id"> <h3>User ID</h3> <label for="lname">Username ID:</label><br> <input type="text" id="userid" name="userid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="POST" action="index.php?page=search&type=user_string"> <h3>User string</h3> <label for="lname">Username String:</label><br> <input type="text" id="userstring" name="userstring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="POST" action="index.php?page=search&type=post_id"> <h3>Post ID</h3> <label for="lname">Post ID:</label><br> <input type="text" id="postid" name="postid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="POST" action="index.php?page=search&type=post_string"> <h3>Post string: </h3> <label for="lname">Post String:</label><br> <input type="text" id="poststring" name="poststring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> Ok. Basically, i want the code to output the site info using POST functions and PHP PDO. For some reason though this code is not working for me. I really don't know where I'm going wrong. Can you spot my bug? ATM it will display nothing but "$keyword".
-
Nevermind.. found this: $handle = popen("tail -50l 'information/information.txt' 2>&1", 'r'); echo '<pre>'; while(!feof($handle)) { $buffer = fgets($handle); echo "$buffer"; ob_flush(); flush(); } echo '</pre>'; pclose($handle); thanks and enjoi
-
<?php $file = 'information/information.txt'; $orig = file_get_contents($file); $a = htmlentities($orig); echo '<code>'; echo '<pre>'; echo $a; echo '</pre>'; echo '</code>'; ?> I managed to get this code working, however I only want it to display the first "1000 lines" from the file.. how would i go about changing the code to do this? The idea behind the method is so that it takes users HTTP "logged" data and stores the to a text file, i only need the first 1000 lines. Thanks.
-
php image scaler, (supporting "gif", "png" and "jpeg")
oz11 replied to oz11's topic in PHP Coding Help
Just going to place this here also... <?php function resize_image($image_name) { //$image_name = 'https://c.tenor.com/GnSnviXkCR4AAAAd/memes.gif'; $image_name = $image_name; $path_parts = pathinfo($image_name); $path_parts['extension']; if ($path_parts['extension'] == "jpg") { //echo "jpeg"; list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; $image = imagecreatefromjpeg($image_name); $img = imagescale( $image, $new_width, $new_height ); header("Content-type: image/jpeg"); imagejpeg($img); } if ($path_parts['extension'] == "png"){ //echo "png ;)"; list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; $image = imagecreatefrompng($image_name); $img = imagescale( $image, $new_width, $new_height ); header("Content-type: image/png"); imagepng($img); } if ($path_parts['extension'] == "gif"){ //echo "gif ;)"; list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; $image = imagecreatefromgif($image_name); $img = imagescale( $image, $new_width, $new_height ); header("Content-type: image/gif"); imagegif($img); } // Later support } resize_image("https://c.tenor.com/GnSnviXkCR4AAAAd/memes.gif") ; ?> This uses the aspect ratio of original file. Enjoi -
php image scaler, (supporting "gif", "png" and "jpeg")
oz11 replied to oz11's topic in PHP Coding Help
omg. it worked. thanks ALOT comrade. -
php image scaler, (supporting "gif", "png" and "jpeg")
oz11 replied to oz11's topic in PHP Coding Help
Hey mac_gyver. Thanks for the feedback.. I've changed the code a bit and swapped stuff around. Tho now its just showing up as white squares. Not sure where to go from here. <?php function resize_image($image_name) { $path_parts = pathinfo($image_name); $path_parts['extension']; //$image_name = 'https://i.kym-cdn.com/photos/images/newsfeed/001/708/961/9e1.png'; if ($path_parts['extension'] == "jpg") { echo "jpeg"; $image = imagecreatefromjpeg($image_name); $img = imagescale( $image, 500, 400 ); header("Content-type: image/jpeg"); imagejpeg($img); } if ($path_parts['extension'] == "png"){ echo "png ;)"; $image = imagecreatefrompng($image_name); $img = imagescale( $image, 500, 400 ); header("Content-type: image/png"); imagepng($img); } if ($path_parts['extension'] == "gif"){ echo "gif ;)"; $image = imagecreatefromgif($image_name); $img = imagescale( $image, 500, 400 ); header("Content-type: image/gif"); imagegif($img); } } resize_image($_GET['name']) ; ?> -
<?php function resize_image($image_name) { echo $image_name = "images/".$image_name; $path_parts = pathinfo('$image_name'); $path_parts['extension']; if ($path_parts['extension'] = "jpg") { // Assign image file to variable //$image_name; // Load image file $image = imagecreatefromjpeg ($image_name); list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; // Use imagescale() function to scale the image $img = imagescale( $image, $new_width, $new_height ); //Output image in the browser header("Content-type: image/jpeg"); imagejpeg($img); } if ($path_parts['extension'] = "png"){ //echo "png ;)"; //$image_name = //'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'; // Load image file $image = imagecreatefrompng($image_name); list($width, $height) = getimagesize($image_name); // Use imagescale() function to scale the image $img = imagescale( $image, 300, ($height/$width)*$new_width ); // Output image in the browser header("Content-type: image/png"); imagepng($img); } if ($path_parts['extension'] = "gif"){ //echo "png ;)"; //$image_name = //'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'; // Load image file $image = imagecreatefromgif($image_name); list($width, $height) = getimagesize($image_name); // Use imagescale() function to scale the image $img = imagescale( $image, 300, ($height/$width)*$new_width ); // Output image in the browser header("Content-type: image/gif"); imagegif($img); } } $image_name = $_GET['name']; resize_image($image_name ) ; ?> Hey, i'm having some trouble with this code I've written. It works when using remote website's images, though wont work when i point it to files on my host. Took me a while to write the code (im a noob), but cannot get it to work. Any suggestions? ps: It'a also kinda slow, maybe it will speed up when using local content/images, though im not sure? Is this a bad solution in your opinions? Thanks.