PHP foreach 和 foreach在特定时间运行

发布于 2025-01-02 01:39:23 字数 1052 浏览 1 评论 0原文

第一部分

我正在尝试编写一个脚本,以便在未支付服务电话费用时向我们发送电子邮件。

这是我开始的内容:

$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);

                }
        }   

我不确定我是否正确编写了 foreach (可能没有,而且我不在可以尝试它的环境中,因为它与其他所有内容挂钩。)

但基本上,它会在我的 while 语句中加载所有服务调用。我想检查每条记录 $status,并检查它是否为“Unpaid”,如果是,则运行函数 mailBadAction() 并将 $id、$account、$status、$dateEntered 传递给该函数。 我只希望这种情况每天发生一次。

第二部分:

我需要它每天在特定时间运行一次,每天一次。我对 cron 作业的了解为零,所以我认为除非有人愿意帮助我解决这个问题,否则这是不可能的。但我了解到,如果我只是将此页面包含在索引或登录页面上,那么当有人只需点击登录页面时它就会运行。但这会在每次有人点击索引页时运行它。

有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七秒鱼° 2025-01-09 01:39:23

由于您已经处于循环中,正在检查结果,因此您不需要 foreach,只需要 if

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

    }

如果您在 Linux 环境中,你需要 cron 每天运行一次。

最简单的事情(如果你的系统有它......),是将 php 脚本添加到 /etc/cron.daily 并使其可执行。你的脚本看起来像这样(取决于环境......):

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

你真的应该测试它......

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...

彩扇题诗 2025-01-09 01:39:23

Jeroen 的答案是正确的,您需要一个 if 语句。

您可以通过添加数据库检查来绕过设置 cron 作业,以确保自上次发送电子邮件以来至少已过去 24 小时,并更新该时间戳。但老实说,这带来的麻烦大于其价值。当它处理数据和发送电子邮件时,它会阻塞并大大减慢速度,一些可怜的傻瓜会坐在那里想知道为什么页面会永远等待。学习如何设置 cron 作业会更有价值。

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.

时光无声 2025-01-09 01:39:23

我认为你的 foreach 语法是错误的:

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

但是就像之前所说的,你可以调整你的查询只为你提供未付费的字段:(

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

我不知道你的表到底是什么样子,但我想象那个结构)

现在每个结果来自您的数据库的是“未付费”,因此测试 if(status=='Unpaid') 是不必要的。

对于 Cronjobs,在“cron php”之后在 google 上查找,您可能会得到:

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

这意味着您完全从主站点弹出脚本并将其作为独立系统运行,无需访问网络。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文