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

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

You are retrieving an associative array, yet you are trying to loop through it with a numerical index.

 

Put

<?php
echo '<pre>' . print_r($imgArray,true) . '</pre>';
?>

before the "for loop" to see what is being returned.

 

Ken

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.