gpt4 book ai didi

php - 在php中每三个结果后显示横幅广告图像

转载 作者:行者123 更新时间:2023-11-29 16:13:09 25 4
gpt4 key购买 nike

我想在从 mySql 数据库获取的每 3 个公司名称之后显示从数据库中提取的不同横幅图像。

我目前在一页上显示 25 家公司。DB中的横幅数量为4个,可以更改。

我编写了以下代码,该代码仅在每 3 个公司之后显示第一个横幅。希望每 3 个公司后显示不同的横幅。

<?php 
$Records_per_page = 25;
//Intervel to show banner
$ShowBannerAfter = 3;
//Query to get the companies form DB
$sqlQuery = "SELECT * FROM company WHERE company_name != '' ORDER BY company_name";
//Query to get the banners form the db.
$sqlQueryBanner = "SELECT * FROM banner";

$resultV = mysql_query($sqlQuery);
//Number of companies returned
$num_rowsV = mysql_num_rows($resultV);
//Check if no company is found
if ($num_rowsV > 0) {
//Looping through company recorde
for ($i = 1; $i < $num_rowsV + 1; $i++) {
$row = mysql_fetch_array($resultV);
//Showing the company to the user.
echo "<div>".$row['company_name']."</div>";

//checking if the number of company is less than banner interval.
$j = $num_rowsV <= $ShowBannerAfter ? $num_rowsV - 1 : $ShowBannerAfter;

//Removing the last '-' form the banner ID session
$_SESSION['banID'] = rtrim($_SESSION['banID'],"-");
//array to store all the banner ID
$arrBanID = array();
//passing the banner ID form sessionn to array.
$arrBanID = explode("-",$_SESSION['banID']);
$isBreak = "false";

//This will check to show the banner after every X interval.
if($i % $j == 0){
$resultBanner = mysql_query($sqlQueryBanner );

//Number of banner returned
$num_rowsB = mysql_num_rows($resultBanner);

//Looping through the banner
for($k = 0; $k < $num_rowsSB; $k++){
$rowB = mysql_fetch_array($resultBanner);

//checking if banner id already exist in the array
if(!in_array($rowSB['id'], $arrBanID)){
//if the banner ID not found then this banner should be shown to the user.
$isBreak = "true";
//Adding the current banner ID in Session which is shown to user
$_SESSION['banID'] = $_SESSION['banID'] . $rowSB['id'] . "-";
}else {
//If banner ID found in array dont show to user.
$isBreak = "false";
}
//Show the banner to user if not found in the array.
if($isBreak = "true"){
echo "<img src=" . $rowSB['image_URL'] ."/>";
break;
}
}
}
}
}
?>
Getting Output
----------------------------
COMPANY 1
-----------------------------
COMPANY 2
-----------------------------
COMPANY 3
-----------------------------
BANNER 1
-----------------------------
COMPANY 4
-----------------------------
COMPANY 5
-----------------------------
COMPANY 6
-----------------------------
BANNER 1
-----------------------------
COMPANY 7
-----------------------------


Output Needed
----------------------------
COMPANY 1
-----------------------------
COMPANY 2
-----------------------------
COMPANY 3
-----------------------------
BANNER 1
-----------------------------
COMPANY 4
-----------------------------
COMPANY 5
-----------------------------
COMPANY 6
-----------------------------
BANNER 2
-----------------------------
COMPANY 7
-----------------------------

最佳答案

输入您的查询:$resultBanner = mysql_query($sqlQueryBanner);到顶部不在循环中。

if ($num_rowsV > 0) {
$resultBanner = mysql_query($sqlQueryBanner );
...
}

关于php - 在php中每三个结果后显示横幅广告图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55076143/

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