Jump to content

Cheops

New Members
  • Posts

    7
  • Joined

  • Last visited

Cheops's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Its badly written but I managed to get it working so I didnt check on how to reduce the number of queries to one though I understood what you were implying and it actually looked simpler and less lines of code
  2. Where exactly do I place this code
  3. I would like to link my HTML form and PHP script such that when a user passes values via the form they are processed by the PHP script and a relevant response given. My PHP script is as follows; <?php //create server and database connection constants define ("DB_SERVER", "localhost"); define ("DB_USER","root"); define ("DB_PASSWORD",""); define ("DB_NAME", "project"); global $con; $con = new mysqli (DB_SERVER,DB_USER,DB_PASSWORD, DB_NAME); //Check server connection if ($con->connect_error){ die ("Connection failed:". $con->connect_error); }else { echo "Connected successfully <br />"; } // Calculating the attack stength function predictor ($home_team,$away_team){ global $con; $result=[0,0]; // For the home team $home_team_strength=get_query_value($con,"SELECT SUM(HTgoals)/Count(*) FROM `results` WHERE Home_team = '$home_team' "); $league_strength=get_query_value($con,"SELECT SUM(HTgoals)/Count(*) FROM `results`"); $home_team_attack_strength=$home_team_strength/$league_strength; $away_team_strength = get_query_value($con, "SELECT SUM(HTgoals)/Count(*) FROM `results` WHERE Away_team='$away_team'"); $away_team_defence_strength=$away_team_strength/$league_strength; $result[0]=$home_team_attack_strength*$away_team_defence_strength*$league_strength; // For the away team $away_team_strength_2 = get_query_value($con, "SELECT SUM(ATgoals)/Count(*) FROM `results` WHERE Away_team='$away_team'"); $league_away_strength=get_query_value($con, "SELECT SUM(ATgoals)/Count(*) FROM `results`"); $away_team_attack_strength =$away_team_strength_2/$league_away_strength; $home_team_defence =get_query_value($con, "SELECT SUM(ATgoals)/Count(*) FROM `results` WHERE Home_team='$home_team'"); $home_team_defence_strength =$home_team_defence/$league_away_strength; $result[1] = $away_team_attack_strength*$home_team_defence_strength*$league_away_strength; return $result; } function get_query_value ($con,$query){ $return_value=-1; $con->real_query($query); $result=$con->use_result(); $row=$result->fetch_row(); $result->close(); return $row[0]; } My HTML form is below; <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Prediction</title> <form> <form action="predictor.php" method="POST"> <font color ="blue"> <div align="center"> <p> Please enter the teams you would like to view predictions for</p> <p> Enter the home team</p> Home_team:<select name = "Home_team"> <br> <option value = "Bandari">Bandari</option> <option value = "Chemelil">Chemelil</option> <option value = "Gor Mahia">Gor Mahia</option> <option value = "Kakamega Homeboyz FC" >Kakamega Homeboyz FC</option> <option value = "Leopards">Leopards</option> <option value = "Mathare Utd">Mathare Utd</option> <option value = "Muhoroni">Muhoroni</option> <option value = "Nairobi City">Nairobi City</option> <option value = "Rangers">Rangers</option> <option value = "Sony Sugar">Sony Sugar</option> <option value = "Sofapaka">Sofapaka</option> <option value = "Thika Utd">Thika Utd</option> <option value = "Tusker">Tusker</option> <option value = "Ulinzi Stars">Ulinzi Stars</option> <option value = "Ushuru">Ushuru</option> <option value = "Western Stima">Western Stima</option> </select><br><br> <p> Enter the away team</p> Away_team:<select name = "Away_team"><br> <option value = "Bandari">Bandari</option> <option value = "Chemelil">Chemelil</option> <option value = "Gor Mahia">Gor Mahia</option> <option value = "Kakamega Homeboyz FC" >Kakamega Homeboyz FC</option> <option value = "Leopards">Leopards</option> <option value = "Mathare Utd">Mathare Utd</option> <option value = "Muhoroni">Muhoroni</option> <option value = "Nairobi City">Nairobi City</option> <option value = "Rangers">Rangers</option> <option value = "Sony Sugar">Sony Sugar</option> <option value = "Sofapaka">Sofapaka</option> <option value = "Thika Utd">Thika Utd</option> <option value = "Tusker">Tusker</option> <option value = "Ulinzi Stars">Ulinzi Stars</option> <option value = "Ushuru">Ushuru</option> <option value = "Western Stima">Western Stima</option> <select><br><br> <td> <input type="submit" name="submit" value="Submit"/> </form> Any ideas it is currently not working.
  4. Here is the code in a more presentable manner, hopefully it looks better function predictor ($home_team, $away_team){ global $con; $result=[0,0]; // For the home team $home_team_strength=get_query_value($con,"SELECT SUM(HTgoals)/Count(*)FROM `results` WHERE Home_team = '$home_team' "); $league_strength=get_query_value($con,"SELECT SUM(HTgoals)/Count(*)FROM `results`"); $home_team_attack_strength=$home_team_strength/$league_strength; $away_team_strength = get_query_value($con, "SELECT SUM(HTgoals)/Count(*)FROM `results` WHERE Away_team='$away_team'"); $away_team_defence_strength=$away_team_strength/$league_strength; $result[0]=$home_team_attack_strength*$away_team_defence_strength*$league_strength; // For the away team $away_team_strength_2 = get_query_value($con, "SELECT SUM(ATgoals)/Count(*) FROM `results` WHERE Away_team='$away_team'"); $league_away_strength=get_query_value($con, "SELECT SUM(ATgoals)/Count(*)FROM `results`"); $away_team_attack_strength = $away_team_strength_2/$league_away_strength; $home_team_defence =get_query_value($con, "SELECT SUM(ATgoals)/Count(*)FROM `results` WHERE Home_team='$home_team'"); $home_team_defence_strength =$home_team_defence/$league_away_strength; $result[1] = $away_team_attack_strength*$home_team_defence_strength*$league_away_strength; return $result; } function get_query_value ($con,$query){ $return_value=-1; $con->real_query($query); $result=$con->use_result(); $row=$result->fetch_row(); $return_value=$row[0]; $result->close(); }
  5. I have a PHP function that queries a database and then uses the results of the different queries to give me a result but I am getting a divide by zero warning, however when I run the queries on the database I get the result I need but on calling the PHP function I get the warning. What could be the problem? Here is the function code function predictor ($home_team, $away_ team){ global $con; $result=[0,0]; // For the home team $home_team_strength=get_query_value($ con,"SELECT SUM(HTgoals)/Count(*) FROM `results` WHERE Home_team = '$ home_team' "); $league_strength=get_query_value($ con,"SELECT SUM(HTgoals)/Count(*) FROM `results`"); $home_team_attack_strength=$home_ team_strength/$league_strength; $away_team_strength = get_query_value($ con, "SELECT SUM(HTgoals)/Count(*) FROM `results` WHERE Away_team='$ away_team'"); $away_team_defence_strength=$away_ team_strength/$league_strength; $result[0]=$home_team_attack_strength*$ away_team_defence_strength*$league_ strength; // For the away team $away_team_strength_2 = get_query_ value($con, "SELECT SUM(ATgoals)/Count (*) FROM `results` WHERE Away_team='$ away_team'"); $league_away_strength=get_query_value($ con, "SELECT SUM(ATgoals)/Count(*) FROM `results`"); $away_team_attack_strength = $away_ team_strength_2/$league_away_strength; $home_team_defence =get_query_value($ con, "SELECT SUM(ATgoals)/Count(*) FROM `results` WHERE Home_team='$ home_team'"); $home_team_defence_strength =$home_ team_defence/$league_away_strength; $result[1] = $away_team_attack_strength* $home_team_defence_strength*$league_ away_strength; return $result; } function get_query_value ($con,$query){ $return_value=-1; $con->real_query($query); $result=$con->use_result(); $row=$result->fetch_row(); $return_value=$row[0]; $result->close(); }
×
×
  • 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.