如何动态传递日期以获取特定日期的数据?

时间:2015-12-24 10:13:31

标签: php codeigniter

嗨,我正在尝试获取特定日期的记录。

我每天都有员工登录和注销。 现在我想只显示他的第一次登录和最后一次登出。

所以在我的模型中,我想要传递我希望在我的视图中传递从日期选择器中选择的日期的条件。并且只想显示该日期的数据。

所以这是我的控制器:

 public function index()
    {

   $this->data['attendances'] = $this->attendance_m->join_data();
   //var_dump($rajan);
    $this->data['attendance_dropdown'] = $this->attendance_m->get_emp_list();
    $this->data['subview'] = 'admin/attendance/index';
    $this->load->view('admin/_layout_main', $this->data);
    }

这是模型功能:

   public function join_data()
   {
  $this->db->select('*');
  $this->db->from('daily_data2');
  $this->db->where('date_data',"2015-11-04 14:07:06");
  $this->db->order_by("date_data","DESC");
  $this->db->LIMIT(1);
  $this->db->join('users', 'users.emp_id = daily_data2.emp_id');

  $query = $this->db->get();




  $res   = $query->result();        
    return $res;



 }

观点:

<script type="text/javascript" src="<?php echo site_url('js/bootstrap.min.js');?>"></script>
        <h2>Upload CSV To Import Users</h2>
        <!-- in the action you need to place /controller/function in our case @Attendance, @upload -->
        <form method="post" action="<?php echo site_url('admin/attendance/upload');?>" enctype="multipart/form-data">
        <input type="file" name="file" id="file">
        <br>
        <input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
        </form>


        <div class="row">
        <div class="col-lg-12">


        <div class="row">

        <div class="col-md-3">

        <div class="form-group">
        <h1><label for="sel1">Select list:</label></h1>
        <select name="attendance-list" id="attendance-list" class="form-control" >

        <?php foreach($attendance_dropdown as $value) { ?>
        <option value="<?php echo $value['emp_id'];?>"><?php echo $value['emp_name']; ?> </option>
        <?php } ?>


        </select>

        <p>Date: <input type="text" id="datepicker"></p>
        </div>

        <h1 class="page-header">Attendance Details</h1>

        </div>
        <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->



        <div class="row">
        <div class="col-lg-12">
        <div class="panel panel-primary">
        <div class="panel-heading">
        All Calls Records
        </div>
        <!-- /.panel-heading -->
        <div class="panel-body">
        <div class="dataTable_wrapper">
        <table class="table table-striped table-bordered table-hover" id="dataTables-example">


        <thead>
        <tr>
        <th>Emp_ID</th>
        <th>Name</th>
        <th>Date</th>
        <th>Entry</th>



        </tr>
        </thead>



        <tbody>

        <?php if(count($attendances)): foreach($attendances as $attendance): ?>


        <tr class="odd gradeX">
        <td><?php echo trim($attendance->emp_id,'">'); ?></td>
        <td><?php echo trim($attendance->name.$attendance->last_name,'">'); ?></td>
        <td><?php echo trim($attendance->date_data,'">'); ?></td>
        <td><?php trim($attendance->entry,'">'); ?> <?php if($attendance->entry >100)
        {
        echo "Logged In";
        }
        else
        {
        echo"Logged Out";
        }?>
        </td>


        </tr>

        <?php endforeach; ?>

        <?php else: ?>

        <tr>
        <td colspan="3">We could not find any Data.</td>
        </tr>

        <?php endif; ?>



        </tbody>
        </table>
        </div>
        <!-- /.table-responsive -->

        </div>
        <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->

        <!-- /.row -->







        <!-- jQuery -->
        <script src="../bower_components/jquery/dist/jquery.min.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="../bower_components/metisMenu/dist/metisMenu.min.js"></script>

        <!-- DataTables JavaScript -->
        <script src="../bower_components/datatables/media/js/jquery.dataTables.min.js"></script>
        <script src="../bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.min.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="../dist/js/sb-admin-2.js"></script>


         <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
          <script src="//code.jquery.com/jquery-1.10.2.js"></script>
          <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

        <!-- Page-Level Demo Scripts - Tables - Use for reference -->

        <script>
          $(function() {
            $( "#datepicker" ).datepicker();
          });
          </script>

        <script>
        $(document).ready(function() {
        $('#dataTables-example').DataTable({
        responsive: true
        });
        });
        </script>





        <script>
        site_url="http://127.0.01/project/admin/";
        $('#attendance-list').on('change', function () {
        var select_emp_id=$(this).val();
        if(select_emp_id!=""){
        var params={};
        params["id"]= select_emp_id;
        params["name"]= select_name;
        $.ajax({type: 'POST',dateType:'json', url: site_url + 'attendance/getUserData', data: params,success: function (response) {
        //write on response logic/set employee details
        var user_data=JSON.parse(response);
        }, async: true});
        }
        });
        </script>



        <script> 
        site_url = "http://127.0.01/project/admin/"; 
        $('#attendance-list').on('change', function() { 


        var select_emp_id=$(this).val(); 
        if(select_emp_id!=""){ 
        var params={}; 
        params["id"]= select_emp_id;


        $.ajax({type: 'POST', url: site_url + 'attendance/getUserInfo', data: params, success: function(response) { 


        //write on response logic/set employee details 

        console.log(response); 
        var user_data = JSON.parse(response); 
        console.log(user_data); 
        var row = '<tr class="odd gradeX"><td>' + user_data.emp_id + '</td><td>' + user_data.name + ' ' + user_data.last_name'</td><td>' + user_data.date_data + '</td><td>' + user_data.entry + ' ' + (user_data.entry > 100?"Logged In":"Logged Out") + '</td> </tr>'; 
        $("#dataTables-example tbody").find('tr').remove(); 
        $("#dataTables-example tbody").append(row); 
        }, async: true}); 
        } 
        }); 
        </script>

1 个答案:

答案 0 :(得分:1)

如果查看CodeIgniter tutorial,则会显示如何根据传递的数据获取信息。在示例中,它来自URL。

关于模型如何将POST信息与数据库一起使用的creating news教程有一个例子,所以你有类似的东西:

// in the model you can get the date
$filter_date = $this->input->post('date_field'); // change the 'date_field' to your actual field name in the POST

// other query information
$this->db->where('date_data', $filter_date);

// finish the query and continue...

编辑 - 遵循编辑请求

根据我上面所说的内容以及评论,您的观点将包含以下内容:

<p>Date: <input type="text" id="datepicker" name="date_data"></p>

您的模型将具有:

$filter_date = $this->input->post('date_data');

在进一步检查代码时,由于date_data字段位于视图中的<form></form>之外,因此您目前正在为此传递null,因此它不是要提交的表单的一部分。 / p>

看起来你也试图重新加载基于AJAX的网格,这意味着你的JavaScript / jQuery需要改变,所以它通过日期字段作为参数的一部分。