根据首选

时间:2015-05-21 06:44:10

标签: javascript php jquery html codeigniter

我是第一年的学生,我需要一名助手使用以下代码,我被赋予了使用框架创建会员系统的任务,我选择使用codeigniter 我在codeigniter中创建下拉列表时遇到了困难。信息正在加载成员数据库。我只需要一个如何实现这个人口下拉列表的方向

DATABASE

            CREATE TABLE `member` (
             `id` tinyint(4) NOT NULL auto_increment,
             `membership` tinyint(4) NOT NULL,
             `region` tinyint(4) NOT NULL,
             `country` tinyint(4) NOT NULL,
             `state` varchar(40) NOT NULL,
             `city` varchar(40) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

            CREATE TABLE `region` (
              `id` tinyint(4) NOT NULL auto_increment,
              `region` varchar(20) NOT NULL default '',
            PRIMARY KEY  (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

            --
            -- Dumping data for table `region`
            --

            INSERT INTO `region` (`id`, `region`) VALUES
            (1, 'South Africa'),
            (2, 'Botswana'),
            (3, 'Namibia'),
            (4, 'Swaziland'),
            (5, 'International');

            CREATE TABLE `country` (
             `id` tinyint(4) NOT NULL auto_increment,
             `regionid` tinyint(4) NOT NULL,
             `country` varchar(40) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

            INSERT INTO `country` (`id`, `regionid`, `country`) VALUES
            (1, 1, 'Eastern Cape'),
            (2, 1, 'Free State'),
            (3, 2, 'Gaborone'),
            (4, 2, 'Lobatsi'),
            (5, 3, 'Windhoek'),
            (6, 4, 'Swaziland'),
            (7, 5, 'International');

            CREATE TABLE `state` (
             `id` tinyint(4) NOT NULL auto_increment,
             `regionid` tinyint(4) NOT NULL,
             `countryid` tinyint(4) NOT NULL,
             `state` varchar(40) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

            INSERT INTO `state` (`id`, `regionid`, `countryid`, `state`) VALUES
            (1, 1, 1, 'Bloemfointein'),
            (2, 1, 1, 'Bethehem');


            CREATE TABLE `city` (
             `id` tinyint(4) NOT NULL auto_increment,
             `regionid` tinyint(4) NOT NULL,
             `countryid` tinyint(4) NOT NULL,
             `stateid` tinyint(4) NOT NULL,
             `city` varchar(40) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

JAVASCRIPT

            <script language="JavaScript">

            function autoSubmit()
            {
                var formObject = document.forms['theForm'];
                formObject.submit();
            }

            </script>       

CONTROLLER

        function member($param1 = '', $param2 = '', $param3 = '')
            {
                if ($this->session->userdata('admin_login') != 1)
                    redirect('login', 'refresh');
                if ($param1 == 'create') {
                    $data['membership_no']          = $this->input->post('membership_no');
                    $data['region']                 = $this->input->post('region');
                    $data['country']                = $this->input->post('country');
                    $data['state']                  = $this->input->post('state');
                    $data['city']                   = $this->input->post('city');
                    $this->db->insert('member', $data);
                    $member_id = mysql_insert_id();
                    move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/member_image/' . $member_id . '.jpg');
                    $this->email_model->account_opening_email('member', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL
                    redirect(base_url() . 'index.php?admin/member_add/' . $data['class_id'], 'refresh');
                }

查看

            <?php

            $region = $country = $state = $city = null; //declare vars

            if(isset($_GET["region"]) && is_numeric($_GET["region"]))
            {
                $region = $_GET["region"];
            }

            if(isset($_GET["country"]) && is_numeric($_GET["country"]))
            {
                $country = $_GET["country"];
            }

            if(isset($_GET["state"]) && is_numeric($_GET["state"]))
            {
                $state = $_GET["state"];
            }

            if(isset($_GET["city"]) && is_numeric($_GET["city"]))
            {
                $city = $_GET["city"];
            }

            ?>  



        <?php echo form_open('admin/member/create/' , array('class' => 'form-horizontal form-groups-bordered validate', 'enctype' => 'multipart/form-data'));?> 

                <div class="form-group">
                    <label for="field-1" class="col-sm-3 control-label"><?php echo get_phrase('membership_no');?></label>

                    <div class="col-sm-5">
                        <input type="text" class="form-control" name="membership_no" data-validate="required" data-message-required="<?php echo get_phrase('value_required');?>" value="" autofocus>
                    </div>
                </div>

                <select name="region" onChange="autoSubmit();">
                    <option value="null"></option>
                    <option value="1" <?php if($region == 1) echo " selected"; ?>>East</option>
                    <option value="2" <?php if($region == 2) echo " selected"; ?>>West</option>
                </select>

                <br><br>



                <?php

                if($region != null && is_numeric($region))
                {

                ?>

                <select name="country" ">
                    <option value="null"></option>

                    <?php

                    //POPULATE DROP DOWN MENU WITH COUNTRIES FROM A GIVEN REGION

                    $sql = "SELECT COUN_ID, COUN_NAME FROM COUNTRY WHERE RE_ID = $region";
                    $countries = mysql_query($sql,$conn);

                    while($row = mysql_fetch_array($countries))
                    {
                        echo ("<option value=\"$row[COUN_ID]\" " . ($country == $row["COUN_ID"] ? " selected" : "") . ">$row[COUN_NAME]</option>");
                    }

                    ?>

                </select>

                <?php

                }

                ?>

                <br><br>

                <?php

                if($country != null && is_numeric($country) && $region != null)
                {

                ?>

                <select name="state" onChange="autoSubmit();">
                    <option value="null"></option>

                    <?php

                    //POPULATE DROP DOWN MENU WITH STATES FROM A GIVEN REGION, COUNTRY

                    $sql = "SELECT STAT_ID, STAT_NAME FROM states WHERE COUN_ID = $country ";
                    $states = mysql_query($sql,$conn);

                    while($row = mysql_fetch_array($states))
                    {
                        echo ("<option value=\"$row[STAT_ID]\" " . ($state == $row["STAT_ID"] ? " selected" : "") . ">$row[STAT_NAME]</option>");
                    }

                    ?>

                </select>

                <?php

                }

                ?>

                <br><br>

                <?php

                if($state != null && is_numeric($state) && $region != null && $country != null)
                {

                ?>

                <select name="city" onChange="autoSubmit();">
                    <option value="null"></option>

                    <?php

                    //POPULATE DROP DOWN MENU WITH CITIES FROM A GIVEN REGION, COUNTRY, STATE

                    $sql = "SELECT CIT_ID, CITY_NAME FROM CITY WHERE STAT_ID = $state ";
                    $cities = mysql_query($sql,$conn);

                    while($row = mysql_fetch_array($cities))
                    {
                        echo ("<option value=\"$row[CIT_ID]\" " . ($city == $row["CIT_ID"] ? " selected" : "") . ">$row[CITY_NAME]</option>");
                    }

                    ?>

                </select>

                <?php

                }

                ?>

1 个答案:

答案 0 :(得分:1)

你需要在开始编码之前先学习一下框架,在编写代码之前先了解MVC,我相信你对我的评论很生气但很遗憾地说有很多改进可用。

https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

$options = array(
              'small'  => 'Small Shirt',
              'med'    => 'Medium Shirt',
              'large'   => 'Large Shirt',
              'xlarge' => 'Extra Large Shirt',
            );

//pass this `$options` as a part of an array to view.
$data['options'] = $options
$this->load->view('sample_page', $data);

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

echo form_dropdown('shirts', $options, $shirts_on_sale);

// Would produce:

<select name="shirts" multiple="multiple">
<option value="small" selected="selected">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>