Jump to content

Retrieve mySql field that contains comma-separated list


mainstreetop

Recommended Posts

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.

Link to comment
Share on other sites

@ 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 />;
}
  }
}

Link to comment
Share on other sites

@ 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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@ 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?

Link to comment
Share on other sites

@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???

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.