karimali831 Posted December 12, 2010 Share Posted December 12, 2010 Hi, What is a shorter way instead of having all these if statements? Here it is: if($matchno==1) return 66; elseif($matchno==2) return 67; elseif($matchno==3) return 68; elseif($matchno==4) return 69; elseif($matchno==5) return 70; elseif($matchno==6) return 71; elseif($matchno==7) return 72; elseif($matchno== return 73; elseif($matchno==9) return 74; elseif($matchno==10) return 75; elseif($matchno==11) return 76; elseif($matchno==12) return 77; elseif($matchno==13) return 78; elseif($matchno==14) return 79; elseif($matchno==15) return 80; elseif($matchno==16) return 81; elseif($matchno==17) return 82; elseif($matchno==18) return 83; elseif($matchno==19) return 84; elseif($matchno==20) return 85; elseif($matchno==21) return 86; elseif($matchno==22) return 87; elseif($matchno==23) return 88; elseif($matchno==24) return 89; elseif($matchno==25) return 90; elseif($matchno==26) return 91; elseif($matchno==27) return 92; elseif($matchno==28) return 93; elseif($matchno==29) return 94; elseif($matchno==30) return 95; elseif($matchno==31) return 96; elseif($matchno==32) return 97; someone please code shorter? I will learn this way. Thanks. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2010 Share Posted December 12, 2010 It looks like you are adding 65 to $matchno, did you try doing that? Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 hmm? no do not need to do this. I think I need to be using for () to make it shorter? Quote Link to comment Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 Hi there, What are you trying to do in your code? If we have a better understanding of your requirements, then maybe we can come up with a shorter version. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 ok, if matchno equals a value then I want it to return another value but for this, I must use hundreds of if statements which I do not want. If you look at the code above, it follows a pattern. if matchno = 1 2 3 4 5 6 7 then return= 66 67 68 69 70 71 72 I simply want an alternative method instead of using soo many if statements. or is this only way? Quote Link to comment Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 Okay, do you have a specific logic behind those numbers/pattern? Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 I hope you understand when I show you this. this is basically what I want: http://team-x1.co.uk/site/test.html and the reason behind this is: http://team-x1.co.uk/site/admin/templates/64.html a tournament. the looser in $match[1] && $match[2] will go to $match[66] etc it is for automatic switching so I need to return value. if you are familar with tournaments, look on the template where $match[1] and $match[66] is and you should understand. Quote Link to comment Share on other sites More sharing options...
anasather Posted December 12, 2010 Share Posted December 12, 2010 <?php $matchno=6; $diff = 65; for ($i=1; $i<33; $i++) { if ($i = $matchno) { echo "Matchno: " . $i . " Return: " . ($i+$diff) . "<br>"; return $i+$diff; break; } } ?> Try this if it works Quote Link to comment Share on other sites More sharing options...
laffin Posted December 12, 2010 Share Posted December 12, 2010 Pretty familiar with tourneys, but there is no real sense to your logic. you don't really define teams, nor does yer system promote the winner of the round nor do you express which rounds a team is participating in when you define those variables, its much easier to imagine the system, which is nothing more than the elimination of a team, and move to the next round, until you have 1 winner. Very Simple Tourney system to get you started <?php $teams=array('Alpha','Beta','Gamma','Delta','Epsilon','Zeta','Eta'); $round=0; $participants=$teams; while(count($participants)>1) { $round++; // Increment our round Echo 'Round '. $round. PHP_EOL; $tables=array(); // Clear our tables $index=0; while(count($tables) < floor(count($participants)/2)) // want an even amount of tables $tables[]=array($participants[$index++],$participants[$index++]); if($index<count($participants))// extra team, add to tables, but no opposing team $tables[]=array($participants[$index++],NULL); $participants=array(); // clear out next round participants foreach($tables as $idx=>$table) { $tbl=$idx+1; echo " Table #{$tbl}: "; if($table[1]===NULL) // extra team advances to next level automatically { echo "{$table[0]} Holdover"; $winner=0; } else { echo "{$table[0]} vs. {$table[1]}"; $winner=rand(0,1); // Generate a winner } echo " - Winner {$table[$winner]}". PHP_EOL; $participants[]=$table[$winner]; // Add WInnerto next round } } Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 Pretty familiar with tourneys, but there is no real sense to your logic. you don't really define teams, nor does yer system promote the winner of the round nor do you express which rounds a team is participating in when you define those variables, its much easier to imagine the system, which is nothing more than the elimination of a team, and move to the next round, until you have 1 winner. Very Simple Tourney system to get you started <?php $teams=array('Alpha','Beta','Gamma','Delta','Epsilon','Zeta','Eta'); $round=0; $participants=$teams; while(count($participants)>1) { $round++; // Increment our round Echo 'Round '. $round. PHP_EOL; $tables=array(); // Clear our tables $index=0; while(count($tables) < floor(count($participants)/2)) // want an even amount of tables $tables[]=array($participants[$index++],$participants[$index++]); if($index<count($participants))// extra team, add to tables, but no opposing team $tables[]=array($participants[$index++],NULL); $participants=array(); // clear out next round participants foreach($tables as $idx=>$table) { $tbl=$idx+1; echo " Table #{$tbl}: "; if($table[1]===NULL) // extra team advances to next level automatically { echo "{$table[0]} Holdover"; $winner=0; } else { echo "{$table[0]} vs. {$table[1]}"; $winner=rand(0,1); // Generate a winner } echo " - Winner {$table[$winner]}". PHP_EOL; $participants[]=$table[$winner]; // Add WInnerto next round } } what are you talking about? my tournament works perfect, I am just extending it to support 128 teams. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 <?php $matchno=6; $diff = 65; for ($i=1; $i<33; $i++) { if ($i = $matchno) { echo "Matchno: " . $i . " Return: " . ($i+$diff) . "<br>"; return $i+$diff; break; } } ?> Try this if it works thanks anas, this is exackly what I have done but the different won't always be 65. I don't think you seen this yet: http://team-x1.co.uk/site/test.html ?? Quote Link to comment Share on other sites More sharing options...
laffin Posted December 12, 2010 Share Posted December 12, 2010 The problem is your system is hardcoded, as u said your extending it to support 128 teams. a system like i describe is dynamic, the tables/rounds are all generated at run time, not design time. Quote Link to comment Share on other sites More sharing options...
anasather Posted December 12, 2010 Share Posted December 12, 2010 The problem is your system is hardcoded, as u said your extending it to support 128 teams. a system like i describe is dynamic, the tables/rounds are all generated at run time, not design time. Yes you are right, I created dynamic Tabular system in OOP that was backed end by massive PHP Code but If it is dynamic it saves time and behaves according to condition Quote Link to comment Share on other sites More sharing options...
anasather Posted December 12, 2010 Share Posted December 12, 2010 <?php $matchno=6; $diff = 65; for ($i=1; $i<33; $i++) { if ($i = $matchno) { echo "Matchno: " . $i . " Return: " . ($i+$diff) . "<br>"; return $i+$diff; break; } } ?> Try this if it works thanks anas, this is exackly what I have done but the different won't always be 65. I don't think you seen this yet: http://team-x1.co.uk/site/test.html ?? Yes! Karim, just move $diff inside loop then provide $matchno and $diff from same Multidimensional array like $arr[$i]["matchno"] && $arr[$i]["diff"] That was just for an example//// Good Luck Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 The problem is your system is hardcoded, as u said your extending it to support 128 teams. a system like i describe is dynamic, the tables/rounds are all generated at run time, not design time. It will take a week or two to design 128 brackets. Very long, but to allow customization for other users, there is no alternative. Quote Link to comment Share on other sites More sharing options...
laffin Posted December 12, 2010 Share Posted December 12, 2010 It will take a week or two to design 128 brackets. Very long, but to allow customization for other users, there is no alternative. Uhm, thats what dynamic is. A system by which changes according to the variables. Hardcoding things, is static, like html pages, cant change a thing unless u edit the html script dynamic pages, give it a few variables, and it gives data out according to those variables. Quote Link to comment Share on other sites More sharing options...
anasather Posted December 12, 2010 Share Posted December 12, 2010 <?php $input = $_GET["input"]; $diff = 66; $start=1; $inc = 0; for ($i=1; $i<17; $i++) { //echo "start: " . ($start+$i+$inc-1) . " Return: " . ($start +$diff-1) . "<br>"; //echo "start: " . ($start+$i+$inc) . " Return: " . ($start +$diff-1) . "<br>"; //echo "<br><br>"; $anas[] = ($start+$i+$inc-1) . "." . ($start +$diff-1); $anas[] = ($start+$i+$inc) . "." . ($start +$diff-1); $diff++; $inc ++; } foreach ($anas as $val) { if ($input == substr($val, 0, strrpos($val, "."))) { echo "Matchno " . substr($val, 0, strrpos($val, ".")) . " Return " . substr($val, (strrpos($val, ".")+1)) . "<br>"; } } //print_r($anas); ?> http://www.khansolicitors.org.uk/karim/short.php?input=13 (Input any value) Quote Link to comment Share on other sites More sharing options...
anasather Posted December 12, 2010 Share Posted December 12, 2010 http://www.khansolicitors.org.uk/karim/short.php?input=13 (any value here "matchno") <?php $input = $_GET["input"]; $diff = 66; $start=1; $inc = 0; for ($i=1; $i<17; $i++) { //echo "start: " . ($start+$i+$inc-1) . " Return: " . ($start +$diff-1) . "<br>"; //echo "start: " . ($start+$i+$inc) . " Return: " . ($start +$diff-1) . "<br>"; //echo "<br><br>"; $anas[] = ($start+$i+$inc-1) . "." . ($start +$diff-1); $anas[] = ($start+$i+$inc) . "." . ($start +$diff-1); $diff++; $inc ++; } foreach ($anas as $val) { if ($input == substr($val, 0, strrpos($val, "."))) { echo "Matchno " . substr($val, 0, strrpos($val, ".")) . " Return " . substr($val, (strrpos($val, ".")+1)) . "<br>"; } } //print_r($anas); ?> Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 perfect, thanks very much! Quote Link to comment Share on other sites More sharing options...
laffin Posted December 12, 2010 Share Posted December 12, 2010 <?php $teams=135; $participants=$teams; $cur_round=1; $total_matches=$offset=0; while($participants>1) { $matches=floor($participants/2); $total_matches+=$matches; echo "Round #{$cur_round}: {$matches} matches {$offset} offset <br />". PHP_EOL; $offset+=ceil($participants/2); $cur_round++; $participants-=$matches; } echo "Winner after {$total_matches} matches. <br />". PHP_EOL; See no hard coding required if u have the proper math involved. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 My question has been answered, I did not ask "how do I make this dynamic?" . Understand that not everyone is as good for your ways of doing things. Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 is it not as simple as: return $matchno+65; of course, this is only if $matchno is a number, and if you want to check if it is between something, just add that before this... if($matchno>0 && $matchno<33){ return $matchno+65; } Quote Link to comment Share on other sites More sharing options...
karimali831 Posted December 12, 2010 Author Share Posted December 12, 2010 no it is not as simple as that because it will not always be +65 if you looked at http://team-x1.co.uk/site/test.html you would of known. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 12, 2010 Share Posted December 12, 2010 you would HAVE known. Quote Link to comment 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.