gpt4 book ai didi

php - 为什么这个 foreach 循环提前结束?

转载 作者:行者123 更新时间:2023-12-02 03:17:36 25 4
gpt4 key购买 nike

我想知道为什么这个函数中的 foreach 循环在循环一次后输出数据后就结束了。我已经在 MySQL 中运行了查询,它返回了我正在查找的 8 条记录。当我 print_r 数组在 foreach 之前和之后都被打包到数组中时,它在数组中显示了 8 个记录集。

但是,当我在页面上运行该函数时,它只输出第一个记录集,然后停止。有什么想法吗?

此外,将所有这些都塞到一个函数中还有一个非常长且愚蠢的原因。只是知道,这是我唯一的选择。

function query_hp_hero_events() {
global $wpdb;

$sql = "SELECT SQL_CALC_FOUND_ROWS *, mt2.meta_value AS start_time, mt3.meta_value AS end_time, mt4.meta_value AS start_date, mt5.meta_value AS event_id, mt6.meta_value AS event_meta, mt7.meta_value AS event_thumb FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id AND mt2.meta_key = 'event_start_time') INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id AND mt3.meta_key = 'event_end_time') INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id AND mt4.meta_key = 'event_start_date') INNER JOIN wp_postmeta AS mt5 ON (wp_posts.ID = mt5.post_id AND mt5.meta_key = 'event_id') INNER JOIN wp_postmeta AS mt6 ON (wp_posts.ID = mt6.post_id AND mt6.meta_key = 'event_meta') INNER JOIN wp_postmeta AS mt7 ON (wp_posts.ID = mt7.post_id AND mt7.meta_key = 'event_thumbnail_url') WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (7) ) AND wp_posts.post_type = 'espresso_event' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_key = 'event_start_date' AND (mt1.meta_key = 'event_start_date' AND CAST(mt1.meta_value AS DATE) >= '" . date('Y-m-d') . "') ) GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS DATE) ASC, CAST(mt2.meta_value AS TIME) ASC LIMIT 0, 8";

echo $sql;

$events_query = $wpdb->get_results( $wpdb->prepare( $sql ));

foreach ($events_query as $event_listing) {

$eventDate = strtotime($event_listing->start_date);
$eventEndTime = strtotime($event_listing->end_time);
$eventStartTime = strtotime($event_listing->start_time);
$eventId = $event_listing->event_id;
$eventMeta = $event_listing->event_meta;
$eventThumb = $event_listing->event_thumb;

$newEventMeta = unserialize($eventMeta);
$newEventMeta2 = unserialize($newEventMeta);

$homepageOverride = $newEventMeta2["homepage_override"];
$ticketLink = $newEventMeta2["ticket_link"];
$suiteRentals = $newEventMeta2["suite_rentals_link"];
$vipTickets = $newEventMeta2["vip_ticket_link"];
$groupTicketSales = $newEventMeta2["group_sales_link"];
$parkingLink = $newEventMeta2["parking"];

if ($eventDate >= strtotime(date('Y-m-d')) && $eventEndTime >= strtotime(date('G:i'))) {

$hero = '<div id="event' . $eventId . '">';
$hero .= '<div class="details">';
if ( in_category( 'news' ) ) {
$hero .= '<h1><a href="' . get_permalink() . '">' . $event_listing->post_title . '</a></h1>';
} else {
$hero .= '<h1><a href="/event-registration/?ee=' . $eventId . '">' . $event_listing->post_title . '</a></h1>';
}

if ( isset ( $homepageOverride ) && $homepageOverride != "" ) {
$hero .= '<h2>' . $homepageOverride . '</h2>';
} else {
$hero .= homepage_event_date_range($eventId, $eventStartTime);
}

$hero .= '<ul>';
if ( in_category( 'news' ) ) {
$hero .= '<li><a href="' . get_permalink() . '">More Info</a></li>';
} else {
$hero .= '<li><a href="/event-registration/?ee=' . $eventId . '">More Info</a></li>';
}
if ( isset ( $ticketLink ) && $ticketLink != "" ) { $hero .= '<li><a href="' . $ticketLink . '" rel="external">Buy Tickets</a></li>'; }
if ( isset ( $suiteRentals) && $suiteRentals != "" ) { $hero .= '<li><a href="' . $suiteRentals . '">Suite Rentals</a></li>'; }
if ( isset ( $vipTickets) && $vipTickets != "" ) { $hero .= '<li><a href="' . $vipTickets . '">VIP Seats</a></li>'; }
if ( isset ( $groupTicketSales) && $groupTicketSales != "" ) { $hero .= '<li><a href="' . $groupTicketSales . '">Group Tickets</a></li>'; }
if ( isset ( $parkingLink) && $parkingLink != "" ) { $hero .= '<li><a href="' . $parkingLink . '" rel="external">Parking</a></li>'; }
$hero .= '</ul>';

$hero .= '</div>';

if ( isset( $regexMatch[0] ) && $regexMatch[0] != "" && isset( $youtubeIdMatch[0] ) && $youtubeIdMatch[0] != "") {
$hero .= '<div class="yt-video-player" id="videoplayerdiv1" src="http://www.youtube.com/v/' . $youtubeIdMatch[0] . '?enablejsapi=1&playerapiid=videoplayergen1&version=3"></div>';
} else {
$hero .= '<img src="' . $eventThumb . '" width="595" height="350" alt="" class="heroImg" />';
}

$hero .= '</div>';

}

return $hero;

}
}

最佳答案

您的 foreach 中有一个 return - 一旦它到达 return,函数就会返回,并且不再执行任何迭代.

关于php - 为什么这个 foreach 循环提前结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16193997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com