Rajan
Rajan

Reputation: 2425

how to pass the date dynamically for get the data for particular date?

hi i am trying get records for particular date.

I have logins and logouts of employee for every day. Now i want to show only his first login and his last logout.

So in my model i want to pass where condition where i want to pass a date selected from date picker in my view. and want to show data of that date only.

So this is my controller:

 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);
    }

This is the model function :

   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;



 }

The View:

<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>

Upvotes: 1

Views: 1764

Answers (1)

gabe3886
gabe3886

Reputation: 4265

If you look at the CodeIgniter tutorial, this shows how to get information based on data passed through. In the example, it's from the URL.

There's an example on the tutorial of creating news of how the model can use the POST information with the database, so you'd have something like:

// 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...

EDIT - following a request to edit

Based on what I have said above, and in the comments, your view will have the following part:

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

Your model will then have :

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

And on further inspection of your code, you're currently getting null passed through for this because the date_data field is outside of the <form></form> in your view, so it's not part of the form to be submitted.

It also looks like you're trying to reload the grid based on AJAX, which will mean your JavaScript/jQuery will need changing so it passes the date field through as part of the params.

Upvotes: 1

Related Questions