Jump to content

What's wrong with this little piece of code! ?


tmyonline

Recommended Posts

Hi guys:

 

Would you please kindly tell me what's wrong with the following code.  I have an array of images and I want to display them.

 

<?php

$imgArray  = @mysql_fetch_assoc($imgResult);

for ($i = 1; $i <= count($imgArray); $i++) {

?> <img src="<?php echo $imgArray[$i]; ?>" alt="image<?php echo $i; ?>" width="100" height="100" />

<?php } ?>

 

Somehow, the images do not appear.  Thanks a lot!

 

T

Link to comment
Share on other sites

Hi Ken,

 

This is what I have:

 

<?php

$k = 15;

 

$imgSQL    = "SELECT experiences_loc_item_jn.LOC_key, loc_item.thumbnail FROM experiences_loc_item_jn, loc_item WHERE              experiences_loc_item_jn.experiences = $k AND experiences_loc_item_jn.LOC_key = loc_item.LOC_key";

$imgResult = mysql_db_query($db,$imgSQL,$cid);

$imgArray  = mysql_fetch_assoc($imgResult);

 

for ($i = 1; $i <= count($imgArray); $i++) {

?> <img src="<?php echo $imgArray[$i]; ?>" alt="image<?php echo $i; ?>" width="100" height="100" />

<?php } ?>

 

Thanks a lot.

Tommy

Link to comment
Share on other sites

Hi Ken,

 

My understanding is that an associate array is one that has its keys as "string".  In my case, the elements (not the keys) of the array contain string values.  So,... how can this be an associative array ?  Please correct me if I'm wrong.

 

I have put your piece of code above the for-loop as you suggest.  This is what it returns:

 

 

Array

(

    [LOC_key] => 13

    [thumbnail] => http://memory.loc.gov/service/pnp/cph/3c10000/3c14000/3c14700/3c14749t.gif

)

 

 

So, it returned the URL of the image.  How come the image did not appear then ! ?  Thanks.

Link to comment
Share on other sites

You will notice that the indices of the returned array are the strings "LOC_key" and "thumbnail", not 0 & 1.

 

I think the following modification to your code will give you something closer to what you're trying to do:

<?php
<?php
$k = 15;

$imgSQL    = "SELECT experiences_loc_item_jn.LOC_key, loc_item.thumbnail FROM experiences_loc_item_jn, loc_item WHERE              experiences_loc_item_jn.experiences = $k AND experiences_loc_item_jn.LOC_key = loc_item.LOC_key";
$imgResult = mysql_db_query($db,$imgSQL,$cid);
$i = 0;
while ($imgArray  = mysql_fetch_assoc($imgResult)) {
    echo '<img src="' . $imgArray['thumbnail'] .'" alt="image' . $i . '" width="100" height="100" />';
    $i++;
}?>

 

Ken

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.