Jump to content


Photo

PHP Loop change after the first Iteration

php while mysql

  • Please log in to reply
4 replies to this topic

#1 ibz786

ibz786

    Member

  • Members
  • PipPip
  • 29 posts
  • LocationLondon

Posted 12 February 2013 - 04:03 PM

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

#2 behicthebuilder

behicthebuilder

    Advanced Member

  • Members
  • PipPipPip
  • 156 posts
  • LocationNew Zealand

Posted 12 February 2013 - 04:17 PM

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

Start procrastinating early. That way you might actually start something on time...

#3 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,921 posts
  • LocationCanada

Posted 12 February 2013 - 04:31 PM

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
}

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#4 behicthebuilder

behicthebuilder

    Advanced Member

  • Members
  • PipPipPip
  • 156 posts
  • LocationNew Zealand

Posted 12 February 2013 - 04:36 PM

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.
Start procrastinating early. That way you might actually start something on time...

#5 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,650 posts
  • LocationCheshire, UK

Posted 12 February 2013 - 04:50 PM

BTW, the image source will be in $chart[0], not $chart.

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com