mainstreetop Posted February 27, 2010 Share Posted February 27, 2010 Here's my code so far... $query = mysql_query ('SELECT AlternateImage FROM item'); while ($row = mysql_fetch_assoc($result)) { $Image = $row['ItemImage']; } AlternateImage contains a comma-separated list of filenames (i.e. 1.jpg, 2.jpg, 3.jpg). How can I retrieve those filenames and display them as separate variables? I'm thinking I need a FOREACH statement, but am not clear on how to set this. Thank you. Quote Link to comment Share on other sites More sharing options...
jskywalker Posted February 27, 2010 Share Posted February 27, 2010 i think you need 'split' http://www.php.net/split Quote Link to comment Share on other sites More sharing options...
harristweed Posted February 27, 2010 Share Posted February 27, 2010 $Image="1.jpg,2.jpg,3.jpg"; $jpg_array=explode(",",$Image); foreach ($jpg_array as $key => $value) { echo" $key = $value<br />\n"; } Quote Link to comment Share on other sites More sharing options...
jskywalker Posted February 27, 2010 Share Posted February 27, 2010 indeed explode might be better ;-) Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted February 27, 2010 Author Share Posted February 27, 2010 @ harristweer... I'm sorry, but I should have noted that I do not know the image filenames. It's an item table that contains about 30,000 items. Each item may or may not contain an AlternateImage()s. So, how to I modify your code to account for unknown filenames? Do I set $Image = $row['AlternateImage'];? If so, then I'm not exactly sure how to insert the foreach into the while statement?? Maybe something like this??? while ($row = mysql_fetch_assoc($result)) { foreach ($Image = $row['ItemImage'] as $AltImage) { $jpg_array=explode(",",$AltImage); foreach ($jpg_array as $key => $value) { echo" $key = $value<br />; } } } Quote Link to comment Share on other sites More sharing options...
harristweed Posted February 27, 2010 Share Posted February 27, 2010 Possibly... while ($row = mysql_fetch_assoc($result)) { $jpg_array=explode(",",$row['ItemImage']); foreach ($jpg_array as $key => $value) { echo" $key = $value<br />\n";//or whatever you are doing with the results... } } Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted February 27, 2010 Author Share Posted February 27, 2010 @ harristweed... Your code worked perfectly! I just have one more detail to remedy... Now I need to take each of those file names and append them to a path like this: echo "<a><img src='http://www.website.com/images/" . $path . " '></a>"; So, how do I reference $path foreach occurrence of $key => $value. Does that make sense? Quote Link to comment Share on other sites More sharing options...
harristweed Posted February 27, 2010 Share Posted February 27, 2010 foreach ($jpg_array as $path) { echo "<a><img src='http://www.website.com/images/" . $path . " '></a>"; } Quote Link to comment Share on other sites More sharing options...
jskywalker Posted February 27, 2010 Share Posted February 27, 2010 I just have one more detail to remedy... can you...... Quote Link to comment Share on other sites More sharing options...
harristweed Posted February 27, 2010 Share Posted February 27, 2010 Sure, would you like me to write the solution on a $100 bill and post it to you? Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted February 27, 2010 Author Share Posted February 27, 2010 @ harristweed... As soon as I'm up and running and starting to sell some of these products (office supplies), I'll be sure to send you something for your help. Maybe an ink or toner cartridge? Do I contact you through the 'PM'? Quote Link to comment Share on other sites More sharing options...
harristweed Posted February 27, 2010 Share Posted February 27, 2010 Kind thought but not necessary. Glad to help. Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted February 27, 2010 Author Share Posted February 27, 2010 Well, if the weight of a specific type of paper or the thickness of a pencil lead has you stumped, you know who to ask. I should have paid more attention in school! Maybe before too long, you'll see another posting of mine. I'll include an attachment of what I'm sure will be one of the two hairs I have remaining by then. Thanks again and take care. Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted February 28, 2010 Author Share Posted February 28, 2010 @ harristweed... Not sure if you're still monitoring this thread, but I am noticing one small problem that I can't seem to figure. My mySql field contains this data which is separated by semi-colons: 1.JPG; 2.JPG; 3.JPG I'm exploding this file using (as you helped previously): $jpg_array = explode(';',$row['Images']); // THIS WORKS WELL Trying to display each image by: foreach ($jpg_array as $key => $path) { echo "<img src='http://www.website.com/images/" . $path . "' />"; } If I echo $path . "<br/>": I get: 1.jpg 2.jpg 3.jpg Everything seems to be as expected if mySql field contains one image. However, when there are multiple images, there is a problem. When I echo the image with that foreach loop, only the first image is being displayed. The other two are not. Those 'invalid path' (sorry, I'm sure of their proper name) boxes are being rendered in the browser. Do you see what I'm missing? Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted February 28, 2010 Author Share Posted February 28, 2010 @harristweed... Never mind previous issue. I echoed the $path with the <br> and noticed the output as: 1.jpg 2.jpg 3.jpg There is a space between each filename. So, I changed the explode() to reflect the delimiter as a semi-colon followed by a space: $jpg_array = explode(", ", $row['Image']); Amazing how one simple character can cause so much frustration. I imagine, though, as I move along, I'll learn how to efficiently debug??? 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.