I have two tables with fields.
Right now I can get all the data I need in a two queries. But how can I combine the result or should I say mix them?
Here's my code:
$query = "SELECT * FROM product";
$select_product = $db->query($query);
while($row = $db->fetch_object($select_product)) {
$status = $row->status;
if($status == 'published') {
$product_id = $row->id;
$product_title = $row->title;
$product_image = $row->image;
echo '<div class="item">';
echo '<div class="title"><a href="/product/' . $product_id . '">' . $product_title . '</a></div>';
echo '</div>';
}
}
$query = "SELECT * FROM recipe";
$select_recipe = $db->query($query);
while($row = $db->fetch_object($select_recipe)) {
$status = $row->status;
if($status == 'published') {
$recipe_id = $row->id;
$recipe_title = $row->title;
$recipe_image = $row->image;
echo '<div class="item">';
echo '<div class="title"><a href="/recipe/' . $recipe_id . '">' . $recipe_title . '</a></div>';
echo '</div>';
}
}
As you can see from the code, the html item are in loops and display all the data from products and in a separate html, it displays the recipe loop item, but my goal is do it like this and add the image, of course:
<div class="product">
<div class="product_title">'the_value'</div>
<div class="product_image">'the_value'</div>
</div>
<div class="recipe">
<div class="recipe_title">'the_value'</div>
<div class="recipe_image">'the_value'</div>
</div>
<div class="product">
<div class="product_title">'the_value'</div>
<div class="product_image">'the_value'</div>
</div>
<div class="recipe">
<div class="recipe_title">'the_value'</div>
<div class="recipe_image">'the_value'</div>
</div>
... and so on ...
So far I've tried getting the data by using UNION clause in a single query,
$query = "SELECT * FROM product UNION SELECT * FROM recipe";
but I'm receiving an error:
Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given
Thanks.
EDIT2: Here are the tables:
Table name: recipe
fields: id, title, image
Table name: product
fields: id, title, image
All I want is to display the data mixed. Right now I'm displaying like this: recipe/recipe/recipe/product/product/product
and the goal is display like this:
recipe/product/recipe/product