使用CodeIgniter基于另一个查询运行查询

时间:2016-10-27 08:28:11

标签: php mysql codeigniter

我将数据库中的信息填充到主页上的下拉列表中。我有一个名为“banks”的表,其各自的列名为“id”和“bankname”。我还有另一个名为“bankbranches”的表,其中各列名称为“id”,“bankbranch”和“bank”。银行分行中的“银行”列具有与“表格”相同的“id”。我想在我的模型中运行一个查询,根据之前选择的银行下拉列表来弹出银行分行。到目前为止,在我的模型中我有:

        function getBank()
        {
            $query = $this->db->query('SELECT id,bankname FROM banks');
            return $query->result_array();
        }
        function getBankBranch()
        {
            $query = $this->db->query('SELECT id,bankbranch FROM bankbranches');
            return $query->result_array();

但银行分行下拉列出了我数据库中的所有银行分行。我希望根据用户选择的银行下拉来填充银行分行的下拉列表。以下是我的观点:

        <label>Bank</label>
                <select name="banks" id="banks">
                <option value="">--Please select--</option>
                <?php
                foreach($banks as $bank)
                {
                echo '<option value="'.$bank['id'].'">'.$bank['bankname'].'</option>';

                }
                ?>  
        </select> <br/>

        <label>Bank Branch</label>
                <select name="bankbranches" id="bankbranches">
                <option value=""></option>
                <?php
                foreach($bankbranch as $branch)
                {
                    echo '<option value="'.$branch['id'].'">'.$branch['bankbranch'].'</option>';
                }
                ?>  
        </select> <br/>

这是我的控制器:

        public function index()
        { 
          $this->load->helper('form');
          $data['cities'] = $this->site_model->getCity();
          $data['modes'] = $this->site_model->getPaymentMode();
          $data['banks'] = $this->site_model->getBank();
          $data['bankbranch'] = $this->site_model->getBankBranch();
          $data['categories'] = $this->site_model->getCategories();
          $data['staffs'] = $this->site_model->getStaff();

          $this->load->view('supplier_add',$data); 
        }

我真的很想详细说明。

2 个答案:

答案 0 :(得分:1)

在你的模型中,在getBankBranch函数中添加一个参数,参数名称为$bank_id,然后在sql查询中添加一个条件。

在模特:

<?php
function getBankBranch($bank_id){
    $query = $this->db->query("SELECT id,bankbranch FROM bankbranches where bank = '$bank_id'");
    return $query->result_array();
}
?>

在控制器BankBranch中添加新方法。此方法明智地检索分支名称bank_id

在控制器中:

<?php
public function BankBranch() { 
    $this->load->helper('form');
    if ($this->input->is_ajax_request()) {
        $bank_id = $this->input->post('bank_id');
        $bankbranch = $this->site_model->getBankBranch($bank_id);

        echo '<option value=""></option>';
        foreach($bankbranch as $branch)
        {
            echo '<option value="'.$branch['id'].'">'.$branch['bankbranch'].'</option>';
        }
    }
}
?>

查看页面:

<label>Bank</label>
        <select name="banks" id="banks">
        <option value="">--Please select--</option>
        <?php
        foreach($banks as $bank)
        {
        echo '<option value="'.$bank['id'].'">'.$bank['bankname'].'</option>';

        }
        ?>  
</select> <br/>

<label>Bank Branch</label>
<select name="bankbranches" id="bankbranches">

</select>

最后,您可以使用jquery,并使用BankBranch()从您的控制器调用AJAX Request方法。

在剧本中:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script>
jQuery(document).ready(function ($) {
    $('#banks').on('change', function (e) {
        e.preventDefault();
        var bank_id = $(this).val();

        var base_url = "<?php base_url(); ?>";

        $.ajax({
            url: base_url+"your_controller/BankBranch", // Url to which the request is send
            type: "POST", // Type of request to be send, called as method
            data: {bank_id:bank_id}, // Data sent to server, a set of key/value pairs representing form fields and values
        }).done(function (data) {
            $("#bankbranches").html(data);            
        }).fail(function (data) {
            console.log('failed');
        });
    });
}(jQuery));
</script>

答案 1 :(得分:1)

在选择银行时,您可以使用jquery和ajax来获取分支。

在您看来: -

<label>Bank</label>
<select name="banks" id="banks">
    <option value="">--Please select--</option>
    <?php foreach($banks as $bank) {
        echo '<option value="'.$bank['id'].'">'.$bank['bankname'].'</option>';
    } ?>  
</select><br/>

<label>Bank Branch</label>
<select name="bankbranches" id="bankbranches">
    <option value=""></option>
</select> <br/>

<script type= text/javascript>
    $('#banks').on('change', function() {
        var bank = $(this).val();
        $.ajax({
            type: 'post',
            url: '<?php echo base_url('controller/get_branches'); ?>, //set your controller name here
            data: { bank_id : bank },
            success: function (response) {
                 $('#bankbranches').html(response); 
            }
        });
    });
</script>

在您的控制器中: -

public function index()
{ 
    $this->load->helper('form');
    $data['cities'] = $this->site_model->getCity();
    $data['modes'] = $this->site_model->getPaymentMode();
    $data['banks'] = $this->site_model->getBank();
    $data['categories'] = $this->site_model->getCategories();
    $data['staffs'] = $this->site_model->getStaff();

    $this->load->view('supplier_add',$data); 
}

public function get_branches()
{ 
     $bank_id = $this->input->post('bank_id');
     $bankbranch = $this->site_model->getBankBranch($bank_id);
     foreach($bankbranch as $branch) {
         echo '<option value="'.$branch['id'].'">'.$branch['bankbranch'].'</option>';
     }
}

在你的模特中

function getBankBranch($bank_id)
{
     $query = $this->db->query('SELECT id,bankbranch FROM bankbranches WHERE bank='.$bank_id);
     return $query->result_array();
}