The below code works but I want to develop it further by styling the results from the 'sponsor' table differently to the 'photos' table. How can I target it to do so?
<?php
$search = $_GET['s'];
$db = mysqli_connect("", "", "", "") or die ("could not connect to mysql");
if ($search) {
$sql = "SELECT * FROM photos
WHERE title LIKE '%".$search."%'
ORDER BY id DESC";
} else {
$sql = 'SELECT * FROM photos
UNION SELECT * FROM sponsor
ORDER BY id DESC';
}
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) >=1) {
while ($row = mysqli_fetch_array($result)) {
echo"<a href=".$row['link'].">";
echo"<div class='img_div'>";
echo"<div class='white'><img src='images/".$row['image']."'></div>";
echo"<h2>".$row['title']."</h2>";
echo"<div class='date'>".$date = date('j F, Y', strtotime($row['date']))."</div>";
echo"</div>";
echo"</a>";
}
//continue
}else{
echo "No Results";
}
?>
UPDATE: Trying to incorporate the below answer
if ($search) {
$sql = "SELECT photos.*, 'photos' AS 'tableName1' FROM photos
WHERE title LIKE '%".$search."%'
ORDER BY id DESC";
} else {
$sql = "SELECT photos.*, 'photos' AS 'tableName1' FROM photos
UNION SELECT sponsor.*, 'sponsor' AS 'tableName2' FROM sponsor
ORDER BY id DESC";
}
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) >=1) {
while ($row = mysqli_fetch_array($result)) {
echo"<a class=\"{$row['tableName1']}\" href=".$row['link'].">";
echo"<div class='img_div'>";
echo"<div class='white'><img src='images/".$row['image']."'></div>";
echo"<h2>".$row['title']."</h2>";
echo"<div class='date'>".$date = date('j F, Y', strtotime($row['date']))."</div>";
echo"</div>";
echo"</a>";
echo"<a class=\"{$row['tableName2']}\" href=".$row['link'].">";
echo"<div class='img_div2'>";
echo"<div class='white2'><img src='images/".$row['image']."'></div>";
echo"<h2 class='sponsor'>".$row['title']."</h2>";
echo"</div>";
echo"</a>";
}
//continue
}else{
echo "No Results";
}
?>