简体   繁体   English

PHP foreach并在特定时间运行

[英]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.) 我不确定我是否正确地编写了foreach(可能没有,并且我不在可以尝试它的环境中,因为它已经钩住了其他所有内容。)

But basically, it will load all of the service calls in my while statement. 但基本上,它将在我的while语句中加载所有服务调用。 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. 我想检查每个记录$ status,并检查它是否为'Unpaid',如果是,请运行函数mailBadAction()并将$ id,$ account,$ status,$ dateEntered传递给该函数。 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. 我对Cron工作没有零了解,所以除非有人想帮助我,否则我认为那是不可能的。 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 : 由于您已经处于循环中,因此要foreach结果,所以您不需要foreach ,您只需要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. 而且,如果您使用的是Linux环境,则需要cron每天运行一次。

The easiest thing to do (if your system has it...), is add the php script to /etc/cron.daily and make it executable. 最简单的操作(如果您的系统具有此功能...)是将php脚本添加到/etc/cron.daily并使其可执行。 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. Jeroen的答案是正确的,您需要一个if语句。

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. 您可以通过添加数据库检查以确保它距离上次发送电子邮件至少24小时,然后更新该时间戳,来设置cron作业。 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. 学习如何设置Cron工作将更有价值。

I think you got the foreach syntax wrong: 我认为您弄错了foreach语法:

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. 现在,来自数据库的每个结果都是“未付款”,因此不需要测试if(status=='Unpaid')

For the Cronjobs look on google after "cron php" and you may get : 对于Cronjobs,请在“ cron php”之后在Google上查找,您可能会得到:

http://www.thegeekstuff.com/2011/07/php-cron-job/ 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. 这意味着您可以完全从主站点弹出脚本并将其作为独立系统运行,而无需网络访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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