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>';