Forum Moderators: coopster

Message Too Old, No Replies

Running PHP on the command line

How to passrguments?

         

dbarasuk

1:07 pm on Sep 4, 2008 (gmt 0)

10+ Year Member



Hey,

I have an SQL script to insert daily work records in a database.

However, due to the fact that a worker can change the employer from day to day, those records are subject to changes. So I thought PHP would come in handy to address those changes in inserting the records since It allows to write a function that would be run on the command line, passing arguments therebyin, which would avoid me to every time adapt the SQL script whenever the employer comes to change.

Here's how the SQL script looks like:

INSERT INTO work VALUES
(
NULL, CURDATE() , DAYNAME(CURDATE()),'DMX','07:00:00','17:00:00',(finish-start),
'1:00:00',( duration - break ),10.35,CAST(HOUR(net_duration)+MINUTE(net_duration)/60 AS DECIMAL(4,2))*hourly_pay,
'0:00',0,0,daily_pay
);

table `work` columns are:

work_id,
date,
day,
company
start,
finish,
duration,
break,
net_duration,
hourly_pay,
daily_pay,
extratime,
extratime_rate,
extratime_pay,
total_daily_pay

When an employer changes, column values that must change are:
- company,
- start and finish(every company has its own work schedule),
- break (not every company offers same break length),
- hourly_pay (notevery company offers employees same wages),
- extratime (some companies allow overtime),
- extratime_rate(whenever there's overtime, the hourly rate depends on the company),
** The duration is just the difference between the finish time and the starting time whereas the net_duration is the difference between the duration and the break time.

So, i'd like a help of a PHP function that would be run on the command line to simplify the editing of SQL file every time there's any change, a function like:
work($company, $start, $finish, $break, $hourly_pay, $extratime, $extratime_rate) to be called on the command-line.
Also which configuration should I make to make PHP be runnable on the command line?

Thank you guys

vincevincevince

1:31 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look into the array '$argv'; it's an array of command line arguments.

dbarasuk

3:05 pm on Sep 4, 2008 (gmt 0)

10+ Year Member



thanks,
but this is a pretty new topic for me since i have never run PHP on the command-line, so, a more detailed explaination would help me more.

Where's the array '$argv' located

vincevincevince

4:10 am on Sep 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



in PHP, try writing this:

print_r($argv);

Then run your code:

php whateverfile.php firstarg secondarg

eelixduppy

4:21 am on Sep 5, 2008 (gmt 0)



or more formally the
$_SERVER['argv']
and
$_SERVER['argc']
variables.