PHP foreach if / else问题

时间:2012-11-07 11:56:56

标签: php arrays foreach

我一直在努力使用以下代码。我要做的是根据分数计算评论数量。信息是从MYSQL中提取的,并且在将数据输入数组之前正在进行计算,最多会有五个结果(格式化后)。算一算

我的代码如下:

   $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 />"; 
    }


    }

这给出了以下结果:

   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 

我可以看到它在循环中运行两次,但无法弄清楚我做错了什么。

添加了数据库结构

    |------
    |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|

更新代码

    $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 ++;
    }

    ?>

现在提供以下结果

Stars 1: 0% 
Stars 2: 0% 
Stars 3: 50% 1
Stars 4: 0% 
Stars 5: 50% 1

3 个答案:

答案 0 :(得分:1)

试一试?

<?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 ++;
        }   

    }

 ?>

答案 1 :(得分:0)

用以下代码替换您的代码并检查:

$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 />"; 
}


}

答案 2 :(得分:0)

  

我要做的是根据数量来计算评论数量   分数。

请向我们展示填充最小化示例数据的数据库结构,并根据该数据告诉我们您的预期结果。


好的,如果你想创建这样的东西:

enter image description here

然后我说,我将如何做,因为我仍然不理解你的逻辑。

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

对产品1的总投票的SQL查询是

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

结果是

star    C
1       1
4       1
5       2

把它写出来:

$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>";

结果:

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)