user5035451
user5035451

Reputation:

Query to database for showing all images from same tag

I have page where are shown images from database and under the images are tags for each image. database tables are: images

image_id
image_name
etc

tag

tag_id
tag_name

tags_images

tag_id
image_id

Under the image I select and show them like this

<li>Tags: </li>';
            $tags = $pdo->prepare("SELECT * FROM `images` p LEFT JOIN `tags_image` tp ON p.image_id = tp.image_id LEFT JOIN `tag` t ON tp.tags_id = t.tag_id WHERE p.image_id = ?");
            $tags -> bindParam(1, $row['image_id'], PDO::PARAM_INT);
            $tags -> execute();
            foreach($tags as $tag) {
                echo '<li><a href="tagsPreview.php?tag_id='.$tag['tag_id'].'">'.$tag['tag_name'].'</li>';  
            }

Now I want when user click on the href link on the tagsPreview.php page to load all images which have this tag. What I tried is

if(isset($_GET['tag_id']) && is_numeric($_GET['tag_id'])){
                    $tag_id = $_GET['tag_id']; {                         

$result = $pdo->prepare("SELECT * from `images` i 
                         INNER JOIN `tags_image` ti ON i.image_id = ti.image_id 
                         WHERE ti.tags_id = ? ASC LIMIT 20");

$result -> bindParam(1, $row['tag_id'], PDO::PARAM_INT);
$result -> execute();
foreach ($result as $row)
{
    // images
}

The problem is that I get empty page with no images.

Upvotes: 1

Views: 109

Answers (1)

Amrut Gaikwad
Amrut Gaikwad

Reputation: 552

Replace this:

$result -> bindParam(1, $row['tag_id'], PDO::PARAM_INT);

with

$result -> bindParam(1, $tag_id, PDO::PARAM_INT);

Upvotes: 2

Related Questions