Jump to content

How To Make This Code Neater


freemancomputer

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/272262-how-to-make-this-code-neater/
Share on other sites

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);

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.