Member 13754270 Ответов: 0

Как передать переменную в модель несколько раз с помощью одного ajax


В настоящее время я затрудняюсь найти решение этой проблемы. Итак, для начала я показываю детали работы проекта, и я просто не могу понять, почему он продолжает извлекать все данные, в которых есть исключения по запросу. Проблема с этим, вероятно, заключалась в том, что я не мог передать ProjectID в модель, и поэтому он не распознает уникальный ключ и не отображает детали в указанном проекте. Нужна помощь, пожалуйста. Очень ценю. :) Кстати, я использую кодовый воспламенитель. Я вставляю свой код ниже, чтобы вы могли это узнать! Спасибо!

===================================================================================
ВИД ПРОЕКТА

<?php 
include ('fragments/profile_header.php');
include('fragments/profile_nav.php');
?>

<div class="content-wrapper">
 
  <section class="content">
          <!-- table -->

          <div class="box box-primary" id="projectman">
              <div class="box-header">
                  <h2 class="box-title">Poject Management</h2>   

              <div class="box-tools pull-right">
                  <a href="AddProject"><button type="button" class="btn btn-primary"> ADD PROJECT</button></a>
              </div>                    
              <hr class="divider">
              </div>
              <div class="box-body">
                <table id="table2" class="table table-bordered table-striped dataTable" role="grid" aria-describedby="example1_info">
              <thead>
                <tr role="row"><th class="sorting_asc" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending" style="width: 120px;">Project No</th><th class="sorting_asc" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending" style="width: 120px;">Project Name</th>
                  <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-label="Browser: activate to sort column ascending" style="width: 120px;">Location</th><th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending" style="width: 120px;">Contractor</th>
                  <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending" style="width: 120px;">Contractor Cost</th><th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending" style="width: 120px;">Date Started</th>
                  <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending" style="width: 120px;">Contract Period</th>
                   <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending" style="width: 120px;">Conforme</th>
                  <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending" style="width: 100px;">Action</th></tr>
                </thead>
                <tbody>
                <?php
                  $project = $this->pmmm->get_project();

                  foreach ($project as $project->) {
                      echo'<tr>
                      <td style="text-transform: capitalize;font-size:14px">'.$project->ProjectNo.'</td>
                  <td style="text-transform: capitalize;font-size:14px">'.$project->ProjectName.'</td>
                  <td style="text-transform: capitalize;font-size:14px">'.$project->Location.'</td>
                  <td style="font-size:14px">'.$project->Contractor .'</td>
                  <td style="font-size:14px">'.$project->ContractorCost .'</td>
                  <td style="font-size:14px">'.$project->DateStarted .'</td>
                  <td style="font-size:14px">'.$project->ContractPeriod .' C.D.</td>
                  <td style="font-size:14px">'.$project->Conforme .'</td>
                      <td style="text-align:center;">
                      <button class="btn btn-flat btn-primary btn-sm" title="EDIT FRAME" onclick="edit_project('."'".$project->ProjectID."'".')"> Edit
                      </button>
                      <button class="btn btn-flat btn-warning btn-sm" title="ADD PROJECT WORK" onclick="view('."'".$project->ProjectID."'".')"> Add Project Work
                      </button>
                  </td>
                  </tr>';
                  }

                  ?>

          </tbody>
          </table>  
              </div><!-- end box body -->
              </div>

              <div class="box-footer hidden" id="projwork">
              <h3 class="header-title"> Project: <label name="projectname"></label></h3>
        
                       <input type="hidden" name="proj_id">
        <div class="form-group">
        <!-- table -->
        <div class="col-md-7">
                <div class="box box-primary">
                <div class="box-header with-border">
                  <h3 class="box-title"> Project Work Details</h3>
                </div>
                <div class="box-body">
                    <table id="table" class="table table-bordered table-striped dataTable" role="grid" aria-describedby="example1_info">
                        <thead>
                        <tr>
                            <th>Project Program Work ID</th>
                            <th>Project Work Description</th>
                            <th>Contract Amount</th>
                           <!--  <th>Action    </th> -->
                        </tr>
                        </thead>
                        <tbody>
                       <?php
                  $pm_project = $this->pmmm->get_project_programwork2();

                  foreach ($pm_project as $pm_project) {
                      echo'<tr>
                      <td style="text-transform: capitalize;font-size:14px">'.$pm_project->Project_ProgramWorkID.'</td>
                  <td style="text-transform: capitalize;font-size:14px">'.$pm_project->ProgramWorkDescription.'</td>
                   <td style="text-transform: capitalize;font-size:14px">'.$pm_project->ContractAmount.'</td>
                  </tr>';
                  }

                  ?>
                        </tbody>
                      </table>
                        
                </div>
                </div>
        </div>
        </div>

             
        <!-- add project work -->
        <div class="col-md-5">
              <div class="box box-primary">
                <div class="box-header with-border">
                  <h3 class="box-title"> Add Project Work</h3>
                </div>
                <div class="box-body">
                       <?php echo form_open_multipart('ProjectManagement/add_work');?>
                       <div class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">Project Work Description</label>
                            <!-- from databese tbl_programwork -->
                            <div class="col-sm-8">
                                <select type="text" class="form-control" name="ProgramWork" required>
                               <?php
                          $work = $this->pmmm->get_progwork();
                          foreach ($work as $work) {
                             echo '<option value='.$work->ProgramWorkID.'>'.$work->ProgramWorkDescription.'</option>';
                          }
                        ?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">Contract Amount</label>
                            <div class="col-sm-5">
                                    <input type="number" class="form-control" name="ContractAmount" placeholder="₱ 0, 000, 000" required>
                            </div>
                        </div>
                        </div>

                        <div class="box-footer">
                    <!-- will be save in tbl_project_programwork -->
                       <button type="submit" class="btn btn-primary pull-right">SAVE PROJECT WORK</button> 
                        </div></div>
                       </form>
              </div>
        </div>
    </div>  
            
         </div></div></section> </div><!-- end box -->
  </section>
</div><!-- end content wrapper -->


<?php 
include('fragments/profile_footer.php');
        // include('fragments/profile_scipt.php');
?>

           <!--  EDIT PROJECT MODAL-->

<!-- <div class="modal modal-default" id="edit_form" role="dialog">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
              <h3 class="modal-title">Update Project Details</h3>
            </div>
            <div class="modal-body form">
             <form action="#" id="editForm" class="form-horizontal">
            <input type="hidden" value="" name="employee_id" id="employee_id" /> 
               <div class="form-group">
            <label class="control-label col-md-3">Project No.</label>
            <div class="col-md-9">
              <input name="proj_no"  class="form-control" type="text" >
              <span class="help-block"></span>
            </div>
          </div>
           <div class="form-group">
            <label class="control-label col-md-3">Project Supervisor</label>
            <div class="col-md-9">
             <select type="text" class="form-control" name="proj_sup">
                               <?php
                          $emp = $this->pmmm->get_employee();
                          foreach ($emp as $emp) {
                             echo '<option value='.$emp->WorkerID.'>'.$emp->Name.'</option>';
                          }
                        ?>
                                </select>
            </div>
          </div>
           <div class="form-group">
            <label class="control-label col-md-3">Project Name</label>
            <div class="col-md-9">
              <input name="proj_name"  class="form-control" type="text" >
              <span class="help-block"></span>
            </div>
          </div>
           <div class="form-group">
            <label class="control-label col-md-3">Location</label>
            <div class="col-md-9">
              <input name="loc"  class="form-control" type="text" >
              <span class="help-block"></span>
            </div>
          </div>
           <div class="form-group">
            <label class="control-label col-md-3">Contractor Cost</label>
            <div class="col-md-9">
              <input name="cont_cost"  class="form-control" type="text" >
              <span class="help-block"></span>
            </div>
          </div>
           <div class="form-group">
            <label class="control-label col-md-3">Date Startedr</label>
            <div class="col-md-9">
              <input name="date_start"  class="form-control" type="text" >
              <span class="help-block"></span>
            </div>
          </div>
           <div class="form-group">
            <label class="control-label col-md-3">Contract Period</label>
            <div class="col-md-9">
              <input name="cont_per"  class="form-control" type="text" >
              <span class="help-block"></span>
            </div>
          </div>
           <div class="form-group">
            <label class="control-label col-md-3">Conforme</label>
            <div class="col-md-9">
              <input name="confor"  class="form-control" type="text" >
              <span class="help-block"></span>
            </div>
          </div>
        </form>
            </div>
            <div class="modal-footer"> 
              <button type="button" id="btnUpdate" onclick="save()" class="btn btn-primary">Update</button>
              <button type="button" class="btn btn-danger btn-flat pull-left" data-dismiss="modal">Cancel</button>
            </div>
          </div>
        </div>
      </div>
 -->
<script type="text/javascript">
    $(document).ready(function() {
        $('#table2').DataTable({
          "paging": true,
          "lengthChange": true,
          "searching": true,
          "ordering": true,
          "info": true,
          "autoWidth": false,
          pageLength:10
        });
       }); 
    $(document).ready(function(){
      $('#table').DataTable();
});

    function view(project_id){
     

      $.ajax({
          url: "<?php echo site_url('ProjectManagement/ajax_view_program/')?>" + project_id,
      type: "GET",
      dataType: "JSON",
      success: function(data){
        console.log(data);
        $('[name="projectname"]').text(data[0].ProjectName);
        $('[name="ProgramWork"]').text(data[0].ProgramWorkID);
        $('[name="proj_id"]').val(data[0].ProjectID);
        $('#projwork').removeClass("hidden");
        $('#projectman').addClass("hidden");
   
      },
       error: function(jqXHR, textStatus, errorThrown)
      {
        alert('Error retreiving data');
      }
      });
    }

      //-----EDIT PROJECT FUNCTIONS//

 // function edit_project(ProjectID){
        
 //       $('#editForm')[0].reset();
 //        $.ajax({
 //          url: "<?php echo site_url('ProjectManagement/ajax_view_project/')?>" + ProjectID,
 //          type:"GET",
 //          dataType: "JSON",
 //          success: function(data)
 //          {
 //            console.log(data);
            

 //            $('[name="proj_no"]').val(data[0].ProjectNo);
 //            $('[name="proj_sup"]').val(data[0].WorkerID);
 //            $('[name="proj_name"]').val(data[0].ProjectName);
 //            $('[name="loc"]').val(data[0].Location);
 //            $('[name="cont_cost"]').val(data[0].ContractorCost);
 //            $('[name="date_start"]').val(data[0].DateStarted);
 //            $('[name="cont_per"]').val(data[0].ContractPeriod);
 //            $('[name="confor"]').val(data[0].Conforme);
 //            $('[name="pro"]').val(data[0].ProjectID);
 //            $('#edit_form').modal('show');
 //            $('.modal-title').text('Edit Project Details');
        



 //          },
 //          error: function(jqXHR, textStatus, errorThrown)
 //          {
 //            alert('Error retrieving data');
 //          }
 //        });
 //       } 

 //    function save(){
 //      url = "<?php echo site_url('ProjectManagement/update')?>"
 //      form = '#editForm';

 //    $.ajax({
 //      url : url,
 //      type: "POST",
 //      data: $(form).serialize(),
 //      dataType: "JSON",
 //      success: function(data)
 //      {     

 //          $('#edit_form').modal('hide');
 //          alert('Project Details Updated!');
 //          location.reload();                       
 //      },
 //      error: function (jqXHR, textStatus, errorThrown)
 //      {
        // console.log(jqXHR);
      //   alert("Error getting data!");
  //     // }
  //   });
  // }
</script>


==================================================================================
КОНТРОЛЛЕР ПРОЕКТА
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ProjectManagement extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('ProjMan_management_model','pmmm');
 }

 function index()
 {
 		if($this->session->userdata('logged_in')){
 		 $session_data = $this->session->userdata('logged_in');
    $data['userAccID'] = $this->session->userdata('logged_in')['userAccID'];
    $data['name'] = $this->session->userdata('logged_in')['name'];
    $data['access'] = $this->session->userdata('logged_in')['access'];
    $data['project'] = $this->pmmm->get_project();
     $data['emp'] = $this->pmmm->get_employee();
     $data['work'] = $this->pmmm->get_progwork();

     /*$data['pm_project'] = $this->pmmm->get_project_programwork2();*/
   $this->load->helper(array('form'));
   $this->load->view('projman_view');
 	}else
   {
     //If no session, redirect to login page
     redirect('home_view', 'refresh');
   }

 }
 

     public function ajax_view_program($project_id)
    {
    $data = $this->pmmm->get_by_project_id($project_id);
    echo json_encode($data);
    }

    public function ajax_view_project($project_id)
    {
    $data = $this->pmmm->get_by_project_id($project_id);
    echo json_encode($data);
    }

    public function update() 
 { 
    $data = array(
     'ProjectNo' => $this->input->post('proj_no'),
     'WorkerID' => $this->input->post('proj_sup'),
     'ProjectName' => $this->input->post('proj_name'),
     'Location' => $this->input->post('loc'),
     'ContractorCost' => $this->input->post('cont_cost'),
     'DateStarted' => $this->input->post('date_start'),
     'ContractPeriod' => $this->input->post('cont_per'),
     'Conforme' => $this->input->post('confor')) ;
         $this->pmmm->update_project(array('ProjectID' => $this->input->post('project_id')), $data);
         echo json_encode(array("status" => TRUE));
          
 }

 public function add_work(){
     
    $data = array(
    'ContractAmount' => $this->input->post('ContractAmount'),
    'ProjectID' => $this->input->post('proj_id'),
    'ProgramWorkID' => $this->input->post('ProgramWork')
    );

    $this->pmmm->add_work($data);
    redirect('ProjectManagement', 'refresh');
 }
}

?>

=============================================================================
МОДЕЛЬ ПРОЕКТА
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class ProjMan_management_model extends CI_Model {
	public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_progwork(){
        $this->db->select('*');
        $this->db->from('tbl_programwork');
        $query = $this->db->get();
        return $query->result();
    }

     public function get_project(){
        $this->db->from('tbl_projects p');
        $this->db->join('tbl_employee e', 'e.WorkerID = p.WorkerID', 'left');
        $query = $this->db->get();
        return $query->result();
    }

    public function get_by_projectid($project_id){
        $this->db->select('*');
        $this->db->from('tbl_projects p');
        $this->db->where('p.ProjectID', $project_id);
        $query = $this->db->get();
        return $query->result();
   }
     public function get_employee(){
        $this->db->from('tbl_employee');
        $query = $this->db->get();
        return $query->result();
    }

    public function update_project($where, $data)
   {
        $this->db->update('tbl_projects', $data, $where);
        return $this->db->affected_rows();
   }

    public function add_work($data){
        $this->db->insert('tbl_project_programwork',$data);
        return $this->db->insert_id();
    }

    public function get_by_project_id($project_id){
         $this->db->select('*');
        $this->db->from('tbl_projects p');
        $this->db->join('tbl_project_programwork pp', 'p.ProjectID = pp.ProjectID', 'left');
        $this->db->join('tbl_programwork pw', 'pw.ProgramWorkID = pp.ProgramWorkID', 'left');
        $this->db->where('p.ProjectID', $project_id);
        $query = $this->db->get();
        return $query->result();
    }

     public function get_project_programwork2(){
        $this->db->from('tbl_project_programwork pp');
        $this->db->join('tbl_projects p', 'p.ProjectID = pp.ProjectID', 'left');
        $this->db->join('tbl_programwork pw', 'pw.ProgramWorkID = pp.ProgramWorkID', 'left');
        // $this->db->where('pp.ProjectID = p.ProjectID');
        $this->db->where('pp.ProgramWorkID = pw.ProgramWorkID');
        $query = $this->db->get();
        return $query->result();
    }

}


Что я уже пробовал:

Я попытался передать его другой переменной в контроллер и сделал его частным, но это не решает проблему. Здесь очень нужна помощь. Пожалуйста. Спасибо!

0 Ответов