Welcome Guest, Not a member yet? Register   Sign In
drop down dependen from one to many and many to many table.
#1

(This post was last modified: 07-23-2018, 09:14 AM by monperiabras.)

I am having a hard time figuring out how to make a make a drop down dependency(payment terms base on selected class name), when you select the name  class in the selectbox the select box generate a payment terms option base on the class select box base on the sql table (see last table). I think there is something wrong with my java script and CI model. I am very new in codeigniter activerecord, Please help me with my concern and Thank you very much in advance

CLASS TABLE
Code:
 classes
     |-------|------------------|
     |  id   |       Name       |
     |-------|------------------|
     |   1   |   Grade 1        |
     |-------|------------------|
     |   2   |   Grade 2        |
     |-------|------------------|

   
PAYMENT TERMS TABLE
Code:
payment_terms
     |--------|------------------------------------|
     |   id   |       payment_terms                |
     |--------|------------------------------------|
     |   1    |   Grade 1    Full Payment          |
     |--------|------------------------------------|
     |   2    |   Grade 1    Quarterly Payment     |
     |--------|------------------------------------|
     |   3    |   Grade 2    Full Payment          |
     |--------|------------------------------------|
     |   4    |   Grade 2    Quarterly Payment     |
     |--------|------------------------------------|


ACADEMIC YEAR TABLE  
PHP Code:
academic_year 
      
|--------|------------------------------------|
        id         academic_year                |
      |--------|------------------------------------|
        1            2018-2019                  |
      |--------|------------------------------------| 


ACADEMIC YEAR AND PAYMENT TERMS TABLE
Code:
academic_year_payment_terms
      |--------|--------------------------------|------------------|
      |   id   | academic_year_id               | Payment_term_id  |
      |--------|--------------------------------|------------------|
      |   1    | 1                              | 1                |
      |--------|------------------ -------------|------------------|
      |   2    | 1                              | 2                |
      |--------|--------------------------------|------------------|
      |   3    | 1            .                 | 3                |
      |--------|--------------------------------|------------------|
      |   4    | 1                              | 4                |
      |--------|--------------------------------|------------------|
 

ACADEMIC YEAR PAYMENT TERMS AND CLASS TABLE
Code:
academic_year_payment_terms_class
     |--------|-----------------------------|------------------|
     |   id   | academic_year_payment_terms | class_id         |
     |--------|-----------------------------|------------------|
     |   1    | 1                           | 1                |
     |--------|-----------------------------|------------------|
     |   2    | 2                           | 1                |
     |--------|-----------------------------|------------------|
     |   3    | 3                           | 2                |
     |--------|-----------------------------|------------------|
     |   4    | 4                           | 2                |
     |--------|-----------------------------|------------------|

 

My Codeigniter View
PHP Code:
<div class="form-group">
 
     <select  id="class_id" name="class_id" class="form-control" >
 
        <option value=""><?php echo $this->lang->line('select'); ?></option>
             <?php
                foreach 
($classlist as $class) { ?>
               <option value="<?php echo $class['id'?>"<?php
                   
if (set_value('class_id') == $class['id']){
 
                       echo "selected =selected";
 
                  }
 
                  ?>><?php echo $class['class'?></option>
                <?php
                   $count
++;
 
               }
 
               ?>
      </select>
    </div>
    <div class="form-group">
      <select class="form-control" id="academic_year_payment_terms_id" name="academic_year_payment_terms_id">
      </select>
    </div> 

My JavaScript
Code:
<script type="text/javascript">
   function getAcademicPaymentTermsByClass(class_id, academic_year_payment_terms_id {
       if (class_id != "" && academic_year_payment_terms_id != "") {
           $('#academic_year_payment_terms_id').html("");
           var base_url = '<?php echo base_url() ?>';
           var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
           $.ajax({
               type: "GET",
               url: base_url + "enrollment/fetchPaymentTermsByClass",
               data: {'class_id': class_id},
               dataType: "json",
               success: function (data) {
                   $.each(data, function (i, obj)
                   {
                       var sel = "";
                       if (academic_year_payment_terms_id == obj.academic_year_payment_terms_id) {
                           sel = "selected";
                       }

                       div_data += "<option value=" + obj.academic_year_payment_terms_id+ " " + sel + ">" + obj.payment_terms + "</option>";
                   });
                   $('#academic_year_payment_terms_id').append(div_data);
               }
           });
       }
   }

   $(document).ready(function () {
       var class_id = $('#class_id').val();
       var academic_year_payment_terms_id = '<?php echo set_value('academic_year_payment_terms_id') ?>';
       getAcademicPaymentTermsByClass(class_id, academic_year_payment_terms_id);

       $(document).on('change', '#class_id', function (e) {
           $('#academic_year_payment_terms_id').html("");
           var class_id = $(this).val();
           var base_url = '<?php echo base_url() ?>';
           var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
           $.ajax({
               type: "GET",
               url: base_url + "enrollment/fetchPaymentTermsByClass",
               data: {'class_id': class_id},
               dataType: "json",
               success: function (data) {
                   $.each(data, function (i, obj)
                   {

                       div_data += "<option value=" + obj.academic_year_payment_terms_id' + ">" + obj.payment_terms' + "</option>";
                   });
                   $('#academic_year_payment_terms_id').append(div_data);
               }
           });
       });
   });
</script>

My Controller
PHP Code:
function fetchPaymentTermsByClass() {
 
       $class_id $this->input->get('class_id');
 
       $data $this->Enrollment_model->getPaymentTermsByClass($class_id);
 
       echo json_encode($data);
 
      

My model
PHP Code:
      public function getPaymentTermsByClass($classid){
 
       $this->db->select('academic_year_payment_terms_class.id, academic_year_payment_terms_class.academic_year_payment_terms_id, academic_year_payment_terms.payment_terms');
 
       $this->db->from('academic_year_payment_terms_class');
 
       $this->db->join('academic_year_payment_terms''academic_year_payment_terms.id = academic_year_payment_terms_class.academic_year_payment_terms_id');
 
       $this->db->where('academic_year_payment_terms_class.class_id'$classid);
 
       $this->db->order_by('academic_year_payment_terms_class.id');
 
       $query $this->db->get();
 
       return $query->result_array();
 
       
Reply
#2

The JS part in your original post is cut off, and you never posted your model code (unless it came after JS). You can use php and code tags in Preview post to better format your code for others to read and make sure things don't get cut off.
Reply
#3

(07-23-2018, 06:54 AM)Pertti Wrote: The JS part in your original post is cut off, and you never posted your model code (unless it came after JS). You can use php and code tags in Preview post to better format your code for others to read and make sure things don't get cut off.

Thank you very much for notifying me about the missing codes i have updated my post and I include the javascript, controller and model. hope you can help me with my concern. Than you very much in advance.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB