我正在制作自己的仪表板,以显示在多个国家/地区的App Store上对移动应用程序的评价情况。我已经显示了每个国家有多少颗5/4/3/2/1颗星,但是我想显示最后一个已知日期与前一天之间的差异(分数增加了'+ 16★★★★★ ★'或减少'-2★,例如美国)。
由于我对PHP / SQL有点陌生,有人可以给我一些提示/技巧来实现这一目标吗?预先感谢!
我的数据库具有以下设置:
表'Ratings':
[ID]--[oneStar]--[twoStar]--[threeStar]--[fourStar]--[fiveStar]--[country]--[crawldate]
1160-- 37 -- 23 -- 50 -- 260 -- 516 -- US -- 2020-03-02 00:05:05
1159-- 5 -- 0 -- 0 -- 9 -- 35 -- GB -- 2020-03-02 00:05:05
1158-- 8 -- 3 -- 19 -- 60 -- 130 -- BE -- 2020-03-02 00:05:05
1157-- 5 -- 0 -- 0 -- 9 -- 30 -- NL -- 2020-03-02 00:05:05
1156-- 39 -- 23 -- 50 -- 253 -- 501 -- US -- 2020-03-01 00:05:05
1155-- 5 -- 0 -- 0 -- 9 -- 33 -- GB -- 2020-03-02 00:05:05
1154-- 8 -- 3 -- 19 -- 59 -- 120 -- BE -- 2020-03-02 00:05:05
1153-- 5 -- 0 -- 0 -- 9 -- 28 -- NL -- 2020-03-02 00:05:05
预期结果:
[ID]--[oneStar]--[twoStar]--[threeStar]--[fourStar]--[fiveStar]--[country]--[crawldate]
1160-- 37 -- 23 -- 50 -- 260 -- 516 -- US -- 2020-03-02 00:05:05
1156-- 39 -- 23 -- 50 -- 253 -- 501 -- US -- 2020-03-01 00:05:05
-- -2 -- 0 -- 0 -- +7 -- +15 -- US -- 2020-03-01 00:05:05
[+15] 516 x ★ ★ ★ ★ ★
[+7] 260 x ★ ★ ★ ★
50 x ★ ★ ★
23 x ★ ★
[-2] 39 x ★
这是我rating.php文件的一部分:
<?php
$sql = "SELECT * FROM Ratings ORDER by `crawlDate` DESC LIMIT 4";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h6 class='date'> <span data-store='AppStore' data-field='updateDate'>";
$input = $row["crawlDate"];
$date = strtotime($input);
echo date('d M Y', $date);
echo "</span></h6>
</div>
<h3>App Store - ".$row["country"]."</h3></div>";
echo "
<center>
<h1 class='rating rating-arrow'> <span data-store='AppStore' data-field='average'>";
// CALCULATE AVERAGE PER COUNTRY
$fiveStars = $row["fiveStar"];
$multiplyByFive = 5;
$five_total = $fiveStars * $multiplyByFive;
$fourStars = $row["fourStar"];
$multiplyByFour = 4;
$four_total = $fourStars * $multiplyByFour;
$threeStars = $row["threeStar"];
$multiplyByThree = 3;
$three_total = $threeStars * $multiplyByThree;
$twoStars = $row["twoStar"];
$multiplyByTwo = 2;
$two_total = $twoStars * $multiplyByTwo;
$oneStars = $row["oneStar"];
$multiplyByOne = 1;
$one_total = $oneStars * $multiplyByOne;
$totalPoints = $one_total + $two_total + $three_total + $four_total + $five_total;
$totalRatings = $oneStars + $twoStars + $threeStars + $fourStars + $fiveStars;
$average = $totalPoints / $totalRatings;
echo substr($average, 0, 4);
echo "</span> </h1>
<p> Average store rating</p>
</center>
<table class='table ratings'>
<tbody>
<tr>
<td class='text-right rating rating-number' width='50%'><span class="positive">+16</span><span data-store='AppStore' data-field='fiveStar'>".$row["fiveStar"]."</span></th>
<td>★ ★ ★ ★ ★ </td>
</tr>
<tr>
<td class='text-right rating rating-number'><span data-store='AppStore' data-field='fourStar'>".$row["fourStar"]."</span></th>
<td>★ ★ ★ ★ <span class='inactive'>★ </span></td>
</tr>
<tr>
<td class='text-right rating rating-number'><span data-store='AppStore' data-field='threeStar'>".$row["threeStar"]."</span></th>
<td>★ ★ ★ <span class='inactive'>★ ★ </span></td>
</tr>
<tr>
<td class='text-right rating rating-number'><span data-store='AppStore' data-field='twoStar'>".$row["twoStar"]."</span></th>
<td>★ ★ <span class='inactive'>★ ★ ★ </span></td>
</tr>
<tr>
<td class='text-right rating rating-number'><span class="positive">- 1</span><span data-store='AppStore' data-field='oneStar'>".$row["oneStar"]."</span></th>
<td>★ <span class='inactive'>★ ★ ★ ★ </span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
";
}
} else {
echo "No ratings available";
}
?>
确定,进行了更多研究,并提出了以下解决方案:
foreach (array("NL", "BE", "US", "CA", "GB") as &$RatingCountry) {
$sql = "SELECT * FROM `Ratings` WHERE `country` = '$RatingCountry' ORDER BY `crawlDate` DESC LIMIT 2";$
result = $mysqli->query($sql);
for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row);
循环通过国家/地区评分并获得两个数组(最后一个和上一个)。
$fiveStarLast = $set[0]["fiveStar"];
$fiveStarPrev = $set[1]["fiveStar"];
$fiveLastTotal = $fiveStarLast * 5;
$fivePrevTotal = $fiveStarPrev * 5;
$fourStarLast = $set[0]["fourStar"];
$fourStarPrev = $set[1]["fourStar"];
$fourLastTotal = $fourStarLast * 4;
$fourPrevTotal = $fourStarPrev * 4;
$threerStarLast = $set[0]["threeStar"];
$threeStarPrev = $set[1]["threeStar"];
$threeLastTotal = $threerStarLast * 3;
$threePrevTotal = $threerStarPrev * 3;
$twoStarLast = $set[0]["twoStar"];
$twoStarPrev = $set[1]["twoStar"];
$twoLastTotal = $twoStarLast * 2;
$twoPrevTotal = $twoStarPrev * 2;
$oneStarLast = $set[0]["oneStar"];
$oneStarPrev = $set[1]["oneStar"];
$oneLastTotal = $oneStarLast * 1;
$onePrevTotal = $oneStarPrev * 1;
$totalLastPoints = $oneLastTotal + $twoLastTotal + $threeLastTotal + $fourLastTotal + $fiveLastTotal;
$totalLastRatings = $oneStarLast + $twoStarLast + $threerStarLast + $fourStarLast + $fiveStarLast;
$totalPrevPoints = $onePrevTotal + $twoPrevTotal + $threePrevTotal + $fourPrevTotal + $fivePrevTotal;
$totalPrevRatings = $oneStarPrev + $twoStarPrev + $threeStarPrev + $fourStarPrev + $fiveStarPrev;
$averageLast = $totalLastPoints / $totalLastRatings;
$averagePrev = $totalPrevPoints / $totalPrevRatings;
$averageDiff = $averagePrev - $averageLast;
$diffRating = $averageDiff;
echo substr($averageLast, 0, 4);
echo diffIndicator($diffRating);
计算5/4/3/2/1星的差异+得到国家的平均值(并与上一个进行比较)