1

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";
}
?>
Ash
  • 25
  • 7
  • When emitting the results to the page, check for some indicator on the record of whether or not you want to style it differently. If that indicator exists, emit a different style for that record. In your database results, how can you tell the difference between the two records? – David Jan 24 '17 at 19:21
  • Holy SQL Injection batman! Don't take user input and directly place it into a SQL statement without escaping it or (preferably) using bind parameters. – Sean McSomething Jan 24 '17 at 21:06

1 Answers1

1

Assuming that your table schema are too identical to easily tell the difference. You can add a custom column to the query to distinguish them. Something like:

<?php
$search = $_GET['s'];
$db = mysqli_connect("", "", "", "") or die ("could not connect to mysql");

if ($search) {
    $sql = "SELECT photos.*, 'photos' AS 'tableName' FROM photos
            WHERE title LIKE '%".$search."%'
            ORDER BY id DESC";
} else {
    $sql = "SELECT photos.*, 'photos' AS 'tableName' FROM photos
            UNION SELECT sponsor.*, 'sponsor' AS 'tableName' 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['tableName']}\" 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";
}
?>

Notice in the queries, I've added 'photos' AS 'tableName' and 'sponsors' AS 'tableName' so that each query also adds a column with what table it was from. That lets you access row['tableName'] in the same way that you would reference any other column.

At that point it is as simple as adding a class to the a tag. You could put that class on any of your elements depending on exactly how you want your markup.

Keep in mind, this code as written is vulnerable to SQL Injection. You should be using prepared statements rather than directly inserting GET variables into your query.

Community
  • 1
  • 1
TheGentleman
  • 2,101
  • 13
  • 16
  • Hi, I'm trying to use your suggestion in the update above, could you take a look thanks. – Ash Jan 24 '17 at 21:59