[英]How to go through an array which is created from mysql database table with a query and use the array in another php file?
我對 php 很陌生,我真的嘗試了幾天來找到解決方案,我覺得我快到了,但仍然沒有成功。 如果有人可以幫助我,我會很高興。 我正在建立一個用於預測足球的新網站。 我在 mysql 中有一個表,我可以成功獲取我需要的所有行。 我也想將數據以一種很好的方式存儲在數組中。
這是我獲取行並將它們存儲在數組中的第一個 php,我還需要列數據,因為我在新查詢的第二個 php 中需要它們。
文件名 filtre1_danimarka.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();
?>
和最終的 index.php
<table class="table">
<thead>
<tr>
<th>Home</th>
<th>Away</th>
<th>FirstHalf Prediction</th>
</tr>
</thead>
<tr>
<?php
include 'filtre1_danimarka.php';
include 'Firsthalfprediction.php';
echo"<td>".$Home."</td>";
echo"<td>".$Away."</td>";
echo"<td>1=".$FirstHalfHomePrediction." 0=".$FirstHalfDrawPrediction."\r 2=".$FirstHalfAwayPrediction."</td>";
echo "</tr>";
?>
</table>
如果我像這樣運行代碼,我只會從數組中的最后一個索引中獲取信息。 我需要的是網站上顯示的所有數組數據。 我很清楚理解我的問題。 我希望得到反饋,非常感謝。
您正在每次循環迭代中覆蓋變量。 您應該填充一個新數組,並使用該數組輸出您的數據。
請閱讀我在您的代碼中所做的評論,我不能保證沒有錯誤,因為我無法對其進行測試,但這應該可以讓您了解您需要什么,請先嘗試自己修復錯誤,如果可以的話不要在這里評論尋求幫助。
filtre1_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
<?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();
?>
索引.php
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.