Identifying tables from union and add href links to their output

Ive got the following search script that seaches my tables in my db and outputs the results. I need to know how to identify which table the result comes from and then add the oppropriate link by id to view the actual data. I have reseached and i know i need a discriminator to identify the tables but have no idea how to implement it with the href links. Can someone please assist me.

<?php
include'db.php';
if(!empty($_POST))
{
$aKeyword = explode(" ", $_POST['keyword']);
$query ="SELECT * FROM tbl_articles WHERE name like '%" . $aKeyword[0] . "%'
  UNION
  SELECT * FROM tbl_album WHERE name like '%" . $aKeyword[0] . "%'
  UNION
  SELECT * FROM tbl_gallery WHERE gname like '%" . $aKeyword[0] . "%'
  UNION
  SELECT * FROM tbl_events WHERE name like '%" . $aKeyword[0] . "%'";

 for($i = 1; $i < count($aKeyword); $i++) {
    if(!empty($aKeyword[$i])) {
        $query .= " OR name like '%" . $aKeyword[$i] . "%'";
    }
  }

 $result = $db->query($query);
 echo "<br>You have searched for keywords: " . $_POST['keyword'];

 if(mysqli_num_rows($result) > 0) {
    $row_count=0;
    echo "<br>Result Found: ";

    While($row = $result->fetch_assoc()) {   
        $row_count++;   
        $id = $row['artid'];
        $name = $row['name'];                      
        echo "<a href='results.php?id=$id'>$name</a>";
    }

}
else {
    echo "<br>Result Found: NONE";
}

}


?>

What’s the schema of those tables?

I have 4 tables tbl_articles -id , name, adesc,image, date, author
tbl_albums -id,name,adesc,image,date,status
tbl_gallery -albumid,gname,gimages,date,status
tbl_events -id,name,name,adesc,date,inmage,status

So if the author column of your result is not null, it’s an article.
If the image column of your result is not null, it’s an album.
If the gname of the result is not null, it’s a gallery
Otherwise, it’s an event.

When a search is conducted and the result for example comes from the tbl_articles table i need the source table to be identified and linked to the relavent article page. something along the lines if($row == tbl_articles{
echo “$row[‘name’]”;
else if($row ==tble_gallery{
echo "Row[‘gname’];

i can then just add an href link. At least thats what my logic is telling me however im unsure of how to go about it.

You’re already sure of how to go about it.

Its not working hence me being here asking for help…:blush:

So if i give you this:

$row = array('name' => "I'm an Article", "author" => "m_hutley", "gname" => null,"image"=>null,"adesc" => "I have a desc.");

Take this sentence:

and write it in code. You may want to shorthand it using is_null

Then change it to

$row = array('name' => "I'm a Gallery", "author" => null, "gname" => "m_hutley's gallery","image"=>null,"adesc" => "I have a desc.");

and write this sentence in code:

… and then stick them together with an else. THEN worry about what goes inside the {}'s.

I managed to get it working as follows:

             if($row['source'] === 'tbl_articles') { 
            echo "<p><a href='result1.php?artid=".$row["artid"]."'>".$row["name"]."</a></p>";
            } 
            else if($row['source'] === 'tbl_album') 
            {