l I have managed to create a search engine which retrieves my results in my mysql database. I can view elements of this this data. However I was looking to display search results in the same style I have on my page titled news.php.
Search.php:
if(isset($_POST['submit'])){
$search = $_POST['search'];
$search = "%$search%";
$articles = $db->prepare("SELECT * FROM articles WHERE headline LIKE :search");
$articles->bindParam(':search', $search);
$articles->execute(array(':search' => $search));
if($articles -> rowCount() > 0) {
while ($rows = $articles->fetch(PDO::FETCH_ASSOC)){
$headline = $rows['headline'];
echo "headline: $headline<br>";
}
} else{
echo "No Results";
}
}
News.php
$query = $handler->query('SELECT * FROM articles');
$results = $query->fetchAll(PDO::FETCH_ASSOC);
for ($i=0; $i < count($results); $i++) {
echo '<div class="col-md-4 col-xs-12 col-sm-12 height-news">';
echo '<p class="news-title">'.$results[$i]['headline'].'<br>'.'</p>';
echo '<img class="news-img" src="data:image/png;base64,'.base64_encode ( $results[$i]['logo'] ).'"/>';
echo '<p class="news-time">'.$results[$i]['date'].'<br>'.'</p>';
echo nl2br('<p class="news-body">'.$results[$i]['text'].'<br>'.'</p>') ;
echo '</div>';
}
You will need to post to the search.php file. Try something like this:
$('#yourSearhcButton").click(function(){
$.post("link/to/Search.php", { search: search });
});
Read the docs -> https://api.jquery.com/jquery.post/