0

I've been struggling with the following code. What I'm trying to do is count the number of reviews based on the score. The information is being drawn from MYSQL and a calculation is being preformed before entering it to an array there will be a maximum of five results (after formatting). To be counted.

The code I have is as follows:

   $myArray = str_split(554);
    $newArray = array_count_values($myArray);

    foreach ($newArray as $key => $value) {

     $reviews_percentage = round($value/3*100);

    if (array_key_exists("1",$newArray)) {


    echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />";   
    }

    else {

    echo "1 - <strong>0</strong> as a percent its : 0 <br />"; 
    }

    if (array_key_exists("2",$newArray)) {

    echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 
    }

    else {

    echo "2 - <strong>0</strong> as a percent its : 0 <br />"; 
    }

    if (array_key_exists("3",$newArray)) {

    echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 
    }

    else {

    echo "3 - <strong>0</strong> as a percent its : 0 <br />"; 
    }
    if (array_key_exists("4",$newArray)) {

    echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 

    }

    else {

    echo "4 - <strong>0</strong> as a percent its : 0 <br />"; 
    }

    if (array_key_exists("5",$newArray)) {

    echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 
    }

    else {

    echo "5 - <strong>0</strong> as a percent its : 0 <br />"; 
    }


    }

Which is giving the following result:

   1 - 0 as a percent its : 0 
   2 - 0 as a percent its : 0 
   3 - 0 as a percent its : 0 
   5 - 1 as a percent its : 50 
   5 - 1 as a percent its : 50 
   1 - 0 as a percent its : 0 
   2 - 0 as a percent its : 0 
   3 - 0 as a percent its : 0 
   4 - 1 as a percent its : 50 
   4 - 1 as a percent its : 50 

I can see its running through the loop twice but cannot work out what I'm doing wrong.

Added Database Structure

    |------
    |id|date_created|date_updated|ip_address|status|element_3|element_4|element_5|element_6|element_7|element_8|element_9|
    |------
    |1|2012-06-21 15:22:57|2012-06-21 16:06:04|::1|1|19|10|10|10|10|10|10|
    |2|2012-06-21 16:21:23|2012-06-21 16:21:40|::1|1|19|10|9|9|9|10|
    |3|2012-06-21 18:14:56|2012-06-21 18:15:19|::1|1|18| 5|5|5|5|5|

UPDATED CODE

    $result = mysql_query("SELECT * FROM ap_form_5 WHERE element_1='19'") or die(mysql_error());

    $num_rows = mysql_num_rows($result);

        while($profile_rows = mysql_fetch_array($result)) {

    $feature1 = $profile_rows['element_4'];
    $feature2 = $profile_rows['element_5'];
    $feature3 = $profile_rows['element_6'];
    $feature4 = $profile_rows['element_7'];
    $feature5 = $profile_rows['element_8'];        
$overalladd  = $feature1+$feature2+$feature3+$feature4+$feature5;

    $ratingsbar .= floor(round($overalladd/5/2, 15, PHP_ROUND_HALF_DOWN));

    $myArray = str_split($ratingsbar);
    $arrayCount = array_count_values($myArray);
    }

    function perc($total,$count){
    $ans = (100/$total) * $count;
    return($ans);


    // this array is only being filled like this to to show my working out (your db will populate this)
    $ratings[1]= $arrayCount[0]; // 1 star ratings - 2 votes
    $ratings[2]= $arrayCount[1]; // 2 star rating - 1 votes
    $ratings[3]= $arrayCount[2]; // 3 star rating - 2 votes
    $ratings[4]= $arrayCount[3]; // 4 star rating - 0 votes
    $ratings[5]= $arrayCount[4]; // 5 star rating - 5 votes

    $total_votes = array_sum($ratings);

    $i = 1;
    foreach($ratings as $rating){
    echo "Stars ".$i.": ".perc($total_votes,$rating)."% $ratings[$i]<br />";
    $i ++;
    }

    ?>

Which is now giving the following result

Stars 1: 0% 
Stars 2: 0% 
Stars 3: 50% 1
Stars 4: 0% 
Stars 5: 50% 1
4
  • make an array of the info for us to work with... it's not too easy to see what your issue is Commented Nov 7, 2012 at 12:33
  • Sorry, updated the code to make it easier to work with. Thanks Commented Nov 7, 2012 at 12:55
  • Ok - I still don't understand what you are trying to achieve. I think you may be over engineering this. So you are trying to take a score of say 400 and divide it by an increment to work out how many people voted, for example score of 400 with a score increase of 10 gives 40 votes. And you are trying to work out that it is 40? Commented Nov 7, 2012 at 13:08
  • Basically I'm tryimg to take the total amount of reviews then grouping them by their score. For example there may be 10 reviews 5 may be 5 star, 2 may be 4 star and so on. I want to count how many there are in each rating then display them as a bar chart as a percentage. So if there are 10 reviews in total five of them are 5 star overall. This would show 50% of the reviews are 5 star, 20% may be 4 star and so on all the way down to 0. Kinda in a similar way to amazon so with their product reviews. Commented Nov 7, 2012 at 13:40

3 Answers 3

1

Give this a try?

<?php

    $result = mysql_query("SELECT * FROM ap_form_5 WHERE element_1='19'") or die(mysql_error());
    $num_rows = mysql_num_rows($result);

    while($profile_rows = mysql_fetch_array($result)) {

        $total_ratings_this_loop = 0; // empty this before looping
        $ratings[1]= $profile_rows['element_4']; // assuming that this field contains 1 star ratings for this product
        $ratings[2]= $profile_rows['element_5']; // assuming that this field contains 2 star ratings for this product
        $ratings[3]= $profile_rows['element_6']; // assuming that this field contains 3 star ratings for this product
        $ratings[4]= $profile_rows['element_7']; // assuming that this field contains 4 star ratings for this product
        $ratings[5]= $profile_rows['element_8']; // assuming that this field contains 5 star ratings for this product

        $total_ratings_this_loop = array_sum($ratings); // takes all of the ratings for this product and totals them from inside the array

        $overalladd  = $feature1 + $feature2 + $feature3 + $feature4 + $feature5;

        echo "Product ID: 19, has the following ratings<br />";

        $i = 1; // empty this before looping
        foreach($ratings as $rating_count){
            echo $i." star, Rating count: ".$rating.",Percentage:".perc($total_ratings_this_loop,$rating)."%<br />";
            $i ++;
        }   

    }

 ?>
Sign up to request clarification or add additional context in comments.

8 Comments

Its almost what I'm aiming to get, however I wanted to count how many reviews for each star rating as well as display this as a percentage of the overall reviews. I did try to modify your code to do this however I think I'm getting myself more confused, the more I try to figure this out.
The total number of reviews is $total_votes, the total number of 1 star votes are here 'echo $ratings[1]; number of two star 'echo $ratings[2]'does that make sense
Thanks for all your help. I think I understand what you are saying but I'm not getting my desired result. Do you know how I can match the results from mySQL to the relevant star rating? At the moment its coming out in the order of the search result. So doesn't accurately match the star rating. The values I'm getting from my code are 2, 5 and 4. However I can't think of a way to count these properly and match it to the relevant $ratings row. I've updated my code above to hopefully get an idea of what I've been doing and trying to achieve.
What do these do... $feature5 = $profile_rows['element_8']; $overalladd = $feature1+$feature2+$feature3+$feature4+$feature5; $ratingsbar .= floor(round($overalladd/5/2, 15, PHP_ROUND_HALF_DOWN));
What do each of the columns in your database mean? |id|date_created|date_updated|ip_address|status|element_3|element_4|element_5|element_6|element_7|element_8|element_9|, are they ratings/ votes etc
|
0

Replace your code with below code and check :

$myArray = str_split($ratingsbar);
$newArray = array_count_values($myArray);

foreach ($myArray as $key => $value) {

$reviews_percentage = round($value/$num_rows*100);

if (array_key_exists("1",$myArray)) {


echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />";   
}

else {

echo "1 - <strong>0</strong> as a percent its : 0 <br />"; 
}

if (array_key_exists("2",$myArray)) {

echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 
}

else {

echo "2 - <strong>0</strong> as a percent its : 0 <br />"; 
}

if (array_key_exists("3",$myArray)) {

echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 
}

else {

echo "3 - <strong>0</strong> as a percent its : 0 <br />"; 
}
if (array_key_exists("4",$myArray)) {

echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 

}

else {

echo "4 - <strong>0</strong> as a percent its : 0 <br />"; 
}

if (array_key_exists("5",$myArray)) {

echo "$key - <strong>$value</strong> as a percent its : $reviews_percentage <br />"; 
}

else {

echo "5 - <strong>0</strong> as a percent its : 0 <br />"; 
}


}

1 Comment

still getting a similar issue. Just percentage figures have changed.
0

What I'm trying to do is count the number of reviews based on the score.

Please show us the database structure filled with minimized example data, and tell us what is your expected result according to that data.


OK, if you would like to create something like this:

enter image description here

then I demostrate, how I would do it, because I still does not understand your logic.

votes table (this means user U voted on product P with S stars):
user_id |  product_id    | star
      1           1         5   
      2           1         5
      3           1         4
      4           1         1

sql query for total votes on product 1 is

SELECT star,COUNT(*) as C 
FROM votes 
WHERE product_id=1 
GROUP BY star 

the result of this is

star    C
1       1
4       1
5       2

to write it out:

$rs = mysql_query("");//put query here
$stars_count = array();
$star_sum = 0;
while($r = mysql_fetch_assoc($rs))
{
  $stars_count[$r['star']] = $r['C'];
  $star_sum += $r['C'];
}
for($i=5;$i>=1;$i--)
  echo "Product 1 was voted $i star: ".
     (isset($stars_count[$i])? round($stars_count[$i]/$star_sum*100)."%":"0%").
     " (".( isset($stars_count[$i])? $stars_count[$i] : 0 ).")<br>";

result:

Product 1 was voted 5 star: 50% (2)
Product 1 was voted 4 star: 25% (1)
Product 1 was voted 3 star: 0% (0)
Product 1 was voted 2 star: 0% (0)
Product 1 was voted 1 star: 25% (1)

2 Comments

Added database structure. I run a mysql query to get element_4 through to element_9 this brings the total score which i divide by 6 then that total by 2 to get the overall score then round down to the nearest whole number. So row 1 should come out as 5. As each product has multiple reviews I wanted to work out how many reviews there were for each star rating then show the number of reviews for that star plus its percentage of the overall reviews for that product.
This is spot on what I'm trying to achieve. However the database structure I'm using is doesn't store an overall score for the product so I'm having to add the 6 elements (element_4 thru to element_9 in the table) to create an overall score which I round down to the nearest whole number to give it the star rating. This is the part I'm really struggling to grasp. Showing all 5 star ratings (whether they have that star rating or not).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.