php - How to name groups of radio buttons that are created dynamically depending on varied conditions

Firstly I have checkout the web but found nothing that gives a satisfactory answer to my problem. I am creating a scoring system for our lawn bowl club. Up to 7 games are played in a session and the winner of each game goes into a random draw for the day's winner and gets the prize. The scores are entered into my system and it produces a list of game-winners. If a game is drawn, however, the agreed winner (by the toss of a coin) must be selected by clicking on the appropriate radio button. This works perfectly ok if there is just one drawn game and one radio button group, and the overall session winners can then be decided. If, however, a second or subsequent game is drawn the radio button names are all the same. I am not sure how to give the addition radio button groups a different name.

I have tried adding just a number to the name i.e. draw0, draw1, etc, and then using PHP echo to add it in as the name. This didn't work. I've also tried putting it into an array and then using PHP echo to add it in. This didn't work either. This is my current code that works for one radio group. This creates the list of winners:-

<?php
if (!isset($_POST['draw'])){  //checks if radio button is checked
    ?>
    <form name="displayWinners" action="processScores.php" onsubmit="return validateForm()" method="post">
    
    
    <table width="70%" align="center" border="1">
        <tr width="80%" align="center" >
        <td align="center" colspan="2"><h2>Winners of individual games</h2></td>
        </tr>
        
        <?php
        
        for ($w = 0; $w < count($gameWinners); $w++){
                
        ?>
            
            <tr width="70%" align="center">
            <td width="15%" >
            <?php
                $n = $gameWinners[$w][0];
                ?> <b> <?php echo "Rink ". $gameWinners[$w][5]; ?> <b> 
            </td>   
            <td align="center">
                <?php 
                if ($gameWinners[$w][6] == true) {  // The game is a draw
                    echo "This game was a draw. Select the agreed winner - ". ($gameWinners[$w][4]-1)."  ";
                    $f = ($gameWinners[$w][4] - 1);  //team no

                    ?>
                    <!--Creates radio buttons-->                    
                    <input type="radio" name="draw" value="<?php echo $f; ?>" /><?php echo $team[$f][2]."'s team"; ?>  <?  //echo $name; ?>
                    <input type="radio" name="draw" value="<?php echo ($f + 1); ?>" /><?php  echo $team[$f+1][2]."'s team"; ?>
                    <?php
                
                }else{
                    $n = ($gameWinners[$w][4] - 1 );
                    if ($gameType == 2){
                        ?> <b> <?php echo  " ".$team[$n][2].",   ". $team[$n][3]; ?> </b> <?php 
                    }elseif ($gameType == 3){
                        ?> <b> <?php echo @$team[$n][2].",   ". @$team[$n][3].",   ". @$team[$n][5]; ?> </b> <?php  
                    }elseif ($gameType == 4){
                        ?> <b> <?php echo $team[$n][2].",   ". $team[$n][3].",   ". $team[$n][4].",   ". $team[$n][5]; ?> </b> <?php    
                    }
                }
                
                ?>
            </td>
            </tr>
        <?php
        }
}       
        ?>

The code I tried but didn't work was:-

if (!isset($_POST['draw0'])){  //checks if radio button is checked
    ?>
    <form name="displayWinners" action="processScores.php" onsubmit="return validateForm()" method="post">
    
    
    <table width="70%" align="center" border="1">
        <tr width="80%" align="center" >
        <td align="center" colspan="2"><h2>Winners of individual games</h2></td>
        </tr>
        
        <?php
        
        for ($w = 0; $w < count($gameWinners); $w++){
            
        ?>
            
            <tr width="70%" align="center">
            <td width="15%" >
            <?php
                $n = $gameWinners[$w][0];
                ?> <b> <?php echo "Rink ". $gameWinners[$w][5]; ?> <b> 
            </td>   
            <td align="center">
                <?php 
                if ($gameWinners[$w][6] == true) {  // The game is a draw
                    echo "This game was a draw. Select the agreed winner - ". ($gameWinners[$w][4]-1)."  ";
                    $f = ($gameWinners[$w][4] - 1);  //team no
                    $p = 0;
                    $name = "draw$p";
                    echo "name - ". $name."<br>";  //This displays 'draw0' as expected
                    ?>
                    <!--Creates radio buttons    changed just here-->                   
                    <input type="radio" name="<?php echo $name; ?>" value="<?php echo $f; ?>" /><?php echo $team[$f][2]."'s team"; ?>  <?  //echo $name; ?>
                    <input type="radio" name="<?php echo $name; ?>" value="<?php echo ($f + 1); ?>" /><?php  echo $team[$f+1][2]."'s team"; ?>
                    <?php
                
                }else{
                    $n = ($gameWinners[$w][4] - 1 );
                    if ($gameType == 2){
                        ?> <b> <?php echo  " ".$team[$n][2].",   ". $team[$n][3]; ?> </b> <?php 
                    }elseif ($gameType == 3){
                        ?> <b> <?php echo @$team[$n][2].",   ". @$team[$n][3].",   ". @$team[$n][5]; ?> </b> <?php  
                    }elseif ($gameType == 4){
                        ?> <b> <?php echo $team[$n][2].",   ". $team[$n][3].",   ". $team[$n][4].",   ". $team[$n][5]; ?> </b> <?php    
                    }
                }
                
                ?>
            </td>
            </tr>
        <?php
        }
}       
        ?>
        
        
    </table> 

This was a test I did and the only things changed were how the name was added into the radio button. The (!isset) bit was changed was 'draw0' was the expected name. When I tried the code the 'if (!isset($_POST['draw0'])){ ' part would not recognize the name 'draw0'.

Any ideas where I'm going wrong.

Answer

Solution:

Here's a way that one might go about naming the radio-button groups. Simply keep track of which pair of scores we're comparing and append that to the name of the associated radio-buttons.

First we count the number of entries in the array that holds the days scores. This'll be 7. Not that we actually need this number...

Each of the entries is an array that holds player1's score and player2's score too. If both elements are the same, it's a draw and we present the radio-buttons.

If not, we just show 'winner' and 'loser' if p1's score is greater, or 'loser' & 'winner' if p2's score is larger.

I've opted to hard-code the scores and a means to deal with them since your code is both (0) without any examples of input-data and (1) making my eyes hurt with the interspersed php code & html elements. Hope it's of utility to you. :)

<!doctype html>
<html>
<head>
<style>
span
{
    padding: 4px;
    margin: 4px;
    border: solid 1px #888;
}
</style>
</head>
<body>
<?php
$scores = [ [10,10], [4,5], [3,2], [1,3], [8,4], [20,20], [10,10] ];
$numGames = count($scores);

$gameIndex = 0;
forEach ($scores as $score)
{
    if ($score[0] == $score[1])
    {
        printf("Game #%d is a draw.<br>", $gameIndex + 1);
        printf("Please designate the winner:<br>");
        printf("<label>Player #1<input type='radio' name='game_%d' value='player1'></label>", $gameIndex+1);
        printf("<label>Player #2<input type='radio' name='game_%d' value='player2'></label>", $gameIndex+1);
        printf("<br>");
    }
    else
    {
        if ($score[0] > $score[1])
        {
            $p1 = 'winner';
            $p2 = 'loser';
        }
        else
        {
            $p1 = 'loser';
            $p2 = 'winner';
        }
        printf("Game #%d: <span>%s</span><span>%s</span><br>", $gameIndex + 1, $p1, $p2);
    }
    ++$gameIndex;
}
?></body>
</html>

Source