I’m currently trying to link up database names with image filenames, and I’ve been successful in doing this so far. However, when I try to put these names on-top of an image, it only takes the last set of the names and puts that on all of the images. This isn’t what I’m trying to do.
I want to be able to take a filename, find it in the database, and then replace it with the name that corresponds with that post. (I my case they’re artists, so if the filename was original-5192eef6f06cc.jpg, I would replace it with This Super Cool Band Name, and overlay ontop of the individual image.
Here is my code
<?php
$hostname="localhost";
$username="root";
$password="";
$db = "sbp old 2";
$imagesDirectory = "images/";
if(is_dir($imagesDirectory))
{
$opendirectory = opendir($imagesDirectory);
while (($image = readdir($opendirectory)) !== false)
{
if(($image == '.') || ($image == '..'))
{
continue;
}
$imgFileType = pathinfo($image,PATHINFO_EXTENSION);
$string = "600x600";
if(strpos($image, $string) !== false) {
}
else if(($imgFileType == 'jpg') || ($imgFileType == 'png') || ($imgFileType == 'jpeg')) {
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT REPLACE("'.$image.'", "'.$image.'", name) basename, name FROM media, artists where media.id = artists.id') as $row) {
$output = '<h6 class="text2">'.$row['basename'].'</h6>';
}
echo '<div class="img-container">';
echo "<a href='images/".$image."'><img src='images/".$image."' class='actimage' width=300' height='300'></a>";
echo "<a href='images/".$image."'>";
echo '<div class="overlay">';
echo '<h6 class="text">'.$image.'</h6>'; //FileName Text
echo '<h6 class="text2">'.$output.'</h6>'; //FileName Text
echo '</div>';
echo '</a>';
echo '</div>';
}
}
closedir($opendirectory);
}
?>
The part that handles the finding and replacing of the text is:
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT REPLACE("'.$image.'", "'.$image.'", name) basename, name FROM media, artists where media.id = artists.id') as $row) {
$output = '<h6 class="text2">'.$row['basename'].'</h6>';
}
Is anyone able to assist me here?
Thanks
Advertisement
Answer
This is the query…
foreach($dbh->query('SELECT REPLACE("'.$image.'", "'.$image.'", name) basename, name FROM media, artists where media.id = artists.id') as $row) {
And you should be able to see the fields here: “basename” and “name”. If you print “basename” with this…
$output = '<h6 class="text2">'.$row['basename'].'</h6>';
Then print the “name” with this…
$output = '<h6 class="text2">'.$row['name'].'</h6>';