PHP在每5行结果后插入一个ad div

时间:2017-11-29 01:07:35

标签: php html mysql

如何在结果页面的每第五行后插入一个div?

我有一个脚本,在每个分页中显示来自数据库的15行。 这是我的整个脚本

<?php
    $sql = "SELECT COUNT(id) FROM table";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);

    // Here we have the total row count
    $rows = $row[0];
    $page_rows = 15;

    $last = ceil($rows/$page_rows);

    if($last < 1){
        $last = 1;
    }

    $pagenum = 1;

    if(isset($_GET['pn'])){
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }

    if ($pagenum < 1) { 
        $pagenum = 1; 
    } 
    else if ($pagenum > $last) { 
        $pagenum = $last; 
    }   

    // This sets the range of rows to query for the chosen $pagenum
    $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

    $sql = "SELECT id, fname, image_text, address, contact, price, image FROM table WHERE fname LIKE '%".$search."%' $limit";
    $query = mysqli_query($db_conx, $sql);

    $textline1 = "table (<b>$rows</b>)";
    $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";

    // Establish the $paginationCtrls variable
    $paginationCtrls = '';

    // If there is more than 1 page worth of results
    if($last != 1){

        if ($pagenum > 1) {
            $previous = $pagenum - 1;
            $paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> </td>&nbsp; &nbsp; ';

            for($i = $pagenum-3; $i < $pagenum; $i++){
                if($i > 0){
                    $paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> </td>&nbsp; ';
                }
            }
        }

        $paginationCtrls .= ''.$pagenum.' &nbsp; ';

        for($i = $pagenum+1; $i <= $last; $i++){
            $paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a></td> &nbsp; ';
            if($i >= $pagenum+3){
                break;
            }
        }

        if ($pagenum != $last) {
            $next = $pagenum + 1;
            $paginationCtrls .= ' &nbsp; &nbsp;<td> <a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a></td> ';
        }
    }
    $list = '';
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
?>
    <div class="light-purple column">
        <table style="height:100px; border-bottom:#F88 solid 2px;" width="100%" border="0">
            <tr>
                <td>
                    <img  src="img/<?php echo $row['image']; ?>" alt="No Image">
                </td>
                <td width="70%">
                    <div class="detail_text" style="font-weight:bold;"><input type="hidden" value="<?php echo $row['id']; ?>"/>
                    <?php echo $row['fname']; ?>
                    </div>
                    <div class="detail_text " ><?php echo $row['image_text']; ?></div>
                    <div  style="color: #999; font-weight: bold;"><?php echo $row['address']; ?></div>
                    <div class="detail_text" style="font-weight:bold; color: #606;">
                        <table width="95%" border="0">
                            <tr>
                                <td width="51%"><?php echo $row['contact']; ?></td>
                                <td width="49%"><?php echo $row['price']; ?></td>
                            </tr>
                        </table>
                    </div>
                </td>
            </tr>
        </table>
    </div>
<?php   }
?>

我希望能够在每5行添加一个广告div,就像这样#34;

<div class="ad"></div>

 <row> content </row>
 <row> content </row>
 <row> content </row>
 <row> content </row>
 <row> content </row>

<div class="ad"></div>

 <row> content </row>
 <row> content </row>
 <row> content </row>
 <row> content </row>
 <row> content </row>

<div class="ad"></div>

任何人都可以帮助我如何设置PHP脚本以允许我计算返回的行数,并在每一定数量的行之后插入广告脚本?或者有没有办法在行本身内执行此操作?

2 个答案:

答案 0 :(得分:3)

您可以使用modulus运算符

$count = 1;
while{
   if($count % 5 == 0)
      echo '<div class="add"></div>';

   $count++;
}

答案 1 :(得分:1)

只需添加一个count变量计数到5,然后在循环内重置为零。

$count=0;
while{
      echo "<row> content </row>";
      $count++;
      if($count>=5)
      {
        echo "<div class="ad"></div>";
        $count=0;
      }
    }