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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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