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

标签 php mysql

我想在从 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/

相关文章:

php - 优化SQL语句: Select multiple values

php - 我可以在准备好的语句中参数化表名吗?

php - 双引号嵌套数组不起作用

MySQL - 有没有办法在不编写脚本的情况下将 bin 日志位置包含在转储文件中

MySQL:在这种简单的情况下,何时应用 where 条件检查和文件排序操作?

mysql - 如何使用 Wordpress UpdraftPlus 修复 MySql 数据库恢复错误 : Error Code: 1067. 'user_registered' 的默认值无效

php - 按下后退按钮 Laravel 注销失败

mysql - 成语中多列别名的示例

mysql - 使用mysql插入具有不同条件的一个字段值

php - 其中一个按钮可以使用,但另一个按钮不能使用...而且我在使用 php 中的链接时遇到了问题