How can you display only news articles with a specific keyword, like "lol," in PHP?
To display only news articles with a specific keyword like "lol" in PHP, you can use a combination of PHP and SQL to query your database for articles containing the keyword. You can use the LIKE operator in your SQL query to search for the keyword in the article content or title. Once you retrieve the articles containing the keyword, you can display them on your webpage.
<?php
// Assuming you have a database connection established
$keyword = "lol";
$sql = "SELECT * FROM articles WHERE content LIKE '%{$keyword}%' OR title LIKE '%{$keyword}%'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2>{$row['title']}</h2>";
echo "<p>{$row['content']}</p>";
}
} else {
echo "No articles found with the keyword '{$keyword}'.";
}
mysqli_close($conn);
?>
Keywords
Related Questions
- What are the best practices for handling user input in PHP to ensure data integrity and security?
- What are the common reasons for the error message "Query was empty" in PHP scripts?
- What are the recommended steps for troubleshooting and resolving issues related to generating PDF previews using PHP, particularly when integrating with JavaScript libraries like cluetip for display purposes?