Reputation: 9
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
Upvotes: 0
Views: 328
Reputation: 5615
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 ++;
}
}
?>
Upvotes: 1
Reputation:
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:
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)
Upvotes: 0
Reputation: 1081
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 />";
}
}
Upvotes: 0