简体   繁体   中英

PHP foreach & Run at certain time

First Part:

I am trying to write a script to email us when a service call is not paid.

Heres' what I have got started:

$query = "SELECT * FROM service";
        $result = mysql_query($query);  

    while($row = mysql_fetch_row($result)){

                $id = $row[0];
                $dateEntered = $row[1];
                $type = $row[2];
                $account = $row[3];
                $dateCompleted = $row[4];
                $notes = $row[5];
                $status = $row[6];

                foreach($status == 'Unpaid'){
                mailBadAction($id, $account, $status);

                }
        }   

I am not sure if I wrote the foreach right (probably didn't, and I'm not in environment where I can just try it because its hooked into everything else.)

But basically, it will load all of the service calls in my while statement. I want to check each records $status, and check if it is 'Unpaid', and if so, run the function mailBadAction() and pass the $id, $account, $status, $dateEntered to the function. I only want this to happen ONCE a day.

Second Part:

I need this to run everyday at a certain time, once a day. I have zero understanding of cron jobs so I think that is out unless someone wants to help me out with that. But what I have learned is if I just include this page on the index or login page, it will run when someone simply hits the login page. But this will run it for every single time someone hits the index page.

Can someone help out?

As you are already in a loop, going though the results, so you don't need a foreach , you just need if :

  if($status == 'Unpaid'){
            mailBadAction($id, $account, $status);

    }

And if you are on a linux environment, you need cron to run something once a day.

The easiest thing to do (if your system has it...), is add the php script to /etc/cron.daily and make it executable. Your script would look something like (depending on the environment...):

#!/usr/local/bin/php
<?php
// your script
?>

And you really should test it...

Jeroen's answer is correct, you need an if statement.

You could get around setting up a cron job by adding a database check to make sure it's been at least 24 hours since it last sent out emails, and updating that timestamp. But that's honestly more trouble than it's worth. It would block and slow down immensely as it processed data and sent emails, and some poor sap would be sitting there wondering why the page is taking forever. Learning how to setup a cron job would be much more valuable.

I think you got the foreach syntax wrong:

foreach ($array as $value) {
    //here you can use $value as the current array field
}

But like said before, you can either adjust your query only to give you the fields that are unpaid:

SELECT id,account,status FROM service WHERE status = 'Unpaid'

(i dont know how exactly your table looks like, but i imagine that structure)

Now every result coming from your DB is "Unpaid", so the testing if(status=='Unpaid') is unnecessary.

For the Cronjobs look on google after "cron php" and you may get :

http://www.thegeekstuff.com/2011/07/php-cron-job/

That means you pop the Script completely from the main site and run it as an stand-alone system, without acces from the web.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM