php_b34st Posted August 9, 2008 Share Posted August 9, 2008 Hi this is a fairly complicated procedure im trying to do but i will try to explain as much as I can. I have a database with approx 1000 entries in it. A user has to pick 11 of these entries (a full football squad) but there is limitations to the type of team they can have there is 3 rules. 1. The team's value cannot exceed £50million. 2. The team must have one Goalkeeper, four Defenders, three or four Midfielders, and two or three Strikers. (442 or 433) 3. A user must pick no more than two players from any one Premier League club. Achieving the results for the first rule was straight forward I just added up all the player prices then used an if statement. The second rule was a bit more complicated but I have managed to get the results I need by using a switch statement although im not sure this is the easiest way. I dont know where to start with rule 3, i tried using a switch statement like in rule 2 but there is too many clubs. here is what I have so far: <?php //Define short variables $formation = $_POST["formation"]; $player1 = $_POST["player1"]; $player2 = $_POST["player2"]; $player3 = $_POST["player3"]; $player4 = $_POST["player4"]; $player5 = $_POST["player5"]; $player6 = $_POST["player6"]; $player7 = $_POST["player7"]; $player8 = $_POST["player8"]; $player9 = $_POST["player9"]; $player10 = $_POST["player10"]; $player11 = $_POST["player11"]; //Add players to an array $players = array("$player1", "$player2", "$player3", "$player4", "$player5", "$player6", "$player7", "$player8", "$player9", "$player10", "$player11"); //find out formation and define positions switch($formation) { case 442: $def = '4'; $mid = '4'; $str = '2'; break; case 433: $def = '4'; $mid = '3'; $str = '3'; break; } $total = '0'; foreach($players as $code) { //grab player stats from db $query = "SELECT name, club, price, position FROM playerlist WHERE code = $code"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { preg_match_all('#£(.*?)m#s', $row['price'], $data); $price = $data[1][0]; $position = $row['position']; //count positions switch($position) { case 'DEF': $def1++; break; case 'MID': $mid1++; break; case 'STR': $str1++; break; } //display stats echo 'Name: ' . $row['name'] . '</br> Club: ' . $row['club'] . '</br> Price: ' . $price . '</br> Position: ' . $row['position'] . '</br></br>'; //add up prices $total = $total + $price; } } //compare formations if($def == $def1 && $mid == $mid1 && $str == $str1) { echo 'formation match'; } else { echo 'formation does not match'; } //check if team is within budget echo $total; if($total > 50) { echo "SPENT TOO MUCH"; } else { echo "team ok"; } ?> Any help would be great thanks Link to comment https://forums.phpfreaks.com/topic/118952-limiting-results-from-a-db/ Share on other sites More sharing options...
xoligy Posted August 9, 2008 Share Posted August 9, 2008 Im not good with php but i think i would look into some sort me function with cm if staunent that says <3 from team x sorry for the bad english no mobile Link to comment https://forums.phpfreaks.com/topic/118952-limiting-results-from-a-db/#findComment-612525 Share on other sites More sharing options...
php_b34st Posted August 9, 2008 Author Share Posted August 9, 2008 Thanks for the reply, im not sure sort is required it does not matter what order the clubs are arranged if i had them in an array I think the if statement <3 would work but i dont know how to do the comparison Link to comment https://forums.phpfreaks.com/topic/118952-limiting-results-from-a-db/#findComment-612532 Share on other sites More sharing options...
xoligy Posted August 9, 2008 Share Posted August 9, 2008 its hard to explain no a mobile but u need a mysql query thats checks the team against the player and the value must be less than 3 hope that makes sense if not sorry! Link to comment https://forums.phpfreaks.com/topic/118952-limiting-results-from-a-db/#findComment-612536 Share on other sites More sharing options...
php_b34st Posted August 9, 2008 Author Share Posted August 9, 2008 Thanks for the help xoligy i have achieved the output i required here is the updated code: <?php //Define short variables $formation = $_POST["formation"]; $player1 = $_POST["player1"]; $player2 = $_POST["player2"]; $player3 = $_POST["player3"]; $player4 = $_POST["player4"]; $player5 = $_POST["player5"]; $player6 = $_POST["player6"]; $player7 = $_POST["player7"]; $player8 = $_POST["player8"]; $player9 = $_POST["player9"]; $player10 = $_POST["player10"]; $player11 = $_POST["player11"]; //Add players to an array $players = array("$player1", "$player2", "$player3", "$player4", "$player5", "$player6", "$player7", "$player8", "$player9", "$player10", "$player11"); //find out formation and define positions switch($formation) { case 442: $def = '4'; $mid = '4'; $str = '2'; break; case 433: $def = '4'; $mid = '3'; $str = '3'; break; } $total = '0'; $clubs = array(); foreach($players as $code) { //grab player stats from db $query = "SELECT name, club, price, position FROM playerlist WHERE code = $code"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { preg_match_all('#£(.*?)m#s', $row['price'], $data); $price = $data[1][0]; $position = $row['position']; $clubs[$row['club']] = $clubs[$row['club']] + 1; //count positions switch($position) { case 'DEF': $def1++; break; case 'MID': $mid1++; break; case 'STR': $str1++; break; } //display stats echo 'Name: ' . $row['name'] . '</br> Club: ' . $row['club'] . '</br> Price: ' . $price . '</br> Position: ' . $row['position'] . '</br></br>'; //add up prices $total = $total + $price; } } foreach($clubs as $amount) { if($amount > 2) { echo 'same team used more than twice'; } } //compare formations if($def == $def1 && $mid == $mid1 && $str == $str1) { echo 'formation match'; } else { echo 'formation does not match'; } //check if team is within budget echo $total; if($total > 50) { echo "SPENT TOO MUCH"; } else { echo "team ok"; } ?> can anyone suggest any way of tidying all this up? every little helps thanks Link to comment https://forums.phpfreaks.com/topic/118952-limiting-results-from-a-db/#findComment-612559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.