我从我的数据库中提取了一些数据并输出了以下内容
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:1 Placed:1
20f -Won:1 Placed:1
20f -Won:0 Placed:0
21.5f -Won:1 Placed:1
21.5f -Won:0 Placed:0
21.5f -Won:0 Placed:1
21.5f -Won:0 Placed:1
21.5f -Won:0 Placed:1
21f -Won:0 Placed:1
21f -Won:0 Placed:0
22f -Won:0 Placed:0
22f -Won:0 Placed:0
22f -Won:0 Placed:0
23f -Won:0 Placed:0
23f -Won:0 Placed:0
23f -Won:0 Placed:0
24f -Won:0 Placed:0
24f -Won:0 Placed:1
这是通过使用以下结果来完成的
$data .= .$Horsedist. " -Won:".$wintotals. " Placed:".$placetotals." <br />";
我想要做的是将每个距离分组并添加赢得和放置的数字,以便输出以下内容(Raced是数字的总和)
20f -Won:2 Placed:2 Raced:9
21.5f -Won:1 Placed:4 Raced:5
21f -Won:0 Placed:1 Raced:2
22f -Won:0 Placed:0 Raced:3
23f -Won:0 Placed:0 Raced:3
24f -Won:0 Placed:1 Raced:2
我正在努力想办法如何做到这一点。下面是代码的精简版
<?php //pulling the data from database for the horse
$sqlhorses = "SELECT distance,Place,Runners FROM `horsesrp` WHERE `Horse` = '".$horse."' order by distance";
$horseplaced = mysqli_query($db, $sqlhorses);
$data = "";
while($pasthorse = mysqli_fetch_array($horseplaced)){
// setting wins and places to 0
$placetotals = 0;
$wintotals = 0;
//pull where the horse places
$placeddata = intval($pasthorse['Place']);
//if it finished first add one for win and one for placed
if ($placeddata == 1)
{
$placetotals = 1;
$wintotals = 1;
}
else
{
// if the were between 4 and 7 runners only award a place if it came 2nd
if ($pasthorse['Runners'] > 4 and $pasthorse['Runners'] < 8 )
{
if ($pasthorse['Place'] == 2)
{
$placetotals = 1;
}
}
// if there are between 8 and 15 runners award a place for 2nd and 3rd
if ($pasthorse['Runners'] > 7 and $pasthorse['Runners']< 16 )
{
if ($pasthorse['Place'] == 2 or $pasthorse['Place'] == 3 )
{
$placetotals = 1;
}
}
//above 15 runners and award place for 2nd 3rd and 4th
if ($pasthorse['Runners'] > 15 )
{
if ($pasthorse['Place'] == 2 or $pasthorse['Place'] == 3 or $pasthorse['Place'] == 4 )
{
$placetotals = 1;
}
}
}
// add it to a row to show the distance it ran and if it won or placed
$data .= "<span style='font-size:10.5px'>".$Horsedist. " -Won:".$wintotals. " Placed:".$placetotals." </span><br />";
}
}
//now display it
echo "<tr><td >";
echo "<td >".$data."</td>";
echo "</tr>";
?>
答案 0 :(得分:1)
哦,好吧,这不是很困难,而且你完成了大部分工作。
您可以根据自己的意愿使其工作的第一种方法是将这些数据存储在一个临时数组中,并汇总属于同一主题的结果,例如
$storage = array();
//Do what you want to do
// while(....) {
//and now, each iteration :
//If there isn't any entry for the current subject
if (!isset($storage[$Horsedist])) {
$storage[$Horsedist] = array("win" => 0, "placed" => 0, "raced" => 0);
}
//Then increment if required
$storage[$Horsedist]["win"] += $wintotals;
$storage[$Horsedist]["placed"] += $placetotals;
$storage[$Horsedist]["raced"] += 1;
// }
//At the end of your loop, just display everything
$data = "";
foreach ($storage as $pony => $tab) {
$data .= $pony. " -Won:".$tab["win"]. " Placed:".$tab["placed"]." Raced:".$tab["raced"]."<br />";
}
//Do whatever you want with $data
echo $data;
为了能够获取大量数据的详细信息,您必须在每次迭代时跟踪它们(因为您不能在最后简单地跟踪它,它就像想要知道谁拥有吃了多少甜食,却不知道是谁掏了袋子,他们多久去吃一次。在上面的代码中,我确实假设您使用循环来计算所有信息(获胜,放置),我刚刚添加了上述行为,对于每次迭代,检查当前$ Horsedist是否已经注册了关于自身的信息。如果它没有,那么他宣布自己的&#34;工作空间&#34;。然后,它只是将您需要的内容添加到全局工作区。在最后,foreach循环旨在显示每个&#34;聚合&#34;细节(循环外)
我希望这有帮助,随时可以提出任何问题