doudai8083 2014-06-11 14:48
浏览 28

PHP循环使用CRON返回重复的结果

I have a script that aims to remind users about events they've added to their planner on a website. The script works fine if I load it up in the browser and echo out the result, all of the events that should be there, are there.

However, if I put this code into a function that fires off with a CRON, I get the right number of events in the list, but they all have the information from the last event.

Any ideas why this may be?

Here's the code that runs the script below.

<?php $args = array(
        'role' => 'Subscriber',
        'meta_key' => 'planner-reminders',
        'meta_value' => '0',
        'meta_compare' => '==',
    ); ?>

    <?php $user_query = new WP_User_Query( $args ); ?>

    <?php if ( ! empty( $user_query->results ) ) : ?>


        <?php foreach ( $user_query->results as $user ) : ?>
            <?php $matchedEvents = array(); ?>

            <?php $user_id = $user->ID; ?>

            <?php $planner = get_field('events', 'user_'.$user_id.''); ?>
            <?php $events = explode(" ", $planner); ?>



            <?php $plus = 0; ?>
            <?php foreach ( $events as $event ) : ?>


                <?php $args2 = array(
                    'post_type' => 'event',
                    'p' => $event
                ); ?>

                <?php $the_query = new WP_Query( $args2 ); ?>

                    <?php if( $the_query->have_posts() ): ?>
                        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                            <?php if( have_rows('date') ): ?>
                                <?php $row = 0; ?>
                                <?php while ( have_rows('date') ) : the_row(); ?>
                                    <?php $row++; ?>
                                <?php endwhile; ?>
                            <?php endif; ?>

                            <?php if( have_rows('date') ): ?>
                                <?php $count = 0; ?>
                                <?php while ( have_rows('date') ) : the_row(); ?>

                                    <?php
                                        $startDate = get_sub_field('start-date');
                                        $endDate = get_sub_field('end-date');
                                        $date = date('Ymd');
                                    ?>

                                    <?php if( $endDate < $date && $count === $row ) : ?>

                                        <?php break; ?>

                                    <?php elseif ( $endDate > $date  ) : ?>

                                        <?php if( $startDate <= $date ) : ?>

                                            <?php if ($count === $row) {
                                                break;
                                            } ?>

                                        <?php elseif ( $startDate < date('Ymd', strtotime('+7 day') ) ) : ?>

                                                <?php array_push( $matchedEvents, get_the_ID() ); ?>

                                            <?php if ($count === $row) {
                                                break;
                                            } ?>

                                        <?php endif; ?>

                                    <?php endif; ?>
                                    <?php $count++; ?>  
                                <?php endwhile; ?>
                            <?php endif; ?>

                        <?php endwhile; ?>
                    <?php endif; ?>

                <?php $plus++; ?>

            <?php endforeach; ?>


            <?php

                $userEmail = $user->user_email;
                $userName = $user->first_name;
                $link = get_template_directory_uri();
                $eventhtml = '';

                $args = array(
                    'post__in' => $matchedEvents,
                    'post_type' => 'event'
                );


                $event_query = new WP_Query( $args );
                $plus = 0;

               $headers = 'From: What`s On Advisor <info@whatsonadvisor.com>' . "
";
               $headers .= "MIME-Version: 1.0
";
               $headers .= "Content-Type: text/html; charset=ISO-8859-1
";
               $message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>What`s On Advisor</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
               $message .= '<style type="text/css">* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; } a { color: #61c7dd; text-decoration: none; } a:link {color: #61c7dd;} a:hover { color: #59b8cc; } hr { border: none; height: 2px; width: 100%; background: #767572; float: left; text-align: left; } </style>';
               $message .= '</head><body bgcolor="#0e72b0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">';
               $message .= '<table width="600px" border="0" cellspacing="0" cellpadding="0" style="margin:0 auto;"><tr><td valign="top" bgcolor="#ffffff"><table width="600" border="0" align="center" cellpadding="0" cellspacing="0" style="font: 14px Helvetica, Arial, sans-serif; color: #767572; line-height: 100%;"><tr><td colspan="3"><table width="600" height="120" border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" style="width: 600px; height: 125px; text-align: center;"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="height: 125px;"><tr><td style="height: 33px; background-color:rgb(44, 63, 79);"><img src="'.$link.'/img/email-logo.jpg" height="33" width="279" /></td></tr></table></td></tr></table></td></tr><tr style="padding: 20px 10px;"><td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="color: #767572; padding: 10px;"><tr><td style="font-family: Helvetica, Arial, sans-serif; font-size: 30px; text-align: center; font-weight: bold; color: #767572; letter-spacing: -2px; line-height: 160%;">Hi '.$userName.',</td></tr><tr><td style="font-family: Helvetica, Arial, sans-serif; line-height: 130%; color: #767572;"><small>Here are a list of events that you have added to your planner. They are all starting within the next week so be sure to go along for a fun day out! Go back to your <a href="'.site_url().'/planner">planner</a> to edit events or use the <a href="'.site_url().'/search">search</a> to find more great events.</small></td></tr></table></td></tr>';

               $message .= '<tr style="padding: 20px 10px; width: 100%; display: inline-block;">
                        <td valign="top" style="width:100%; display:block;"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="color: #767572; padding: 10px;">
                          <tr>
                            <td style="font-family: Helvetica, Arial, sans-serif; line-height: 130%; color: #767572;">';


                foreach( $event_query->posts as $post ) : ?>

                    <?php 
                        $eventlink = get_permalink();
                        $title = get_the_title();

                        $message .= '<tr>';
                        $message .= '<tr style="padding: 10px 10px 0px 10px; width: 100%; display:block;">';
                        $message .= '<h2><a href="'.$eventlink.'">'.$title.'</a></h2>';
                        $message .= '</tr>';

                    ?>


                        <?php if( have_rows('date') ): ?>

                            <?php $row = 0; ?>

                            <?php while ( have_rows('date') ) : the_row(); ?>
                                <?php $row++; ?>
                            <?php endwhile; ?>

                        <?php endif; ?>

                        <?php if( have_rows('date') ): ?>

                                <?php while ( have_rows('date') ) : the_row(); ?>


                                    <?php
                                        $startDate = get_sub_field('start-date');
                                        $newDate = date("m/d/Y", strtotime($startDate));
                                        $endDate = get_sub_field('end-date');
                                        $countdownDate = date("F j, Y ", strtotime($startDate));

                                        if( have_rows('start-end-times') ) : 
                                            while ( have_rows('start-end-times') ) : the_row();

                                                $startTime = get_sub_field('start-time');
                                                $newTime = date("G:i A", strtotime($startTime));
                                                $countdownTime = date("G:i:s", strtotime($startTime));

                                            endwhile;
                                        endif; 

                                        $countdownFormat = $countdownDate.' '.$countdownTime;
                                        $date = date('Ymd');
                                        $count = 1;
                                    ?>

                                    <?php if( $endDate < $date && $count === $row ) : ?>
                                            <?php $date = date("F j, Y", strtotime($startDate)); ?>
                                        <?php break; ?>

                                    <?php elseif ( $endDate > $date  ) : ?>

                                        <?php if( $startDate < $date ) : ?>
                                            <?php $date = date("F j, Y", strtotime($startDate)); ?>
                                            <?php $count++; if ($count >= 1) {
                                                break;
                                            } ?>

                                        <?php elseif ( $startDate > $date ) : ?>
                                                <?php $date = date("F j, Y", strtotime($startDate)); ?>
                                            <?php $count++; if ($count >= 1) {
                                                break;
                                            } ?>

                                        <?php endif; ?>

                                    <?php endif; ?>

                                <?php endwhile; ?>

                        <?php endif;

                        $message .= '<tr style="padding: 0px 10px; width: 100%; display: inline-block;">Starts: '.$date.'<br/></tr>';
                        $message .= '<tr style="padding: 0px 10px; width: 100%; display: inline-block;">Distance: '.$distance.'<br/><br/></tr>';

                        $description = get_field('mini-description');
                        $message .= '<tr style="padding: 20px 10px 20px; width: 100%; display: block;">'.$description.'<br/><br/></tr>';

                        $message .= '<tr>';
                        $plus++;

                endforeach;


               $message .= '</td>
                          </tr>
                          <tr><br/><br/><br/>Thanks,<br/>What`s On Advisor<br /><br /><br /></tr>
                          <tr><small>If you`d like to unsubscribe from these e-mails, please login to <a href="'.site_url().'/account">your account</a> and change your preferences.</small></tr>
                        </table></td>
                      </tr>';

               $message .= '</table></td></tr>';
               $message .= '</table></body></html>';



               wp_mail($userEmail, 'Upcoming Events', $message, $headers );


               wp_reset_postdata();

            ?>





        <?php endforeach; ?>

    <?php else : ?>

    <?php endif; ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度