freemancomputer Posted December 21, 2012 Share Posted December 21, 2012 I currently have this code that shows a certain number of images depending on what the rating is 1-5. I know that there has to be a better way of doing this, but i can not think of it. Any pointers would be helpful. if($user_rating_show==1) { echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; } if($user_rating_show==2) { echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; } if($user_rating_show==3) { echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; } if($user_rating_show==4) { echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; } if($user_rating_show==5) { echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; } Thanks Quote Link to comment Share on other sites More sharing options...
mds1256 Posted December 21, 2012 Share Posted December 21, 2012 (edited) do a for loop top of my head: for ($i=1; $i<=$user_rating_show; $i++) { echo "<img src='../img/beer_mug_finish-green.jpg' alt='been_m' name='beer_mug' width='34' height='33' />"; } Edited December 21, 2012 by mds1256 Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 21, 2012 Share Posted December 21, 2012 (edited) I assume you want the number of images displayed to be the same as the value of $user_rating_show in which case you can just create a loop for($i = 1; $i <= $user_rating_show, $i++) { echo "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>"; } or you could use str_repeat(), but I would prefer the loop for ease of understandability echo str_repeat("<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>", $user_rating_show); Edited December 21, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 21, 2012 Share Posted December 21, 2012 <?php $img = "<img src='../img/beer_mug_finish-green.jpg' alt='beer_m' name='beer_mug' width='34' height='33'>\n"; if($user_rating_show >= 1 && $user_rating_show <= 5){ echo str_repeat($img,$usr_rating_show); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.