从MYSQL表中获取所有结果,每行限制6个结果,然后将下6个结果分解到新行等上?

我正在尝试列出表用户的所有结果/用户数,并显示其个人资料图像。

我想在每行显示6,然后将下一个6,下一个和下一个依次类推,以此类推。

到目前为止,这是我的代码:

 <div id="category_case_holder">
    <?php $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);

for ($i = 1; $i <= 6; $i++) {
$key = $i - 1;
if (isset($result[$key])) {
$filename = "data/profile/$i/main/profile.jpg";
if (file_exists($filename)) {
echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/'.htmlspecialchars($result[$key]['user_id']).'/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
} else {
echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/0/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
} } else {
echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';
} } ?>
</div>

CSS样式:

#category_case_holder {
    height:150px;
    display: flex;    
    width:100%;
    height:150px;
    position:relative;


}

#category_case_holder div {
    flex-basis: 100%;
    background:#1f1f1f;
    margin:0 0 0 10px;
    border: 1px solid #333;
    box-shadow: 0 0 8px 2px #ccc; /* the shadow */

}

请有人可以帮助改善/修改我的代码以获得我需要的结果。

谢谢

评论
  • 第夏山
    第夏山 回复

    The following is wholly untested and perhaps wide of the mark having not seen the data nor had an answer regarding the profile image but the general aim was to break the results array into little bits - array_chunk is ideal for this.

    <div id="category_case_holder">
        <?php
    
            $sql = "SELECT * FROM `users` WHERE `status` = 'active' AND `usertype` = 'advertiser'";
            $result = $conn->query( $sql )->fetch_all( MYSQLI_ASSOC );
    
            # a placeholder in the path will be substituted later using sprintf or printf
            $filepath = 'data/profile/%s/main/profile.jpg';
    
            if( !empty( $result ) ){
    
                # split the recordset array into chunks - 6 records long. 
                # Each chunk will be a row from recordset.
                $chunks=array_chunk( $result, 6 );
    
                foreach( $chunks as $chunk ){
                    echo '<div>';
                    foreach( $chunk as $i => $rs ){
    
                        $filename=file_exists( sprintf( $filepath, $i ) ) ? sprintf( $filepath, $rs['user_id'] ) : sprintf( $filepath, '0' );
                        # ID attributes MUST be unique.
                        # substitue placeholders for values from this row data
    
                        printf('
                            <div class="prime">
                                <a href="profile.php?id=%s">
                                    <img src="%s" alt="Profile" height="100%" width="100%">
                                </a>
                            </div>',
                            htmlspecialchars( $rs['user_id'] ),
                            $filename
                        );
                    }
                    echo '</div>';
                }
            }else{
                echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';
            }
        ?>
    </div>
    
  • oiusto
    oiusto 回复

    如果我理解您的问题,我认为您应该尝试以下操作:

    If I understand your question, I think you should try this:
    
    
    <div id="category_case_holder">
    <table>
    <tr>
    <?php 
      $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";
      $result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);
      $resultNumber = count($result);
      for ($i = 1; $i <= $resultNumber; $i++) {
        echo "<td>";
        $key = $i - 1;
          if (isset($result[$key])) {
           $filename = "data/profile/$i/main/profile.jpg";
           if (file_exists($filename)) {
            echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/'.htmlspecialchars($result[$key]['user_id']).'/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
            } else {
              echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/0/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
            }
          } else {
            echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';
          }
        echo "</td>";
        if (($i % 6) == 0 && $i <= $resultNumber) {
          echo "</tr>";
          echo "<tr>";
        }
      }
    ?>
    </tr>
    </div>