如何通过查询遍历从mysql数据库表创建的数组,并在另一个php文件中使用该数组?
我对php非常陌生,我真的尝试了几天来找到一个解决方案,我觉得我几乎做到了,但仍然没有成功。如果有人能帮助我,我会非常高兴。我正在建立一个预测足球的新网站。我在mysql中有一个表,我可以成功地获得我需要的所有行。我也在用一种很好的方式将数据存储在一个数组中 这是我获取行并将它们存储在数组中的第一个php,我还需要列数据,因为在新查询的第二个php中需要它们 文件名filtere1_danimarka.php如何通过查询遍历从mysql数据库表创建的数组,并在另一个php文件中使用该数组?,php,mysql,arrays,Php,Mysql,Arrays,我对php非常陌生,我真的尝试了几天来找到一个解决方案,我觉得我几乎做到了,但仍然没有成功。如果有人能帮助我,我会非常高兴。我正在建立一个预测足球的新网站。我在mysql中有一个表,我可以成功地获得我需要的所有行。我也在用一种很好的方式将数据存储在一个数组中 这是我获取行并将它们存储在数组中的第一个php,我还需要列数据,因为在新查询的第二个php中需要它们 文件名filtere1_danimarka.php <?php include 'tableNameDanimarka.php';
<?php
include 'tableNameDanimarka.php';
include 'indexDanimarka.php';
include 'connectionDatabase.php';
$sql = "SELECT *
FROM $tableName
where NOT A ='*' ";
$result = $conn->query($sql);
$array = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$array[] = $row;
}
for($i = 0, $j = count($array); $i < $j ; $i++){
$B= $array[$i]['B'];
$F = $array[$i]['F'];
$E = $array[$i]['E'];
$O = $array[$i]['O'];
$Home = $array[$i]['HOME'];
$Away = $array[$i]['AWAY'];
}
} else {
echo "0 results";
}
$conn->close();
?>
第二个php文件
Firsthalfprediction.php
<?php
include 'filtre1_danimarka.php';
include 'tableNameDanimarka.php';
include 'connectionDatabase.php';
$FirstHalfHome=0;
$FirstHalfDraw=0;
$FirstHalfAway=0;
$sql = "SELECT * FROM $tableName where B = '$B' AND E = '$E' AND F = '$F' AND O ='$O' ";
$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['Q'] == 1){
$FirstHalfHome++;
}
elseif($row['Q'] == 0){
$FirstHalfDraw++;
}else{
$FirstHalfAway++;
}
}
$FirstHalfHomePrediction = round(($FirstHalfHome/$rowcount )*100);
$FirstHalfDrawPrediction = round(($FirstHalfDraw/$rowcount )*100);
$FirstHalfAwayPrediction = round(($FirstHalfAway/$rowcount )*100);
} else {
echo "0 results";
}
$conn->close();
?>
您正在每个循环迭代中覆盖变量。您应该填充一个新数组,并使用此数组输出数据
请阅读我在您的代码中的注释,我不能向您保证,没有错误,因为我无法测试它,但这应该会让您了解您需要什么,请先尝试自己修复错误,如果您不能这样做,请在此处评论以寻求帮助
filter1_danimarka.php
<?php
//filtre1_danimarka.php
include 'tableNameDanimarka.php';
include 'indexDanimarka.php';
include 'connectionDatabase.php';
$sql = "SELECT *
FROM $tableName
where NOT A ='*' ";
$result = $conn->query($sql);
$array = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$array[] = $row;
}
// We removed the complete for, and give the array to the next scripts
} else {
echo "0 results";
}
$conn->close();
?>
非常感谢马文·菲舍尔。它工作得很好。
<?php
//filtre1_danimarka.php
include 'tableNameDanimarka.php';
include 'indexDanimarka.php';
include 'connectionDatabase.php';
$sql = "SELECT *
FROM $tableName
where NOT A ='*' ";
$result = $conn->query($sql);
$array = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$array[] = $row;
}
// We removed the complete for, and give the array to the next scripts
} else {
echo "0 results";
}
$conn->close();
?>
<?php
include 'filtre1_danimarka.php';
include 'tableNameDanimarka.php';
include 'connectionDatabase.php';
// New foreach to go through the data
foreach($array as $key => $val) {
$FirstHalfHome=0;
$FirstHalfDraw=0;
$FirstHalfAway=0;
$sql = "SELECT * FROM $tableName where B = '{$val['B']}' AND E = '{$val['E']}' AND F = '{$val['F']}' AND O ='{$val['O']}' ";
$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['Q'] == 1){
$FirstHalfHome++;
}elseif($row['Q'] == 0){
$FirstHalfDraw++;
}else{
$FirstHalfAway++;
}
}
//We use an array rather than overriding everytime
$FirstHalfHomePrediction[$key] = round(($FirstHalfHome/$rowcount )*100);
$FirstHalfDrawPrediction[$key] = round(($FirstHalfDraw/$rowcount )*100);
$FirstHalfAwayPrediction[$key] = round(($FirstHalfAway/$rowcount )*100);
} else {
echo "0 results";
}
}
$conn->close();
?>
<table class="table">
<thead>
<tr>
<th>Home</th>
<th>Away</th>
<th>FirstHalf Prediction</th>
</tr>
</thead>
<?php
include 'filtre1_danimarka.php';
include 'Firsthalfprediction.php';
// Added loop to iterate all data
foreach($array as $key => $data) {
echo "<tr>";
echo "<td>".$data['HOME']."</td>";
echo "<td>".$data['AWAY']."</td>";
echo "<td>1=".$FirstHalfHomePrediction[$key]." 0=".$FirstHalfDrawPrediction[$key]."\r 2=".$FirstHalfAwayPrediction[$key]."</td>";
echo "</tr>";
}
?>
</table>