vivis933 Posted August 31, 2016 Share Posted August 31, 2016 (edited) 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 csgoawayteam1scgohometeam2 vs csgoawayteam2scgohometeam3 vs csgoawayteam3scgohometeam4 vs csgoawayteam4scgohometeam5 vs csgoawayteam5scgohometeam6 vs csgoawayteam6 ...etc. Thes desired final echo after combinin the two codesFinal code results if echo $team1," vs ",$team2," = ", $result1 ; scgohometeam1 vs csgoawayteam1 = 25scgohometeam2 vs csgoawayteam2 = 45scgohometeam3 vs csgoawayteam3 = 40scgohometeam4 vs csgoawayteam4 = 30scgohometeam5 vs csgoawayteam5 = 15scgohometeam6 vs csgoawayteam6 = 23...etc. Edited August 31, 2016 by vivis933 Quote Link to comment https://forums.phpfreaks.com/topic/302051-combine-two-codes-in-one/ Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302051-combine-two-codes-in-one/#findComment-1536897 Share on other sites More sharing options...
mac_gyver Posted August 31, 2016 Share Posted August 31, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302051-combine-two-codes-in-one/#findComment-1536904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.