Combine two codes in one

First code

<?php include("conf.php"); $fixtures_data = mysqli_query($conn,"SELECT Location,Date,HomeTeam,AwayTeam FROM datagmaes"); if ($fixtures_data->num_rows > 0) { // output data of each row while($row = $fixtures_data->fetch_assoc()) { $team1 = $row["HomeTeam"]; $team2 = $row["AwayTeam"]; } } else { echo "0 results"; } ?>
**Second code**

<?php include("conf.php"); include("poi.php"); $home = mysqli_query($conn,"SELECT Teamdata1 FROM gamesdata WHERE Team='$team1'"); $row1 = $home->fetch_assoc(); $hometeam = number_format($row1['Teamdata1'],2); $away = mysqli_query($conn,"SELECT Teamdata2 FROM gamesdata WHERE Team='$team2'"); $row2 = $away->fetch_assoc(); $awayteam = number_format($row2['Teamdata2'],2); $calAll = array(); /////All calculations for($goals = 0 ; $goals <= 9 ; $goals++) { array_push($calAll, (poi($goals,$hometeam) * poi(0,$awayteam))*100, (poi($goals,$hometeam) * poi(1,$awayteam))*100, (poi($goals,$hometeam) * poi(2,$awayteam))*100, (poi($goals,$hometeam) * poi(3,$awayteam))*100, (poi($goals,$hometeam) * poi(4,$awayteam))*100, (poi($goals,$hometeam) * poi(5,$awayteam))*100, (poi($goals,$hometeam) * poi(6,$awayteam))*100, (poi($goals,$hometeam) * poi(7,$awayteam))*100, (poi($goals,$hometeam) * poi(8,$awayteam))*100, (poi($goals,$hometeam) * poi(9,$awayteam))*100 ); } $result1 = number_format(array_sum($calAll),2); echo $result1 . "\n"; ?>
The first code return multiple items i need to apply the second code to every item in first code and then to save   ```$result1```  in mysql table.
First code results if ```echo $team1," vs ",$team2;```

scgohometeam1 vs csgoawayteam1
scgohometeam2 vs csgoawayteam2
scgohometeam3 vs csgoawayteam3
scgohometeam4 vs csgoawayteam4
scgohometeam5 vs csgoawayteam5
scgohometeam6 vs csgoawayteam6 
...
etc.

Thes desired final echo after combinin the two codes
Final code results if ```echo $team1," vs ",$team2," = ", $result1  ;```

scgohometeam1 vs csgoawayteam1 = 25
scgohometeam2 vs csgoawayteam2 = 45
scgohometeam3 vs csgoawayteam3 = 40
scgohometeam4 vs csgoawayteam4 = 30
scgohometeam5 vs csgoawayteam5 = 15
scgohometeam6 vs csgoawayteam6  = 23
...
etc.

Can you not just insert your second piece of code (with whatever changes you need to make sure of no clashing variable names and so on) in here:

if ($fixtures_data->num_rows > 0) {
    // output data of each row
    while($row = $fixtures_data->fetch_assoc()) {
        $team1 = $row["HomeTeam"];
        $team2 = $row["AwayTeam"];
// ** INSERT THE SECOND BIT OF CODE HERE ** //
        }

Although Iā€™m sure you could link the queries with an INNER JOIN so that a single query will get all the data you need in one hit.

change the include to require_once and then you can combine them together without duplicating code.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.