Jump to content

[SOLVED] Help with 'for' loop.


raryre23

Recommended Posts

Firstly I'm not even sure if it should be a for loop or whether it should be a while one, or what the difference even is.

 

But anyway it seems to be working fine apart from one thing.

 

It's meant to display up to seven images one after each other but the problem is that if there is any less than seven images in the database then it just displays all of the images it has and then error messages. How can I get it so that it does not display any error messages?

 

<?php

$sql="select * from images where (username= '$username') ORDER BY picurl"; 
$res=mysql_query($sql); 

for($i=0; $i < 7; $i++) {
$picurl= mysql_result($res,$i,"picurl");
?>



          <img src="<?php print $picurl ?>" />
       
	  
<?php
}

?>

 

 

Any help would be greatly appreciated.  :)

Link to comment
https://forums.phpfreaks.com/topic/114449-solved-help-with-for-loop/
Share on other sites

one way

 

<?php

$sql="select * from images where (username= '$username') ORDER BY picurl"; 
$res=mysql_query($sql);

$limit = min (7, mysql_num_rows($res));

for($i=0; $i < $limit; $i++) {
         $picurl= mysql_result($res,$i,"picurl");
         echo "<img src='$picurl' />";
}

?>

one way

 

<?php

$sql="select * from images where (username= '$username') ORDER BY picurl"; 
$res=mysql_query($sql);

$limit = min (7, mysql_num_rows($res));

for($i=0; $i < $limit; $i++) {
         $picurl= mysql_result($res,$i,"picurl");
         echo "<img src='$picurl' />";
}

?>

 

It's working great - thank you  ;D

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.