0

In the site that I designed with WordPress, I have to create the number of visitors and their rating as stars in the products section. I used these javascript codes, but not work.Thank you in advance for your cooperation

enter image description here

var starRating = [...document.querySelectorAll('.starRating')].map(o => o.innerText).join('\n');

for (let x = 0; x <  starRating.length; x++){
  function Getrating() {
    $starNumber = "5";
    for($x=1;$x<=$starNumber;$x++) {
      $output .= '<i class="fa fa-star" aria-hidden="true"></i>';
    }
    if (strpos($starNumber,'.')) {
       $output .= '<i class="fa fa-star-half-o" aria-hidden="true">       </i>';
        $x++;
    }
    while ($x<=5) {
      $output .= '<i class="fa fa-star-o" aria-hidden="true"></i>';
      $x++;
    }
    return $output;

 }

   add_shortcode('starrating', 'Getrating');
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

//wordpress code
<div class="starRating">
  <?php the_field('rating'); ?> /
  <?php the_field('review_count'); ?>
</div>

//html code in inspector
<div class="starRating"> 3.2 /345 </div>

<div class="starRating"> 4 /4567 </div>

<div class="starRating"> 4.7 /987 </div>

<div class="starRating"> 2.7 /1210 </div>
  • 1
    The JavaScript has syntax errors. It seems you're using PHP style string concatenation. Use `+=` instead of `.=`? – evolutionxbox May 27 '22 at 07:14
  • Looks like you're mixing javascript with PHP. Javascript is executed client-side (in the browser), PHP server-side. So the function `function Getrating()` will never execute in javascript, as it is PHP code. – Michel May 27 '22 at 07:22
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Michel May 27 '22 at 07:23

0 Answers0