Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
138 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are several things to fix/improve.

Most critically, $rating = $decode[$i]['rating']; in your first loop is overwriting the same variable $rating over and over. When the loop finishes, $rating will be the value accessed in the final iteration -- this is why you always see five stars with your sample data.

Furthermore, $decode[$i]['rating']; in the first loop does nothing; just delete the line of code.

To make your dynamically generated html easier to read, I recommend a basic template string with placeholders -- this way you do need to use interpolation or concatenation to mix static text with variables.

I don't know what your intention is with the radio inputs, but id attributes must be unique in a valid html document. The name attributes between different sets of stars in the same form will also be in conflict -- again, I don't know your intent.

Use css to implement styling. Use classes to style repeating elements.

Only use html tables to present tabular data. It is not recommended/modern to abuse table as a way to "grid" general html layout.

Recommended Starting Code: (Demo)

$rowTemplate = <<<ROWTEMPLATE
    <tr class="border_bottom">
        <td class="event">%s</td>
        <td class="name">%s</td>
        <td class="review">%s</td>
        <td class="rating">%s</td>
        <td><img class="img-thumbnail" src="%s"/></td>
    </tr>

ROWTEMPLATE;

?>
<table class="table">
    <tr>
        <th>Event</th>
        <th>Reviewer</th>
        <th>Review</th>
        <th>Rating</th>
        <th>Image?</th>
    </tr>
<?php
    foreach (json_decode($json) as $obj) {
        printf(
            $rowTemplate,
            $obj->event,
            $obj->name,
            $obj->review,
            $obj->rating,
            $obj->image,
        );    
    }
?>
</table>

From this, you can replace $obj->rating in printf() with whatever repeating graphical markup that you wish.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...