ibz786 Posted February 12, 2013 Share Posted February 12, 2013 Hi there guys Basically im trying to call images from my SQL DB (which is fine) and im trying to echo them out using a while loop (which is also fine) <?php $getSize = mysql_query("SELECT image_location FROM size_chart WHERE prod_size_id = $GET_ID"); while($chart = mysql_fetch_array($getSize)) { ?> <a href="<?php echo $chart ?>"><img src="<?php echo $chart ?>" width="200px" height="200px" /></a> <?php } ?> So in turn my results display 200 x 200 images of whatever im trying to call from my DB However i wish to display it as that from my array loop, i wish for the first picture to be 200 x 200 then the rest are 100 x 100 If anyone can please help me i would be most grateful Thank You Quote Link to comment https://forums.phpfreaks.com/topic/274415-php-loop-change-after-the-first-iteration/ Share on other sites More sharing options...
codebyren Posted February 12, 2013 Share Posted February 12, 2013 Set a counter outside of your loop and increment it within your loop. If the counter is equal to its starting value then image = 200x200, otherwise image = 100x100. $getSize = mysql_query("SELECT image_location FROM size_chart WHERE prod_size_id = $GET_ID"); $i = 1; while($chart = mysql_fetch_array($getSize)) { $dimension = ($i == 1) ? '200px' : '100px'; ?> <a href="<?php echo $chart ?>"><img src="<?php echo $chart ?>" width="<?php echo $dimension; ?>" height="<?php echo $dimension; ?>" /></a> <?php $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/274415-php-loop-change-after-the-first-iteration/#findComment-1412070 Share on other sites More sharing options...
Psycho Posted February 12, 2013 Share Posted February 12, 2013 Hmm. . . if you are just going to set $dimension on each loop - no need to use the ternary operator and make the PHP processor do more work by doing a comparison check. Just set the value to 200 before the loop and to 100 at the end of each iteration. $query = "SELECT image_location AS src FROM size_chart WHERE prod_size_id = $GET_ID"; $result = mysql_query($query); $size = '200'; //Set size for first iteration while($row = mysql_fetch_assoc($getSize)) { echo "<a href=\"{$row['src']}\"><img src=\"{$row['src']}\" width=\"{$size}px\" height=\"{$size}px\" /></a>\n"; $size = 100; //Set size for subsequent iterations } Quote Link to comment https://forums.phpfreaks.com/topic/274415-php-loop-change-after-the-first-iteration/#findComment-1412074 Share on other sites More sharing options...
codebyren Posted February 12, 2013 Share Posted February 12, 2013 Hmm. . . if you are just going to set $dimension on each loop - no need to use the ternary operator and make the PHP processor do more work by doing a comparison check. Just set the value to 200 before the loop and to 100 at the end of each iteration. Schooled. It's obvious now that you point it out. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/274415-php-loop-change-after-the-first-iteration/#findComment-1412075 Share on other sites More sharing options...
Barand Posted February 12, 2013 Share Posted February 12, 2013 BTW, the image source will be in $chart[0], not $chart. Quote Link to comment https://forums.phpfreaks.com/topic/274415-php-loop-change-after-the-first-iteration/#findComment-1412076 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.