我有三个函数:showActiveAdverts()
,getActDefault()
和getDefaultBanner()
,并且我想使用一个条件遍历所有的函数。
这是我的代码:
<?php
$showBoard_arr = showActiveAdverts();
if($showBoard_arr){
$countBoard;
$advertTop .= '<div class="adSlot">' ;
$advertBottom .= '<div class="adSlot">' ;
foreach($showBoard_arr as $key => $showBoard){ //Get information from my DB
$countBoard += 1;
$advertId = getAdLocation($showBoard, 'id');
$advertTitle = getAdLocation($showBoard, 'title');
$advertImg = refineProfileImage(getAdLocation($showBoard, 'img_url'));
$advertUrl = $site_path . "clicks/" . $board_slug . "/" . $advertId;
$activeBoardIds = getAdLocation($showBoard, 'board_id');
// This is where I split the banners into two, i.e 3 top, three bottom
if ($countBoard <= 3){
$advertTop .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';
}
else {$advertBottom .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';}
}
/* What I want to do is to check if the function showActiveAdverts() does not return 6 advert.
*If this condition = true, then I want to do the getActDefault() function. But if the first
*function (showActiveAdvert()) does not return any advert at all, then do getDefaultBanner().*/
$advertTop .= '</div>' ;
$advertBottom .= '</div>' ;
echo $advertTop;
}
?>
我要做的是检查函数
showActiveAdverts()
是否不返回6 advert。如果此条件为true,则我要执行getActDefault()
函数。但如果第一个函数(showActiveAdvert()
)根本不返回任何广告,则执行getDefaultBanner()
。注意:对于其他函数(
getActDefault()
和getDefaultBanner
),我还需要做一个foreach循环(与showActiveAdverts
的方法相同),以便准确地从数据库中获取详细信息。做这件事最好的方法是什么?
最佳答案
我想这就是你想要的。我在代码中添加了一些内容来解释我要做的事情。这将在第一个foreach之后添加'</div>'
。
<?php
$showBoard_arr = showActiveAdverts();
//Defining this before the first function, in case no results.
$countBoard = 0;
$advertTop .= '<div class="adSlot">' ;
$advertBottom .= '<div class="adSlot">' ;
if($showBoard_arr){
foreach($showBoard_arr as $key => $showBoard){ //Get information from my DB
$countBoard += 1;
$advertId = getAdLocation($showBoard, 'id');
$advertTitle = getAdLocation($showBoard, 'title');
$advertImg = refineProfileImage(getAdLocation($showBoard, 'img_url'));
$advertUrl = $site_path . "clicks/" . $board_slug . "/" . $advertId;
$activeBoardIds = getAdLocation($showBoard, 'board_id');
if ($countBoard <= 3){
$advertTop .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';
}
else {$advertBottom .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';}
}
//The first foreach is ended, now I check if there were not 6 adverts.
if($countBoard != 6){ //If there are NOT exactly 6 adverts.
$countBoard = 0;
$advertTop = '<div class="adSlot">' ;
$advertBottom = '<div class="adSlot">'; //Empty the first function adverts.
$showBoard_arr = getActDefault(); //Get the adverts from this function.
//Repeating foreach statement.
foreach($showBoard_arr as $key => $showBoard){ //Get information from my DB
$countBoard += 1;
$advertId = getAdLocation($showBoard, 'id');
$advertTitle = getAdLocation($showBoard, 'title');
$advertImg = refineProfileImage(getAdLocation($showBoard, 'img_url'));
$advertUrl = $site_path . "clicks/" . $board_slug . "/" . $advertId;
$activeBoardIds = getAdLocation($showBoard, 'board_id');
if ($countBoard <= 3){
$advertTop .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';
}
else {$advertBottom .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';}
}
}
}
//So if we are here there are 2 options: 1) First or second function done.
//2) No results so we need the third function to be called.
if ($countBoard == 0){ //If there are no results at all.
$showBoard_arr = getDefaultBanner(); //Get adverts from this other function.
//Repeating foreach statement.
foreach($showBoard_arr as $key => $showBoard){ //Get information from my DB
$countBoard += 1;
$advertId = getAdLocation($showBoard, 'id');
$advertTitle = getAdLocation($showBoard, 'title');
$advertImg = refineProfileImage(getAdLocation($showBoard, 'img_url'));
$advertUrl = $site_path . "clicks/" . $board_slug . "/" . $advertId;
$activeBoardIds = getAdLocation($showBoard, 'board_id');
if ($countBoard <= 3){
$advertTop .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';
}
else {$advertBottom .= '<a href="'.$advertUrl.'" rel="nofollow"><img src="'.$site_path . "banners/" . $advertImg.'"></a>';}
}
}
//Now we are done, for x, y or z, the adverts are loaded. So we show them.
$advertTop .= '</div>' ;
$advertBottom .= '</div>' ;
echo $advertTop;
?>
关于php - 传递条件后如何运行3个foreach语句?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33436071/