Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to do this in PHP

Tags:

php

i am using this code to generate rows depending on the value in the database, I'm sure there is a more efficient way of doing this but I'm unsure how!

if ($empty == 1) {
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
}
if ($empty == 2) {
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
}
if ($empty == 3) {
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
}
if ($empty == 4) {
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
    echo "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
}
like image 607
Brad Fletcher Avatar asked Nov 28 '22 10:11

Brad Fletcher


1 Answers

You can do it using for loop:

$empty = 5;
$str = '';
for(i=0;$i<$empty;$i++) {
    $str .= "<li class='col-sm-4'><div class='horse-wrap empty'>Empty</div></li>";
}
echo $str;
like image 194
Jayesh Chitroda Avatar answered Dec 09 '22 14:12

Jayesh Chitroda