我有三个功能: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个广告。如果此条件= true,那么我想执行getActDefault()
函数。但如果第一个函数(showActiveAdvert()
)根本没有返回任何广告,那么请getDefaultBanner()
注意:对于其他函数(getActDefault()
和getDefaultBanner
),我还需要执行foreach循环(与showActiveAdverts
的方法相同),以便细节准确从我的DB获得。
完成这项工作的最佳方法是什么?
答案 0 :(得分:1)
我想这就是你想要的。我在代码中添加了一些内容来解释我正在尝试做什么。在添加'</div>'
之前,这将在第一个foreach之后进行。
<?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;
?>