I’ve been trying to fiddle with this code to no avail, my goal is to… well basically what the title says, post the individual images, but all the code does is post everything from the folder and that’s not what I want… Here is the code:
$dir_contents = scandir($dir);
foreach ($dir_contents as $file)
{
$file_type = strtolower(end(explode('.', $file)));
if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true)
{
$animeimg = '<img src="' . $dir . '/' . $file . '" alt="' . $file . '" width="80px;" height="100px" />';
echo $file;
$template->assign_block_vars('animeimg', array(
'IMG' => $animeimg,
));
$sql = "SELECT image, name, description FROM phpbb_anime";
$result = $db->sql_query($sql);
}
}
Where did you define $file_display?
And… what’s that SQL query doing down there?
My bad, forgot to post that…
$file_display = array('jpg', 'jpeg', 'png', 'gif');
and does it matter where I put the SQL query? (PHP noob here, sry) and if that’s not the right spot, where should I put it?
It wasnt a question of positioning so much as “what is it actually DOING?” It doesnt appear to have any use - as soon as the next iteration of the loop is called, your query re-executes itself, and…still does nothing with the result.
If you need the information from the query later on in the script, pull the query out of the loop so you only run it once, and still have the result available.
When you say ‘post the individual images’… what do you mean? This script is clearly designed to post all image files in a directory.
Well I guess that’s idiotic of my part that SQL statement actual refers to another code that I haven’t posted lol
while ($row = $db->sql_fetchrow($result))
{
$template->assign_block_vars('anime', array(
'ANIME_NAME' => $row['name'],
'ANIME_DESC' => $row['description'],
));
}
and when I say post the individual images, I’m wanting to be able to define a variable that can loop through all the images like have the template variable ‘IMG’ and so I can put in the HTML template with the description and name, and keep looping until no more images are available, I don’t want to post all the images at the same time. I hope that makes sense.
Okay. Think we need to adjust some stuff.
You want the name, description, and image all tied together. So, lets put them all together.
I assume that the ‘image’ field in the sql table holds the file name of the image file.
SO. We dont need to scan the directory at all.
if(substr($dir,-1) != "/") { $dir .= "/"; }
$sql = "SELECT image, name, description FROM phpbb_anime";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$template->assign_block_vars('anime', array(
'ANIME_NAME' => $row['name'],
'ANIME_DESC' => $row['description'],
'ANIME_IMG' => (file_exists($dir.$row['image']) ? '<img src="'.$dir.$row['image'].'" alt="'.$row['image'].'" width="80px;" height="100px" />' : '<img src="missing.jpg" alt="Image Missing" width="80px;" height="100px" />')
));
}
Now we just need to modify your template file to loop correctly through the array we just sent. So if you could post the template file you’re using (or at least the relevant section), we can fix that.
Well I just tried to modify it myself, and when i did the image turned into this
<img src="missing.jpg" alt="Image Missing" width="80px;" height="100px">
but here is the template you requested
<!-- BEGIN anime -->
<div class="anime">
{anime.ANIME_IMG}
<h2>{anime.ANIME_NAME}</h2>
<p>{anime.ANIME_DESC}</p>
</div>
<!-- END anime -->
What is in the image field of the database?
the file path, the actual image is stored in a folder.
if it’s the full filepath, remove the $dir entirely, because you dont need it
I did that and still nothing, but I think the
<img src="missing.jpg" alt="Image Missing" width="80px;" height="100px">
error is coming
(file_exists($dir.$row['image']) ? '<img src="'.$dir.$row['image'].'" alt="'.$row['image'].'" width="80px;" height="100px" />' : '<img src="missing.jpg" alt="Image Missing" width="80px;" height="100px" />')
after the colon is where I see it happening, but i take it out, it just screws up entirely (very confused right now)
It’s a Ternary condition.
(ifcondition) ? trueresult : falseresult;
It’s evaluating (file_exists($dir.$row[‘image’]) as False. Which, if $row[‘image’] contains the full path, is the correct result. It should be (file_exists($row[‘image’])
instead.
Well I’m starting to understand it now, but I still can’t get the images to show up, and when I put the file path into the browser URL, the image will show up there, but not for the site, and I took out that first line of code, since you said $dir wasn’t needed anymore, I hope that didn’t mess anything up…
Is it still sending Image Missing out, instead of the image?
yep, still getting that missing.jpg
Couldn’t edit the post, but I finally got it working, the file path in the database was wrong lol, I had it as localhost/images/, when it was supposed to be just images/ but thank you for all your help StarLion, couldn’t have done without you
hehe well yes, that would certainly do it