Jump to content

Image display help


vmicchia

Recommended Posts

Hey there I am having a problem where I am pulling the records for images from my database and then trying to display them. What happens is there are three images in the database and I want to display them. My code displays the images but it only shows two of the three.  I know there are three images in the database I put them in and checked just to make sure. It just seems to only want to display two images instead of three.

 

Here's the code:

$getImages = "SELECT * FROM Images WHERE SKU = '".$sku."'";
$resultGetImages = mysql_query($getImages);
$records = mysql_fetch_assoc($resultGetImages);
?>
     while($records = mysql_fetch_assoc($resultGetImages)){
                
        echo "<a onclick=\"document.getElementById('placeholder').src='../../".$records['Path']."'\" target=\"_blank\"><img src=\"../../".$records['ThumbPath']."\" alt=\"".$records['Description']."\" height=\"".$records['ThumbWidth']."\" /></a>\n";
      }
?>

 

I have tried a for loop as well but it displayed more images than were in the database and just a single image over and over again. I know that was a flaw in my code and I'm not even sure a for loop would even be a reasonable way to go.

Link to comment
Share on other sites

could be a couple things.  are you sure you really have 3 images where SKU = $sku?  perhaps there is a whitespace or a capitalization difference you're not noticing.

 

could it be that you're missing the path or some other info in the database?  when you run the script and view the outputted HTML source code, does it only show HTML for 2 images? or does it show 3 but one of them isn't displaying?

Link to comment
Share on other sites

It could quite simply be that you have not enclosed you php tags correctly...

 

Your code...

$getImages = "SELECT * FROM Images WHERE SKU = '".$sku."'";
$resultGetImages = mysql_query($getImages);
$records = mysql_fetch_assoc($resultGetImages);
?>
     while($records = mysql_fetch_assoc($resultGetImages)){
                
        echo "<a onclick=\"document.getElementById('placeholder').src='../../".$records['Path']."'\" target=\"_blank\"><img src=\"../../".$records['ThumbPath']."\" alt=\"".$records['Description']."\" height=\"".$records['ThumbWidth']."\" /></a>\n";
      }
?>

 

Corrected code...

$getImages = "SELECT * FROM Images WHERE SKU = '".$sku."'";
$resultGetImages = mysql_query($getImages);
$records = mysql_fetch_assoc($resultGetImages);

     while($records = mysql_fetch_assoc($resultGetImages)){
                
        echo "<a onclick=\"document.getElementById('placeholder').src='../../".$records['Path']."'\" target=\"_blank\"><img src=\"../../".$records['ThumbPath']."\" alt=\"".$records['Description']."\" height=\"".$records['ThumbWidth']."\" /></a>\n";
      }
?>

 

Tell me how it goes.

Regards, Paul.

Link to comment
Share on other sites

micah1701,

When I look at the page source it only has the code for the 2 images I am seeing. When I echo out my sql statement and run it directly in the database it returns all 3 records of the images.

 

PaulRyan,

I apologize for the mis-tagging it was due to me removing some extraneous lines of code that I have since also removed from my source file. here is the code as it is now:(not the first image file is a place holder but is also one of the images, it should just be the first image from the database.)

 

 <?php
include('../../php/includes/openDbConn.php');
$sku = 'c-111111';
$getImages = "SELECT * FROM Images WHERE SKU = '".$sku."'";
$resultGetImages = mysql_query($getImages);
$records = mysql_fetch_assoc($resultGetImages);
$num_rows = mysql_num_rows($resultGetImages);
//echo $num_rows;
//echo $getImages;
?>
   <img src="../../<?php echo $records['Path']; ?>" id="placeholder" alt="<?php echo $records['Description']; ?>" height="<?php echo $records['Width']; ?>" />
    <div id="thumbImages">
      <?php
     while($records = mysql_fetch_assoc($resultGetImages)){
        echo "<a onclick=\"document.getElementById('placeholder').src='../../".$records['Path']."'\" target=\"_blank\"><img src=\"../../".$records['ThumbPath']."\" alt=\"".$records['Description']."\" height=\"".$records['ThumbWidth']."\" /></a>\n";
      }
?>

Link to comment
Share on other sites

problem is you do this once for no reason, which removes the first record:

 

$records = mysql_fetch_assoc($resultGetImages); // REMOVE THIS

 

I do that in order to populate the first Image field. So what I need to do is make a separate sql for that first image? Ok I'll give that a try. thank you I didn't know that would remove the first record.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.