每天在php和codeigniter中使用Xampp执行此函数

I want to execute this function everyday. I am using Xampp for development and testing with CodeIgniter as a framework which uses php and mysql.

this code works fine to populate the database table with the current day but i want to do it automatically with an event not a trigger

people told me to use CRON but i don't know how to create the files and where to put them since i am using windows for hosting

function create_users_records(){
    //get the user's IDs and number 
    $this->db->select('id');
    $query = $this->db->get('users');

     $number_of_employees = $query->num_rows();
        echo "number of employees is: ".$number_of_employees."<br>";

          for ($i = 0; $i < $number_of_employees; $i++) {
                 $row = $query->row($i);
                 echo $row->id ."<br>"; 

                  //date and time now
                 $time = time();

                  //date format
                 $datestring = "%Y-%m-%d";

                  $data = array(
                'Attendence_date_daily' => mdate($datestring, $time),
                'Check_in_time' => null,
                'Check_out_time' => null,
                'Attendence_status' => null,
                'Employee_comment' =>null,
                'Deducted_today' => 0,
                'user_id' => $row->id
                );

                  $this->db->insert('daily_attendence_record', $data); 
         }

}      

I am creating an attendance system and I am required to create the daily records for all users at midnight so they can check in/out before that time and to record their vacations and absences.

If you're using Windows for hosting you could set a scheduled task on the server rather than using CRON.

Put your above function into a controller somewhere and make it so that you can invoke it via a URL. You can use routing.php in config to map something like http://example.com/update to the function.

Try out the solution outlined here, which should do what you need to do to schedule the task.

whit this page: http://www.adminschoice.com/crontab-quick-reference

you can create a cron script for everyday execution.

example:

30     18     *     *     *  php /home/someuser/tmp/yourscript.php

I solved this problem of executing a function on a scheduled time by the following: first of all:

  1. Create a normal controller in Codeigniter and put the code and functions your want to execute .

  2. call the functions in the index method of the controller so it can be executed automatically when the php file is parsed and called. now you have an executable php file that when run it will do what you want but only once and when call manually.

the second step is to run this code from the cmd:

  1. Create (.bat file) anywhere you want and name (not necessary to be in the project folder , you can put it in the desktop or documents so u can click it and test it later), name is not important too.

open the .bat file and type in it the following code
cd c:\xampp\htdocs ame_of_your_project\ <-this is the path to where the controller is.

php function_name.php name_of_the_controller
PAUSE
'pause' is to show the cmd and not close it automatically, useful if you want to check error massages which you should handle and echo properly.

ex:

cd c:\xampp\htdocs\Myproject\
php index.php cron
PAUSE

now, you have a file that when clicked will execute the function. just use schedule tasks in windows to execute it whenever you need.

'index' is the name of the function you want to execute and 'cron' you can ask me here and i will try to help as much as i can.
Good luck with your projects