Product Rating Script Using PHP (xhydra.com)

2 points by blackvine ↗ HN
Creating a function to display a product ratings is trivial using PHP.

All you need is to decide on the values for you ratings and a the image to display for each rating.

For this Example we will use a system that stores ratings as descrete values and not continous values..............

2 comments

[ 88.3 ms ] story [ 59.6 ms ] thread
Too much code. Too many "if" statements that have to be executed every time.

Try:

  <?php
  function getRatingImage($rating)
  {
  $vals = ['zero','half','one','one_half','two','two_half','three','three_half','four','four_half','five'];
  $ndx = intval($rating * 2);
  if($ndx > 10){$ndx = 10};
  return "./" + $vals[$ndx] + ".jpg";
  }
  ?>
I totally agree that the use of all those ifs is bad. That said a switch statement will be faster than the code you posted(Though you were thinking in another language when your wrote your snippit methinks) if speed is your concern.