在PHP中每三分之一显示一次后显示横幅广告图像

时间:2019-03-09 10:00:56

标签: php mysql

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

我目前正在一页上显示25家公司。 数据库中的横幅数量为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
-----------------------------

1 个答案:

答案 0 :(得分:0)

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

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