I'm trying to create a "Ratings wall" which is a table that shows people's ratings of a place. The ratings are accessed from a JSON file (ratings.json) and are then displayed on the wall. The problem I'm having is I want the star rating that the person has given the place to appear on the wall in the form of yellow stars. The process I use for this is to: Use a for loop to get all of the ratings, (they are only a number (1-5), so the output would look like "152423". And I want each number for each persons rating to show up in star form so my desired result would look something like this: Desired Result
But at the moment, the stars are appearing, but $rating seems to be defined as 5 for some reason. I feel like it has something to do with the JSON being decoded and the $decode[$i]['rating'] being in array form.
Any help would be appreciated.
Code below:
<html>
<head>
<link rel="stylesheet" href="comments.css" />
<title>Lonely Bris</title>
</head>
<body>
<table class="table">
<?php
$output = file_get_contents("ratings.json");
$decode = json_decode($output, true);
for($i = 0; $i < count($decode); $i++) {
$decode[$i]['rating'];
$rating = $decode[$i]['rating'];
}
$star = '<div class="rate"><input type="radio" id="star" name="rate"/><label for="star" title="text">1 star</label></div>';
if($rating == 1){
$rating = str_repeat($star, 1);
}
elseif($rating == 2){
$rating = str_repeat($star, 2);
}
elseif($rating == 3){
$rating = str_repeat($star, 3);
}
elseif($rating == 4){
$rating = str_repeat($star, 4);
}
elseif($rating == 5){
$rating = str_repeat($star, 5);
}
else{
$rating = "Unknown.";
}
echo "<tr><th></th><th>Review</th><th>Rating</th><th>Image?</th></tr>";
for($i = 0; $i < count($decode); $i++) {
$imgUrl = $decode[$i]['image'];
echo "<tr class=border_bottom><td>" . "<div class=name>" . $decode[$i]['name'] . "</div>" . " - " . $rating . "<br><br>" . $decode[$i]['review'] . "</td>";
?>
<td><img src="<?php echo $imgUrl; ?>" height="70" width="120" class="img-thumbnail" /></td></tr>
<?php
}
?>
</table>
</body>
</html>
JSON file (ratings.json)
[
{
"name": "James",
"event": "Test",
"review": "Test",
"rating": "5",
"image": "http://localhost/ClassIA3/images/RobloxScreenShot20210104_132131740.png"
},
{
"name": "Sup",
"event": "The Storytellers",
"review": "Test",
"rating": "5",
"image": "http://localhost/ClassIA3/images/RobloxScreenShot20201012_195012147.png"
},
{
"name": "Pont",
"event": "ACTivate children's club for 3-4 years",
"review": "Test",
"rating": "4",
"image": "http://localhost/ClassIA3/images/RobloxScreenShot20210101_181318133.png"
},
{
"name": "Hello2",
"event": "Flexibility and core training",
"review": "Was so good for my core",
"rating": "5",
"image": "http://localhost/ClassIA3/images/RobloxScreenShot20201120_213311711.png"
},
{
"name": "Hello2",
"event": "The Letter 'B'",
"review": "SO Cool",
"rating": "1",
"image": "http://localhost/ClassIA3/images/untitled.png"
},
{
"name": "Sup",
"event": "Dragon boating",
"review": "Origami!",
"rating": "4",
"image": "http://localhost/ClassIA3/images/Capture.PNG"
},
{
"name": "Hello2",
"event": "Brisbane Greeters - Paddington walking tour",
"review": "I loved it!",
"rating": "5",
"image": "http://localhost/ClassIA3/images/OLD TECH ROOMS.jpg"
}
]
See Question&Answers more detail:os