Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/221386-reducing-if-statements/
Share on other sites

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?

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.

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
        }
    }

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.

<?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 ??

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

<?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

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.

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.

<?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)

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);

 

?>

<?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.

 

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.