Gibbs Posted January 24, 2007 Share Posted January 24, 2007 I'm looking for a better way to achieve what i'm trying to do...At the moment I have a database entry that is a number below 5. I want to echo something the same amount of times the number is. So if the number is 1 then it draws one picture, if it's 2 then two pictures.Now what I'm doing works, don't get me wrong, but surely there is a better and less time consuming method then what is being done below?[code=php:0]if ($resulta['demerits'] == 1){echo "<td width=\"18\"><img src=\"img/icons/demerit.jpg\" border=\"0\"></img></td>";}elseif ($resulta['demerits'] == 2){echo "<td width=\"18\"><img src=\"img/icons/demerit.jpg\" border=\"0\"></img></td>";echo "<td width=\"18\"><img src=\"img/icons/demerit.jpg\" border=\"0\"></img></td>";}[/code]Thanks for any replies :D Link to comment https://forums.phpfreaks.com/topic/35590-echo-variable-depending-on-its-number/ Share on other sites More sharing options...
kenrbnsn Posted January 24, 2007 Share Posted January 24, 2007 Use a "for" loop:[code]<?phpfor ($i=0;$i<$resulta['demerits'];$i++) echo '<td width="18"><img src="img/icons/demerit.jpg" border="0"></img></td>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/35590-echo-variable-depending-on-its-number/#findComment-168541 Share on other sites More sharing options...
Gibbs Posted January 24, 2007 Author Share Posted January 24, 2007 That's brilliant. I was actually thinking about for loops, havent used them for years though and completely forgot the method for them.Thanks! ;D Link to comment https://forums.phpfreaks.com/topic/35590-echo-variable-depending-on-its-number/#findComment-168544 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.