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 Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/ 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 Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/#findComment-390836 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? Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/#findComment-390839 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 Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/#findComment-390844 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! Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/#findComment-390850 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 Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/#findComment-390855 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. Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/#findComment-390885 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 Link to comment https://forums.phpfreaks.com/topic/77197-whats-wrong-with-this-little-piece-of-code/#findComment-390906 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.