tmyonline Posted November 13, 2007 Share Posted November 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 The "@" is very dangerous when you're trying to debug code, since is suppresses error messages. Remove it and see if you get an error. Can you post the code before this snippet that shows the query? Ken Quote Link to comment Share on other sites More sharing options...
koen Posted November 13, 2007 Share Posted November 13, 2007 There could be a lot of things going wrong here. What's the contents of your $imgArray? If they are a filename, is your path correct? Quote Link to comment Share on other sites More sharing options...
tmyonline Posted November 13, 2007 Author Share Posted November 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
tmyonline Posted November 13, 2007 Author Share Posted November 13, 2007 Hi Koen: The content of the ImgArray contains URL of the images. There is a set of images associated with different values of k and I have tried different k values. Thanks! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
tmyonline Posted November 13, 2007 Author Share Posted November 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 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 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.