tables sample data
walls
wall_id wall_name
1 wall_1
2 wall_2
6 wall_6
wall_categories
wall_id category_id
1 2
2 1
6 1
6 2
categories
category_id category_name
1 Wallpaper
2 Photography
html
<a href="file.php?sort=upload_date">Date</a>
<a href="file.php?sort=wall_views">Views</a>
<a href="file.php?sort=wall_downloads">Downloads</a>
<a href="file.php?filter=1">Wallpaper</a>
<a href="file.php?filter=2">Photography</a>
php
$sortBy = array('upload_date','wall_views','wall_downloads');
$sort = 'upload_date';
if(isset($_GET['sort']) && inarray($_GET['sort'], $sortBy)){
$sort = $_GET['sort'];
}
$filterBy = array(1, 2);
$filter = join(',', $filterBy);
if(isset($_GET['sort']) && inarray($_GET['sort'], $sortBy)){
$sort = $_GET['sort'];
}
I'm trying to pull
wall_id
, wall_name
, wall_views
from walls
table and category_name
from categories table based on wall_categories
table. Below is the query I got:
$walls_query = $connectDB->prepare("SELECT * FROM walls INNER JOIN wall_categories ON
walls.wall_id = wall_categories.wall_id WHERE wall_categories.category_id
IN ($filter) AND walls.wall_id>? ORDER BY $sort DESC;");
$query->execute();
and here is the query that selects the categories from database using the wall_id
$categories_query = $connectDB->prepare("SELECT category_name FROM categories INNER JOIN wall_categories
ON wall_categories.category_id = categories.category_id WHERE wall_id=?;");
The problem is it generates duplicate data, the images that have two categories are displayed twice. Need to stop that.