0

Developing a form using custom module for members to update football match day statistics. These include team sheets, goals substitutions. I have been able to get the team sheets working grand but stuck on adding in the goal scorers. On my form I will have to have a row per goal, so if a team scores 1,2,X amount of goals in the game I will need the same amount of rows. The data to be saved will be player, time of goal and whether a penalty or not.

I have a drop down list for the players name, textbox for time and a checkbox for penalty.

What I need help with is how to display the number of rows per goal. And I would also like use the same form for adding and editing the goals.

My module function

public function match_stats(){

    $match_id = ee()->TMPL->fetch_param('match_id');
    $team_id = ee()->TMPL->fetch_param('team_id');
    $match = ee()->clubmanager_matches->__get_match($match_id);
    $sql = "SELECT club_id FROM exp_ca_exec_clubs where member_id = ?";
    $query = ee()->db->query($sql, array(ee()->session->userdata('member_id')));

    $club_id = $query->row()->club_id;  
    $season_id = 16;

    // Build an array to hold the form's hidden fields
    $hidden_fields = array(
        "match_id" => $match_id,
        "club_id" => $club_id,
        "team_id" => $team_id,
        "season_id" => $season_id,
        "ACT" => ee()->functions->fetch_action_id( 'Clubmanager', 'match_team_action' ),
        "return" => ee()->TMPL->fetch_param('return')
    );

    // Build an array with the form data
    $form_data = array(
        // Send form back to this url - the action must be the same as the form
        "action" => ee()->functions->create_url( ee()->uri->uri_string ), 
        "id" => ee()->TMPL->form_id,
        "class" => 'form-horizontal',
        "hidden_fields" => $hidden_fields
    );

    // Make sure errors is actually an array
    if( ! is_array( self::$errors ) ) {
        self::$errors = array();
    }

    // Match Stats Goals
    $matchPlayersList = ee()->clubmanager_players->__get_match_players_list($team_id, $match_id);
    $clubplayerslist = ee()->clubmanager_players->__get_club_players_list($club_id,  $season_id);

    if($matchPlayersList > 0):
        $variables['players'] = $matchPlayersList;
    else:
        $variables['players'] = $clubplayerslist;
    endif;

    // Set up array to parse tagdata
    $variables['errors'] = array(
        array( self::$errors )// list of error messages
    );

    // Fetch contents of the tag pair, ie, the form contents
    $tagdata = ee()->TMPL->tagdata;

    // Parse tagdata for errors/submitted values
    $tagdata = ee()->TMPL->parse_variables_row( $tagdata, $variables );

    // Create the form
    $form = ee()->functions->form_declaration($form_data) . 
        $tagdata . 
        "</form>";

    return $form;

}

My template code

{exp:clubmanager:match_stats 
                    match_id="{segment_3}"
                    team_id="{segment_4}"
                    return="clubhouse/fixtures/edit-success"
}
 <!--Table Search v1-->
                <div class="table-search-v1 margin-bottom-20">
                    <div class="table-responsive">
                        <table class="table table-hover table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>Player Name</th>
                                    <th>Assist Player Name</th>
                                    <th>Goal Time</th>
                                    <th>Penalty</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>


                                      <select name="player_goal">
                                        <option value="0">Select Player</option>
                                        {players}
                                        <option value="{id}">{name}</option>
                                        {/players}
                                      </select>

                                    </td>
                                     <td>
                                        <select name="player_assist">
                                        <option value="0">Select Player</option>
                                        {players}
                                        <option value="{id}">{name}</option>
                                        {/players}
                                      </select>
                                    </td>
                                        <!-- Text input-->
                                     <td>
                                      <input type="text" placeholder="time"     class="form-control" name="time" value="{time}"  />
                                    </td>
                                    <td>

                                    </td>
                                    <td>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>    
                </div>
                <!--End Table Search v1-->
0

I would use a grid field type, and programatically set the new rows on your template.

Ref :

0

So after some head scratching I have came up with this as they way to do it. There are going to be better ways to do this but this works for me at the moment. I have only included the main part of the code to build out the form tags.

// If no recorded goals fill out form with blank rows for score
    if($recorded_goals == 0){
        $num = 1;
        $goals = array();
        for ($i = 0; $i < $score; $i++){
            $goals[] = array('num' => $num++,
                             'row' => $i
                );
            }
        $variables['goals'] =  $goals ;
    }elseif($recorded_goal_count >= $score){// Display all recorded goals in the form.
                foreach ($recorded_goals as $row){
                    $i=0;
                    $num = 1;
                    $goals = array();
                    foreach ($recorded_goals as $goal)
                    {
                        $goals[] = array('num' => $num++,
                                         'row' => $i++,
                                         'goal_id' => $goal['goal_id'],
                                         'player_name' => $goal['player_name'], 
                                         'player_id' => $goal['player_id'],
                                         'assist_name' => $goal['assist_name'],
                                         'assist_id' => $goal['assist_id'],
                                         'goal_id' => $goal['goal_id'],
                                         'time' => $goal['time'],
                                         'penalty' => $goal['penalty']
                                        );
                    }
                    $variables['goals'] =  $goals ;
                }
    }elseif($recorded_goal_count < $score){//Not enough recorded goals fill form will data and blank rows.
        foreach ($recorded_goals as $row){
            $i=0;
            $num = 1;
            $goals = array();
                foreach ($recorded_goals as $goal)
                {
                    $goals[] = array('num' => $num++,
                                     'row' => $i++,
                                     'goal_id' => $goal['goal_id'],
                                     'player_name' => $goal['player_name'], 
                                     'player_id' => $goal['player_id'],
                                     'assist_name' => $goal['assist_name'],
                                     'assist_id' => $goal['assist_id'],
                                     'goal_id' => $goal['goal_id'],
                                     'time' => $goal['time'],
                                     'penalty' => $goal['penalty']
                                    );
                }
                $variables['goals'] =  $goals ;
                for ($i = $recorded_goal_count; $i < $score; $i++){
                    $goals[] = array('num' => $num++,
                                    'row' => $i
                        );
                }
            $variables['goals'] =  $goals ;
        }
    }

Template code to process the form

 {exp:clubmanager:match_stats 
                    match_id="{segment_3}"
                    team_id="{segment_4}"
                    return="clubhouse/fixtures/edit-success"
                }
  <!--Table Search v1-->
                <div class="table-search-v1 margin-bottom-20">
                    <div class="table-responsive">
                        <table class="table table-hover table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>No.</th>
                                    <th>Player Name</th>
                                    <th>Assist Player Name</th>
                                    <th>Goal Time</th>
                                    <th>Penalty</th>
                                    <th>Delete?</th>
                                </tr>
                            </thead>
                            <tbody>
                            {goals}
                                <tr>
                                    <td>
                                    {num}
                                    </td>
                                    <td>
                                    <input type="hidden" name="goals[{row}][goal_id]" value="{goal_id}" />
                                    <select name="goals[{row}][player_id]">
                                        <option value="0">Select Player</option>
                                        {players}
                                        <option value="{id}" {if id == player_id}selected{/if}>{name}</option>
                                        {/players}
                                      </select>

                                    </td>
                                     <td>
                                        <select name="goals[{row}][assist]">
                                        <option value="0">Select Player</option>
                                        {players}
                                        <option value="{id}" {if id == assist_id}selected{/if}>{name}</option>
                                        {/players}
                                      </select>
                                    </td>
                                        <!-- Text input-->
                                     <td>
                                    <input type="text" placeholder="time" class="form-control" name="goals[{row}][time]" value="{if time}{time}{/if}"  />
                                    </td>
                                    <td>
                                          <select name="goals[{row}][penalty]" class="form-control">
                                        {exp:low_variables:options var="lv_penalty"}
                                                <option value="{lv_penalty:data}" {if penalty == lv_penalty:data}selected{/if}>{lv_penalty:data_label}</option>
                                        {/exp:low_variables:options}
                                        </select>
                                    </td>
                                    <td>
                                    <input type="checkbox" name="goals[{row}][delete]" value="delete_goal"  />
                                    </td>
                                </tr>
                                {/goals}
                            </tbody>
                        </table>
                    </div>    
                </div>
                <!--End Table Search v1-->
{/exp:clubmanager:match_stats}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.