0

i am trying to allow users to filter comments by the hashtags used in them but i am very new to php so do not understand how to link them together (the search button in my html and search in my php)

php:

try {
    if (isset($_POST['search-submit']) && $_POST['search-submit'] == 'Search') {
        $selectedHashtag = isset($_POST['hashtag-filter']) ? $_POST['hashtag-filter'] : '';

        $stmt = $conn->prepare("SELECT commDate, ID, username, comment, hashtags FROM comments WHERE hashtags = :selectedHashtag");
        $stmt->bindParam(':selectedHashtag', $selectedHashtag);
    } else {
        $stmt = $conn->prepare("SELECT commDate, ID, username, comment, hashtags FROM comments");
    }

    $stmt->execute();

    $comments = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo '<div class="comment-container">';
    foreach ($comments as $comment) {
        echo '<div class="comment">';
        echo '<p><strong>Date:</strong> ' . $comment['commDate'] . '</p>';
        echo '<p><strong>Username:</strong> ' . $comment['username'] . '</p>';
        echo '<p><strong>Comment:</strong> ' . $comment['comment'] . '</p>';
        echo '<p><strong>Hashtag:</strong> ' . $comment['hashtags'] . '</p>';
        echo '</div>';
    }
    echo '</div>';

html:

if(isset($_POST['submit'])) {

$retrived_result = $hashtag->search_hashtag($_POST);

}
$stmt = $conn->prepare("SELECT commDate, ID, username, comment, hashtags FROM comments");
        $stmt->execute();
        $comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo '<div class="comment-container">';
        foreach ($comments as $comment) {
            echo '<div class="comment">';
            echo '<p><strong>Date:</strong> ' . $comment['commDate'] . '</p>';
            echo '<p><strong>Username:</strong> ' . $comment['username'] . '</p>';
            echo '<p><strong>Comment:</strong> ' . $comment['comment'] . '</p>';
            echo '<p><strong>Hashtag:</strong> ' . $comment['hashtags'] . '</p>';
            echo '</div>';
        }
        echo '</div>';
2
  • The bit you've labelled "HTML" is also primarily PHP (although it can output some HTML...but then so can your other piece of code)
    – ADyson
    Commented Dec 19, 2023 at 21:19
  • Both bits seem to respond to the submission of a form, and both seem to do something fairly similar. So what exactly is the issue? Have you got two forms and you want to submit them all to the same PHP script? Or you have two scripts and one form? Or what? What data is submitted from the form? What results do you want back? It's really not very cllear - provide us some sample database data, sample input/search data, and expected results please. We are not mind-readers :-).
    – ADyson
    Commented Dec 19, 2023 at 21:21

0

Browse other questions tagged or ask your own question.