Example #1
0
function make_comments_table($episode_id, $offset, $sort = "")
{
    global $connection;
    if ($sort == "newest") {
        $comment_set = get_comments_sorted_by_newest($episode_id, $offset);
    } else {
        if ($sort == "top") {
            $comment_set = get_comments_sorted_by_top($episode_id, $offset);
        } else {
            $comment_set = get_comments_by_episode_id($episode_id, $offset);
        }
    }
    $has_comments = mysqli_affected_rows($connection);
    $total = get_total_comments($episode_id);
    $output = "";
    if ($has_comments > 0) {
        $output .= "<select id=\"comment_sort\"> ";
        $output .= "<option value=\"top\" ";
        if ($sort == "top") {
            $output .= "selected=\"selected\"";
        }
        $output .= ">Top comments</option>";
        $output .= "<option value=\"newest\" ";
        if ($sort == "newest") {
            $output .= "selected=\"selected\"";
        }
        $output .= ">Newest first</option>";
        $output .= "</select>";
        while ($next_comment = mysqli_fetch_assoc($comment_set)) {
            $output .= make_comment_from_id($next_comment["id"]);
        }
        if ($total - $offset > 10) {
            $output .= "<div id=\"load_comments_button\">Load More Comments </div>";
        }
    } else {
        $output .= "No comments posted. ";
    }
    return $output;
}
<?php

require_once "../includes/session.php";
require_once "../includes/db_connection.php";
require_once "../includes/functions.php";
if (isset($_POST["submit"])) {
    // insert comment into comments
    $success = submit_comment($_POST["user_id"], $_POST["episode_id"], $_POST["comment"]);
    if ($success) {
        $comment_id = mysqli_insert_id($connection);
        init_comment_votes($comment_id);
        echo make_comment_from_id($comment_id);
    } else {
        echo "Comment submission failed.";
    }
}