如何通过限制每行4个结果来分离php mysqli结果

时间:2015-03-03 17:09:02

标签: php mysql mysqli

我正在尝试使用php mysqli函数从数据库中获取结果,其中我可以成功获取所有结果,但它们都显示在同一行中。如何使它们在显示4结果后将行分开?

假设我在数据库中总共有8个结果。因此,如果我使用while循环来获取数据并回显它,它应该是这样的:

data1 data2 data3 data4 data5 data6 data7 data8

但如何让它显示如下:

data1 data2 data3 data4 
data5 data6 data7 data8

这是示例硬编码的div结果:

 <div class="row">
    <div id="portofolio">
        <!-- Project 1-->
        <div class="three columns category trains">
            <h5>Sponsor 1</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo1.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 2-->
        <div class="three columns category castles">
            <h5>Sponsor 2</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo2.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 3-->
        <div class="three columns category nature">
            <h5>Sponsor 3</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo7.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 4-->
        <div class="three columns category castles">
            <h5>Sponsor 4</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo3.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 5--><!-- Project 6--><!-- Project 7-->

        <!-- Project 8-->

    </div>
</div>

 <div class="row">
    <div id="portofolio">
        <!-- Project 1-->
        <div class="three columns category trains">
            <h5>Sponsor 5</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo4.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 2-->
        <div class="three columns category castles">
            <h5>Sponsor 6</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo5.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 3-->
        <div class="three columns category nature">
            <h5>Sponsor 7</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo8.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 4-->
        <div class="three columns category castles">
            <h5>Sponsor 8</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img src="images/temp/logo6.jpg" class="fourimage" alt=""/>
            </div>
        </div>
        <!-- Project 5--><!-- Project 6--><!-- Project 7-->

        <!-- Project 8-->
    </div>  
    </div>

这是我将要显示结果的PHP代码,但我不知道如何将结果与4分开并转到下一行并显示。

PHP代码:

<?php
    include 'dbConnection.php';

    global $dbLink;



    $image = "SELECT * FROM sponsor_item where sponsor_data !='' ";
    $result=mysqli_query($dbLink,$image);   

    // Numeric array
    while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){


    ?>
     <div class="row">
        <div id="portofolio">
            <!-- Project 1-->
            <div class="three columns category trains">
                <h5>Sponsor 1</h5>
                <p>
                     Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                </p>
                <div class="portofoliothumb">

                  <img style="margin-left: 0px; margin-top: -218.375px; width: 100%; height: 1150px;" src="data:image/jpeg;base64,<?php echo base64_encode($row['sponsor_data']); ?>" alt="image0<?php echo $row['sponsor_item_id']; ?>">   
                </div>
            </div>
    </div>
    <?php 
    }
    mysqli_free_result($result);
    // Close the mysql connection
    $dbLink->close();
    ?>

3 个答案:

答案 0 :(得分:2)

您需要添加一个计数器并查看计数器值是4的倍数。如果是这种情况,只需回显DIV结束标记和新的DIV开始标记。您还需要从DIV循环中排除第一个和最后一个WHILE代码:

<?php
include 'dbConnection.php';

global $dbLink;



$image = "SELECT * FROM sponsor_item where sponsor_data !='' ";
$result=mysqli_query($dbLink,$image);   

$item = 0;

?>
 <div class="row">
    <div id="portofolio">
<?php
// Numeric array
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
?>
        <!-- Project 1-->
        <div class="three columns category trains">
            <h5>Sponsor 1</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img style="margin-left: 0px; margin-top: -218.375px; width: 100%; height: 1150px;" src="data:image/jpeg;base64,<?php echo base64_encode($row['sponsor_data']); ?>" alt="image0<?php echo $row['sponsor_item_id']; ?>">   
            </div>
        </div>
    <?php 
    $item++;
    if ($item % 4 == 0) { echo '</div></div><div class="row"><div id="portofolio">'; }
}
mysqli_free_result($result);
// Close the mysql connection
$dbLink->close();
?>
    </div>
</div>

答案 1 :(得分:0)

计算您已打印的行数,然后使用该行在多行之后插入行

$counter = 0;

foreach($result as....)
{
    print $result->whatever;
    $counter++;
    if($counter % 4 == 0) // Counter divides cleanly by 4
    {
        print "<br />";
        //Or whatever goes between your rows, eg </div><div class="whatever">
    }
}

为此,请使用模数运算符%。这给出了将第一个数除以第二个数的余数,所以如果数字干净地除以则给出0的答案

例如

1 % 4 = 1 // No new line
2 % 4 = 2 // No new line
3 % 4 = 3 // No new line
4 % 4 = 0 // New line!
5 % 4 = 1 // No new line

答案 2 :(得分:0)

使用

<?php
    $counter=1;
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
{
  if($counter%4==0)
  {
  ?>
    <div class="row">
      <div id="portofolio">
  <?php
  }
  ?>
  <!--Your Regular repeating DIVS start here-->
        <div class="three columns category trains">
            <h5>Sponsor 1</h5>
            <p>
                 Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            </p>
            <div class="portofoliothumb">

              <img style="margin-left: 0px; margin-top: -218.375px; width: 100%; height: 1150px;" src="data:image/jpeg;base64,<?php echo base64_encode($row['sponsor_data']); ?>" alt="image0<?php echo $row['sponsor_item_id']; ?>">   
            </div>
        </div>
  <!--Your Regular repeating DIVS end here-->
  <?php 
 if($counter%4==0)
  {
  ?>
      </div>
    </div>
  <?php
  }
    $count+=1; 
  }
 ?>