Jump to content

Recommended Posts

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.

Edited by vivis933
Link to comment
https://forums.phpfreaks.com/topic/302051-combine-two-codes-in-one/
Share on other sites

The simplest solution:

 

Combine your scripts into one script, and move the code for script 2 into a function:  

 

 

global $conn;
 
function TeamCalc($team1, $team2)  {
   ....
   return $result1;
}

 

Now inject this function into your main code loop, and store the results as desired in your database.

 

The only tricky part is you need to make sure you declare your $conn variable as global at the top of the script, after you've read in your includes.  This will allow that connection variable to be seen inside your function when you use it to make the database calls.  This is why I included it in my snippet.

i would run one JOIN'ed query, then calculate the $result1 value while looping over the rows from that one query. the following query should (untested) work - 
$query = "SELECT g.Location, g.Date, g.HomeTeam, g.AwayTeam, t1.Teamdata1, t2.Teamdata2
FROM datagmaes g
JOIN gamesdata t1 ON g.HomeTeam = t1.Team
JOIN gamesdata t2 ON g.AwayTeam = t2.Team";

you should NOT apply number_format() to the Teamdata1 and Teamdata2 values. number_format() is an output function and should only be applied when you display the number, not when calculating or storing data.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.