1

I am very new in php and I really try a few days to find a solution, I feel like I am almost there but still no success. I f someone can hep me I will be very happy. I am building a new website for prediction of football. I have a table in mysql and I can get successfully all rows which I need. I thing also I am storing the data also in a good way in an array.

this is the first php where I get the rows and stored them in an array, I also need columns data because I need them in the second php for the new query.

file name 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();
?>  

second php file 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();
    ?>

And the final 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>

If I run the code like this, I am only getting the information from the last index in an array. What I need is all array data showing on the site. I hoop it was clear to understand my question. I hoop to get feedback thank's a lot.

1 Answer 1

1

You are overwriting your variables on each loop iteration. You should fill a new array, and use this array to output your data.

Please read the comments I made in your code, I can not guarantee you, that there are no errors because I can't test it, but that should give you the idea of what you need, try fixing errors yourself first and if you can't do it comment on here for help.

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();
?>  

Firsthalfprediction.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();
?>

index.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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Marvin Fischer. It works perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.