我有4个php文件,它们几乎都是相同的,只是表名不同,数组中的值也不同,我想将所有这些值放在另一个php文件中的一个数组中。
此文件为Germany.php
foreach($array as $key => $val) {
$Home=0;
$Draw=0;
$Away=0;
$sql = "SELECT * FROM Germany where B = '{$val['B']}' AND E = '{$val['E']}' AND F = '{$val['F']}' AND O ='{$val['O']}' AND A = '*' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rowcount=mysqli_num_rows($result);
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['R'] == 1){
$Home++;
}elseif($row['R'] == 0){
$Draw++;
}else{
$Away++;
}
}
//We use an array rather than overriding everytime
$AwayPrediction[$key] = round(($Away/$rowcount )*100);
} else {
echo "0 results";
}
}
$conn->close();
我认为我必须在index.php中做类似的事情
<table class="table">
<thead>
<tr>
<th>Home</th>
<th>Away</th>
<th>Result</th>
</tr>
</thead>
<?php
$array_of_teams=array();
include 'germany.php';
foreach($array as $key => $data) {
$array_of_teams+=merge($AwayPrediction[$key]);
echo "<tr>";
echo "<td>".$data['H']."</td>";
echo "<td>".$data['M']."</td>";
echo "<td>".$AwayPrediction[$key]."</td>";
}
include 'france.php';
foreach($array as $key => $data) {
$array_of_teams+=merge($AwayPrediction[$key]);
echo "<tr>";
echo "<td>".$data['H']."</td>";
echo "<td>".$data['M']."</td>";
echo "<td>".$AwayPrediction[$key]."</td>";
}
如何合并$ AwayPrediction [$ key]或将其添加到$ array_of_teams = array();对于每个文件?
答案 0 :(得分:1)
您的PHP“显示”部分
foreach($array as $key => $data) {
echo "<tr>";
echo "<td>".$data['H']."</td>";
echo "<td>".$data['M']."</td>";
echo "<td>".$AwayPrediction[$key]."</td>";
}
仅显示include 'england.php';
中的最后一个数组
因此,在每个数组之后放置一个显示部分,例如
include 'germany.php';
foreach($array as $key => $data) {
echo "<tr>";
echo "<td>".$data['H']."</td>";
echo "<td>".$data['M']."</td>";
echo "<td>".$AwayPrediction[$key]."</td>";
}
include 'france.php';
foreach($array as $key => $data) {
echo "<tr>";
echo "<td>".$data['H']."</td>";
echo "<td>".$data['M']."</td>";
echo "<td>".$AwayPrediction[$key]."</td>";
}
答案 1 :(得分:0)
首先,您应该遵循DRY principle并尝试最小化代码库中的代码重复。就您而言,您可以为重复的功能创建一个功能:
function predictionForCountry($countryName, $array, $conn)
{
foreach ($array as $key => $val) {
$Home = 0;
$Draw = 0;
$Away = 0;
$sql = "SELECT * FROM {$countryName} where B = '{$val['B']}' AND E = '{$val['E']}' AND F = '{$val['F']}' AND O ='{$val['O']}' AND A = '*' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rowcount = mysqli_num_rows($result);
// output data of each row
while ($row = $result->fetch_assoc()) {
if ($row['R'] == 1) {
$Home++;
} elseif ($row['R'] == 0) {
$Draw++;
} else {
$Away++;
}
}
//We use an array rather than overriding everytime
$AwayPrediction[$key] = round(($Away / $rowcount) * 100);
} else {
echo "0 results";
}
}
return $array;
}
另一种可能的解决方案,如果表的列数相同,则可以使用UNION ALL运算符在SQL中合并所有结果,并根据需要调整WHERE条件(如果每个国家/地区的条件不同)>
$sql = "SELECT * FROM Germany where B = '{$val['B']}' AND E = '{$val['E']}' AND F = '{$val['F']}' AND O ='{$val['O']}' AND A = '*'
UNION ALL
SELECT * FROM France where B = '{$val['B']}' AND E = '{$val['E']}' AND F = '{$val['F']}' AND O ='{$val['O']}' AND A = '*'
UNION ALL
SELECT * FROM Holland where B = '{$val['B']}' AND E = '{$val['E']}' AND F = '{$val['F']}' AND O ='{$val['O']}' AND A = '*'
UNION ALL
SELECT * FROM England where B = '{$val['B']}' AND E = '{$val['E']}' AND F = '{$val['F']}' AND O ='{$val['O']}' AND A = '*' ";
$result = $conn->query($sql);
答案 2 :(得分:0)
$array
并没有在那里更改,并且对于所有国家来说似乎都是相同的,$key
和$val
或$data
也是如此。
每个收录都将覆盖之前的$AwayPrediction[$key]
,并且您不会在国家/地区之间看到任何差异。
$AwayPrediction[$key]
与其前一个值相加,与行数相同,然后在显示前计算百分比。在国家/地区文件中:
$rowcount += mysqli_num_rows($result);
$AwayPrediction[$key] += $Away;
在index.php中
echo "<td>".round(($AwayPrediction[$key]/$rowcount )*100)."</td>";
$AwayPrediction[$key]['Germany']
添加1个维度然后您可以执行以下操作:
foreach ($array as $key => $data) {
foreach ($AwayPrediction[$key] as $country => $prediction) {
echo "<tr>";
echo "<td>".$data['H']."</td>";
echo "<td>".$data['M']."</td>";
echo "<td>".$country."</td><td>".$prediction."<td>\n";
}
}