Hi,

I am working on a code that dispatches airtime depending on a schedule that is specified. Now all works just fine. The only problem is that the code executes only once.
To throw more light, here is how it is meant to work;
1. An administrator creates a schedule, specifying how frequently they want the airtime to be dispatched [working ok]
2. The cron runs on a daily basis to find which airtime to send

Currently, I have a field called airtime status, whose default value is 0. But after sending the airtime, then the statusis updated to 1.

Now the real challenge is that; if this status is set to 1, it means that the script will not send that airtime again; yet it is supposed to be sent daily.

Any suggestions as to how I can go about this?

Member Avatar for LastMitch

To throw more light, here is how it is meant to work;
1. An administrator creates a schedule, specifying how frequently they want the airtime to be dispatched [working ok]
2. The cron runs on a daily basis to find which airtime to send

You can take a look at this code here:

http://stackoverflow.com/questions/321494/calculate-when-a-cron-job-will-be-executed-then-next-time/3453872#3453872

I am assume you are using this:

http://mtdowling.com/blog/2012/06/03/cron-expressions-in-php/

This is the code/file which you can download it here:

https://github.com/mtdowling/cron-expression

Currently, I have a field called airtime status, whose default value is 0. But after sending the airtime, then the statusis updated to 1.
Now the real challenge is that; if this status is set to 1, it means that the script will not send that airtime again; yet it is supposed to be sent daily.

You can test it here to see the schedule/status:

http://www.openjs.com/scripts/jslibrary/demos/crontab.php

Thanx. Gon try that out right away ...

@LastMitch, I went through the links that you shared, however; the only difference is that with my code, I am using a php file, which will be called upon by the cron.

Now, I want the code to execute on a daily basis, for a given range of time. Perhaps, it may help if I shared my code.

<?php

$get_schedule_query = "SELECT * FROM schedule_airtime_submission WHERE schedule_frequency='Daily' AND airtime_status=0";
    $execute_get_scheduled_airtime = mysql_query($get_schedule_query,$connection) or handleDatabaseError(''. mysql_error(), $get_record_query);

 //Check whether there are any rows being returned by the query 
    if (mysql_num_rows($execute_get_scheduled_airtime) == 0) 
    {
        // In case there are no records returned
        echo "No record returned";

    } 
    else 
    {
        for ($i = 1 ; ($transactionArray = mysql_fetch_array($execute_get_scheduled_airtime)) && $i < 15 ; $i++)
        {
            // Fetch the records from the database, and assign them variables.
            $order_id = $transactionArray['order_id'];
            $employee_username = $transactionArray['employee_username'];
            $employee_password = $transactionArray['employee_password'];
            $client_session_id = $transactionArray['session_id'];
            $client_first_name = $transactionArray['client_first_name'];
            $client_last_name = $transactionArray['client_last_name'];
            $client_email_address = $transactionArray['client_email_address'];
            $receiver_first_name = $transactionArray['receiver_first_name'];
            $receiver_phone = $transactionArray['receiver_phone'];
            $receiver_email = $transactionArray['receiver_email'];
            $receiver_network_id = $transactionArray['receiver_network_id'];
            $item_id = $transactionArray['item_id'];
            $note_to_receiver = $transactionArray['note_to_receiver'];
            $airtime_load_option = $transactionArray['airtime_load_option'];
            $schedule_frequency = $transactionArray['schedule_frequency'];
            $date_to_commence_sending_airtime = $transactionArray['date_to_commence_sending_airtime'];
            $date_to_stop_sending_airtime = $transactionArray['date_to_stop_sending_airtime'];
            $date_schedule_added = $transactionArray['date_schedule_added'];

                // Create a transaction ID for the airtime to be sent
                $trans_id_date_part = date("YmdHis");
                $trans_id_sess_id_part = substr($client_session_id,-4);

                $transaction_id = TRANSACTION_ID_PREFIX."_".$trans_id_date_part.$trans_id_sess_id_part.$i;

                $transactionArray['transID'] = $transaction_id;
                $transactionArray['employeeUsername'] = $employee_username;
                $transactionArray['employeePassword'] = $employee_password;
                $transactionArray['clientEmail'] = $client_email_address;
                $transactionArray['clientFirstName'] = $client_first_name;
                $transactionArray['receiverFirstName'] = $receiver_first_name;
                $transactionArray['receiverPhone'] = $receiver_phone;
                $transactionArray['receiverEmail'] = $receiver_email;
                $transactionArray['itemIDNumber'] = $item_id;

            // Get the function that is to send out the airtime .   
            $transactionArray = sendScheduledAt($countryAbbreviation,$transactionArray);
            $success = $transactionArray['success'];
            $message = $transactionArray['message'];

        if($success)
        {
            // If the airtime has successfully been sent, then update the database table and update status from 0 to 1
            // This is to ensure that airtime is dispatched once
            $update_schedules = "UPDATE schedule_airtime_submission SET airtime_status='1' WHERE order_id = '$order_id'";
            $execute_update_schedule = mysql_query($update_schedules,$connection) or handleDatabaseError(''. mysql_error(), $get_record_query);
        }
        else
        {
            $_SESSION['message'] = $message;
            echo $_SESSION['message'];
            }
            }
    }

?>

The major concern here is that when line 63 executes, the status of the airtime will be set to 1. That means that when the next day comes, the airtime will not be sent, coz the script will read it as already sent ...

No worries, I have been ablt to work my way around it. Thanx @LastMitch for your input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.