从表中获取所有结果,每行最多列出 6 个结果。如果少于 6 个结果存在,列出其他项目以填充行中

我的 MYSQL 表“用户”中共有 8 个结果/用户。


我想每行显示 6 个结果/用户配置文件图像,如下所示:


1st Result.   2nd Result.  3rd Result.  4th Result.   5th Result.  6th Result


7th Result.   8th Result.    No More Results..... 

要求每行至少包含 6 个结果/个人资料图片。如果没有足够的结果/个人资料图片来完成一行,那么我会尝试使用模板个人资料图片“在此处宣传您的个人资料”来填充空间。


advertise here 模板图像存储在以下目录中:


<div><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';

给出期望的结果:


1st Result.   2nd Result.  3rd Result.  4th Result.   5th Result.  6th Result


7th Result.   8th Result.    9 Ad Here.  10 Ad Here.  11 Ad Here.  12 Ad Here.

这是我目前拥有的代码,感谢用户@MegaColorBoy 到目前为止帮助我编写代码。


但是,代码仍然没有给出预期的结果。请有人帮助改进修改代码,让它给我我需要的结果。谢谢。


代码:


 <?php $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";

        $result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);


        $limit = 6; 

        $chunks = array_chunk($result, $limit);


        foreach($chunks as $chunk){

        echo '<div id="category_case_holder">';

        foreach($chunk as $chunkItem){



        echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($chunkItem['user_id']).'"><img src="data/profile/'.htmlspecialchars($chunkItem['user_id']).'/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';



        }

        echo '</div>';


        } ?> 


白板的微信
浏览 138回答 3
3回答

摇曳的蔷薇

如果我理解你的问题,我想你应该试试这个:<div id="category_case_holder"><table><tr><?php&nbsp;&nbsp; $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";&nbsp; $result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);&nbsp; $resultNumber = count($result);&nbsp; for ($i = 1; $i <= $resultNumber; $i++) {&nbsp; &nbsp; echo "<td>";&nbsp; &nbsp; $key = $i - 1;&nbsp; &nbsp; &nbsp; if (isset($result[$key])) {&nbsp; &nbsp; &nbsp; &nbsp;$filename = "data/profile/$i/main/profile.jpg";&nbsp; &nbsp; &nbsp; &nbsp;if (file_exists($filename)) {&nbsp; &nbsp; &nbsp; &nbsp; 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>';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; echo "</td>";&nbsp; &nbsp; if (($i % 6) == 0 && $i <= $resultNumber) {&nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; }&nbsp; }?></tr></div>

芜湖不芜

以下是完全未经测试的,可能还没有看到数据,也没有关于个人资料图像的答案,但总体目标是将结果数组分解成小块——这是理想的选择array_chunk。<div id="category_case_holder">&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; $sql = "SELECT * FROM `users` WHERE `status` = 'active' AND `usertype` = 'advertiser'";&nbsp; &nbsp; &nbsp; &nbsp; $result = $conn->query( $sql )->fetch_all( MYSQLI_ASSOC );&nbsp; &nbsp; &nbsp; &nbsp; # a placeholder in the path will be substituted later using sprintf or printf&nbsp; &nbsp; &nbsp; &nbsp; $filepath = 'data/profile/%s/main/profile.jpg';&nbsp; &nbsp; &nbsp; &nbsp; if( !empty( $result ) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # split the recordset array into chunks - 6 records long.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Each chunk will be a row from recordset.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $chunks=array_chunk( $result, 6 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $chunks as $chunk ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $chunk as $i => $rs ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename=file_exists( sprintf( $filepath, $i ) ) ? sprintf( $filepath, $rs['user_id'] ) : sprintf( $filepath, '0' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ID attributes MUST be unique.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # substitue placeholders for values from this row data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="prime">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="profile.php?id=%s">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="%s" alt="Profile" height="100%" width="100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlspecialchars( $rs['user_id'] ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?></div>

繁星coding

您可以使用该array_chunk()方法轻松实现。例子://set it to whatever limit you want.$limit = 6; // this will divide the array into x number of chunks based on y limit.$chunks = array_chunk($result, limit);foreach($chunks as $chunk){   echo '<div>';   foreach($chunk as $chunkItem){      // your stuff here   }   echo '</div>';} 
打开App,查看更多内容
随时随地看视频慕课网APP