Jump to content

PHP Loop change after the first Iteration


ibz786

Recommended Posts

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

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++;
}
?>

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
}

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.

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.