vmicchia Posted October 27, 2010 Share Posted October 27, 2010 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. Quote Link to comment Share on other sites More sharing options...
micah1701 Posted October 27, 2010 Share Posted October 27, 2010 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? Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 27, 2010 Share Posted October 27, 2010 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. Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 27, 2010 Author Share Posted October 27, 2010 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"; } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 27, 2010 Share Posted October 27, 2010 problem is you do this once for no reason, which removes the first record: $records = mysql_fetch_assoc($resultGetImages); // REMOVE THIS Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 28, 2010 Author Share Posted October 28, 2010 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. Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 28, 2010 Author Share Posted October 28, 2010 Awesome writing a second query worked just great. Thank you so much for everyone's help and suggestions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.