0

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.

alkhamis
  • 49
  • 3
  • 9
  • Typically you'd create a CRON via crontab on *nix builds, but I'm not sure how you'd do it on a Windows machine. One alternative is to use https://cron-job.org/en/ to ping a URL every day. – Jamie Bicknell Jul 07 '15 at 22:34

3 Answers3

0

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.

Community
  • 1
  • 1
Daniel Waghorn
  • 2,997
  • 2
  • 20
  • 33
  • i have created this function in a controller , and have url which when executed , will call the function and create the record in the database. but right now i am in the development stage . this url is local and not in the internet. so i can't user cron-job.org or something like that. – alkhamis Jul 07 '15 at 22:49
  • @alkhamis That still won't cause you a problem, just set the scheduled task to target the local URL you visit in your browser when you run it manually. – Daniel Waghorn Jul 07 '15 at 22:50
  • i will check the other solution. It looks hard for me since i don't use VB.net or powershell that much I can't thank you enough for your replays and advices . – alkhamis Jul 07 '15 at 22:52
  • @alkhamis No problem, if you follow that post you should be fine. The only bit you'll want to change is the URL it targets since the task used in the example is set to run daily. – Daniel Waghorn Jul 07 '15 at 22:55
  • your answer guided me to a good solution. i will post it soon and i will reference you in the answer – alkhamis Jul 10 '15 at 10:34
0

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
  • thanks for your answer. this is great if i am using Unix-like systems for hosting. which what the system will be run on, but i am using Xampp and windows for development. i found a good solution for my problem. i will post it soon – alkhamis Jul 10 '15 at 10:28
0

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

alkhamis
  • 49
  • 3
  • 9