使用 CodeIgniter 动态选择下拉菜单

发布于 2024-10-24 06:50:03 字数 6083 浏览 1 评论 0原文

我正在使用 CodeIgniter 开发一个 Web 应用程序,想知道是否有人可以帮助解决出现的问题。

我有两个下拉菜单 - 部门和团队。我正在创建一个固定装置,因此希望能够允许用户从第一个下拉列表中选择一个部门,然后在第二个下拉列表中仅包含所选部门中的球队。

到目前为止我的代码:

控制器:

function create() { 
    $data['division_list'] = $this->add_fixture_model->get_divisions();
    $data['team_list'] = $this->add_fixture_model->get_teams();
    $data['referee_list'] = $this->add_fixture_model->get_referee();


    // field name, error message, validation rules
    $this->form_validation->set_rules('team_name', 'Team Name', 'trim|required');
    $this->form_validation->set_rules('home_team', 'Home Team Name', 'trim|required');
    $this->form_validation->set_rules('away_team', 'Away Team Name', 'required');
    $this->form_validation->set_rules('division_name', 'Division', 'trim|required');
    $this->form_validation->set_rules('referee', 'Referee', 'trim|required');
    $this->form_validation->set_rules('fixture_week', 'Fixture Week', 'trim|required');
    $this->form_validation->set_rules('fixture_day', 'Fixture Day', 'trim|required');
    $this->form_validation->set_rules('fixture_month', 'Fixture Month', 'trim|required');
    $this->form_validation->set_rules('fixture_year', 'Fixture Year', 'trim|required');
    $this->form_validation->set_rules('confirm_day', 'Fixture Confirm Day', 'trim|required');
    $this->form_validation->set_rules('confirm_month', 'Fixture Confirm Month', 'trim|required');
    $this->form_validation->set_rules('confirm_year', 'Fixture Confirm Year', 'trim|required');




    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('includes/top');
        $this->load->view('includes/header');
        $this->load->view('league/add_fixture', $data);
        $this->load->view('includes/footer');
    }

    else
    {           
        if($query = $this->add_fixture_model->add_fixture())
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/fixture_added');
            $this->load->view('includes/footer');

        }
        else
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/fixture_added', $data);
            $this->load->view('includes/footer');       
        }
    }

}

型号:

class Add_fixture_model extends Model {

function add_fixture()
{

    $add_new_fixture = array(
        'team_name' => $this->input->post('team_name'),
        'home_team' => $this->input->post('home_team'),
        'away_team' => $this->input->post('away_team'),
        'division' => $this->input->post('division_name'),
        'ground' => $this->input->post('ground'),
        'ground_postcode' => $this->input->post('ground_postcode'),
        'referee' => $this->input->post('referee'),
        'fixture_week' => $this->input->post('fixture_week'),
        'fixture_day' => $this->input->post('fixture_day'),
        'fixture_month' => $this->input->post('fixture_month'),
        'fixture_year' => $this->input->post('fixture_year'),
        'confirm_day' => $this->input->post('confirm_day'),
        'confirm_month' => $this->input->post('confirm_month'),
        'confirm_year' => $this->input->post('confirm_year')                            
    );


    $insert = $this->db->insert('fixtures', $add_new_fixture);


    return $insert;

}


function get_divisions()
{
      $this->db->from('divisions');
      $this->db->order_by('division_name');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a division';
        $return[$row['id']] = $row['division_name'];
            }
        }

    return $return;
}

function get_teams()
{
      $this->db->from('teams');
      $this->db->order_by('team_name');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a team';
        $return[$row['id']] = $row['team_name'];
            }
        }

    return $return;
}

function list_teams() {
    $this->db->from('teams');
    $this->db->where('division_id', $this->input->post('id'));
    $this->db->order_by('team_name');
    $result = $this->db->get();

    return $result;
}

function get_referee() {        
      $this->db->from('referees');
      $this->db->order_by('id');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a referee';
        $return[$row['id']] = $row['first_name'].' '.$row['surname'];
            }
        }

    return $return;

}

}

View:

    <?php echo form_open('add_fixture/create') ;?>  

<label for="division" class="label">Division:</label><?php $js = 'id="division"'; echo form_dropdown('division', $division_list, set_value('division'), $js);  ?>

JavaScript:

    <script type="text/javascript">
      $(document).ready(function() {
        $('select#division').change(function() {
               var div = $(this).val();   
               if(div != '') {
                         $.post("/add_fixture/list_teams/",{division_id: div}, function(data){
                                  $("select#team_name").html(data);
                         });
               }
      });
      }); 
</script>

任何帮助将非常感激:)

I'm using CodeIgniter to develop a web application and wondered if anyone could help with a problem that has arose.

I have two dropdowns - Division and Teams. I'm creating a fixture so want to be able to allow the user to choose a division from the first dropdown, then within the second dropdown will only contain the teams from the selected division.

The Code I have so far:

Controller:

function create() { 
    $data['division_list'] = $this->add_fixture_model->get_divisions();
    $data['team_list'] = $this->add_fixture_model->get_teams();
    $data['referee_list'] = $this->add_fixture_model->get_referee();


    // field name, error message, validation rules
    $this->form_validation->set_rules('team_name', 'Team Name', 'trim|required');
    $this->form_validation->set_rules('home_team', 'Home Team Name', 'trim|required');
    $this->form_validation->set_rules('away_team', 'Away Team Name', 'required');
    $this->form_validation->set_rules('division_name', 'Division', 'trim|required');
    $this->form_validation->set_rules('referee', 'Referee', 'trim|required');
    $this->form_validation->set_rules('fixture_week', 'Fixture Week', 'trim|required');
    $this->form_validation->set_rules('fixture_day', 'Fixture Day', 'trim|required');
    $this->form_validation->set_rules('fixture_month', 'Fixture Month', 'trim|required');
    $this->form_validation->set_rules('fixture_year', 'Fixture Year', 'trim|required');
    $this->form_validation->set_rules('confirm_day', 'Fixture Confirm Day', 'trim|required');
    $this->form_validation->set_rules('confirm_month', 'Fixture Confirm Month', 'trim|required');
    $this->form_validation->set_rules('confirm_year', 'Fixture Confirm Year', 'trim|required');




    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('includes/top');
        $this->load->view('includes/header');
        $this->load->view('league/add_fixture', $data);
        $this->load->view('includes/footer');
    }

    else
    {           
        if($query = $this->add_fixture_model->add_fixture())
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/fixture_added');
            $this->load->view('includes/footer');

        }
        else
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/fixture_added', $data);
            $this->load->view('includes/footer');       
        }
    }

}

Model:

class Add_fixture_model extends Model {

function add_fixture()
{

    $add_new_fixture = array(
        'team_name' => $this->input->post('team_name'),
        'home_team' => $this->input->post('home_team'),
        'away_team' => $this->input->post('away_team'),
        'division' => $this->input->post('division_name'),
        'ground' => $this->input->post('ground'),
        'ground_postcode' => $this->input->post('ground_postcode'),
        'referee' => $this->input->post('referee'),
        'fixture_week' => $this->input->post('fixture_week'),
        'fixture_day' => $this->input->post('fixture_day'),
        'fixture_month' => $this->input->post('fixture_month'),
        'fixture_year' => $this->input->post('fixture_year'),
        'confirm_day' => $this->input->post('confirm_day'),
        'confirm_month' => $this->input->post('confirm_month'),
        'confirm_year' => $this->input->post('confirm_year')                            
    );


    $insert = $this->db->insert('fixtures', $add_new_fixture);


    return $insert;

}


function get_divisions()
{
      $this->db->from('divisions');
      $this->db->order_by('division_name');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a division';
        $return[$row['id']] = $row['division_name'];
            }
        }

    return $return;
}

function get_teams()
{
      $this->db->from('teams');
      $this->db->order_by('team_name');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a team';
        $return[$row['id']] = $row['team_name'];
            }
        }

    return $return;
}

function list_teams() {
    $this->db->from('teams');
    $this->db->where('division_id', $this->input->post('id'));
    $this->db->order_by('team_name');
    $result = $this->db->get();

    return $result;
}

function get_referee() {        
      $this->db->from('referees');
      $this->db->order_by('id');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a referee';
        $return[$row['id']] = $row['first_name'].' '.$row['surname'];
            }
        }

    return $return;

}

}

View:

    <?php echo form_open('add_fixture/create') ;?>  

<label for="division" class="label">Division:</label><?php $js = 'id="division"'; echo form_dropdown('division', $division_list, set_value('division'), $js);  ?>

JavaScript:

    <script type="text/javascript">
      $(document).ready(function() {
        $('select#division').change(function() {
               var div = $(this).val();   
               if(div != '') {
                         $.post("/add_fixture/list_teams/",{division_id: div}, function(data){
                                  $("select#team_name").html(data);
                         });
               }
      });
      }); 
</script>

Any help would very much be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文