Reputation: 1
I have a family recipe site that I am working on. Each recipe has a star rating that is stored in a mysql database (integer between 1 and 5). Currently, these ratings are displayed using html class=fa fa-star. So 5 lines need to be added to the code to display 5 stars. I want to dynamically generate these stars depending on the star rating in the database. The recipe list page (desserts.php) calls a function (stored in another php file) to populate the list of recipes in the desserts category dynamically depending on the recipes stored in the database.
<!-- desserts.php -->
<!-- ##### New Recipe Area Start. This section displays all recipes from the database. ##### -->
<section class="small-receipe-area section-padding-80-0">
<div class="section-heading">
<h3>Dessert Recipes</h3>
</div>
<div class="container">
<div class="row">
<?php
while ($row = mysqli_fetch_assoc($result)){
list_of_recipes($row['recipe_name'], $row['recipe_img'], $row['recipe_link'], $row['stars'], $row['date_entered']);
}
?>
</div>
</div>
</section>
<!-- ##### New Recipe Area End ##### -->
<!-- recipe_list_component.php -->
<!-- ##### This function is called by desserts.php for every recipe found in the database in this category. ##### -->
<?php
function list_of_recipes($recipename, $recipeimg, $recipelnk, $stars, $recipedate){
$i = "";
$element = "
<!-- New Recipe Area -->
<div class=\"col-12 col-sm-6 col-lg-4\">
<div class=\"single-small-receipe-area d-flex\">
<!-- Recipe Thumb -->
<div class=\"receipe-thumb\">
<img src=\"$recipeimg\" alt=\"caramel_sauce\">
<!--<img src=\"img/bg-img/caramel1_thumb.jpg\" alt=\"\">-->
</div>
<!-- Recipe Content -->
<div class=\"receipe-content\">
<span>$recipedate</span>
<a href=\"$recipelnk\">
<h5>$recipename</h5>
</a>
<div class=\"ratings\">
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
<i class=\"fa fa-star\" aria-hidden=\"true\"></i>
</div>
<p>0 Comments</p>
</div>
</div>
</div>
";
echo $element;
}
?>
In the function, I receive the number of stars recorded in the database ($stars). I want to use this number to generate this HTML line <i class="fa fa-star" aria-hidden="true"> which shows a star in the rating area. I believe a For Loop is needed, but I am not sure how to generate the HTML statement so that if $stars = 4, I will see 4 stars for that recipe on the desserts page.
Thanks
Upvotes: 0
Views: 3147
Reputation: 78994
Just repeat the string that number of times:
$element = "
--code--
<div class=\"ratings\">"
. str_repeat('<i class="fa fa-star" aria-hidden="true"></i>', $stars) .
"</div>
--code--";
Or store it in a variable and use it as you use the others:
$star_list = str_repeat('<i class="fa fa-star" aria-hidden="true"></i>', $stars);
Upvotes: 1
Reputation: 742
use a loop inside function
for($stars > 5;$stars--;){
echo "<i class=\"fa fa-star\" aria-hidden=\"true\"></i>\n";
}
so your function must be like this
<?php
function list_of_recipes($recipename, $recipeimg, $recipelnk, $stars, $recipedate){
$i = "";
$star = "";
for($stars > 5;$stars--;){
$star .= "<i class=\"fa fa-star\" aria-hidden=\"true\"></i>\n";
}
$element = "
<!-- New Recipe Area -->
<div class=\"col-12 col-sm-6 col-lg-4\">
<div class=\"single-small-receipe-area d-flex\">
<!-- Recipe Thumb -->
<div class=\"receipe-thumb\">
<img src=\"$recipeimg\" alt=\"caramel_sauce\">
<!--<img src=\"img/bg-img/caramel1_thumb.jpg\" alt=\"\">-->
</div>
<!-- Recipe Content -->
<div class=\"receipe-content\">
<span>$recipedate</span>
<a href=\"$recipelnk\">
<h5>$recipename</h5>
</a>
<div class=\"ratings\">
$star
</div>
<p>0 Comments</p>
</div>
</div>
</div>
";
echo $element;
}
Upvotes: 0