I'm have made a search function in PHP for my MySQL database. The search works but I want to display the result in a special way. I'm using cards to show the tables where some information is from the table (LessonPlans) I search and another information is from another table (Images). The original code was this:
<?php
require_once("db_link.inc.php");
if(isset($_GET['search'])) {
$search = $link->escape_string($_GET['search']);
$query = $link->query("SELECT Subject, Level, Aim FROM LessonPlans WHERE Subject LIKE '%".$search."%' OR Level LIKE '%".$search."%' OR Aim LIKE '%".$search."%' AND Language='English'");
if($query->num_rows){
while($r = $query->fetch_object()){
echo '<div class="col s6 m4 l3">
<div class="card">
<div class="card-image waves-effect waves-block waves-light">
<img class="activator" src="../mlslp/assets/img/'. $r->Name-img .'" alt="'. $r->Alt-img .'">
</div>
<div class="card-content">
<span class="card-title activator grey-text text-darken-4 truncate tooltipped" data-position="bottom" data-delay="800" data-tooltip="'.$r->Subject.'">'.$r->Subject.'</span>
<p class="blue-text">'.$r->Level.'</p>
</div>
<div class="card-action">
<a href="lessonplan.php?Id='. $r->Id .'">Open Lesson Plan</a>
</div>
<div class="card-reveal">
<span class="card-title grey-text text-darken-4">'.$r->Subject.'<i class="material-icons right">close</i></span>
<p>'.$r->Aim.'</p>
</div>
</div>
</div>';
}
}
}
?>
The tables I want to use are:
LessonPlans
Id | ImageId | Subject | Level | Aim | Language
and
Image
ImageId | Name-img | Alt-img
As it is it shows the correct result from LessonPlans but not the information that is not searchable. I know it will not work like this and I've tried with UNION
, JOIN
and as well with just two SELECT
queries. As soon as I add the Images
table into the query I get this error message:
Notice: Trying to get property of non-object in ...
From what I understand, this is because $query
is not an object when I add another table. What I don't understand is how I get around this. There must be a way to search one table but display results from two tables. Does anyone know how to do this?
What query do I need and how do I display it all?