猿问

获取所有结果 MYSQL 表 - 每行 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 个结果/配置文件。如果没有足够的结果/个人资料来完成一行,那么我会尝试使用广告模板“在此处宣传您的个人资料”来填充剩余的不存在的个人资料。


广告模板图片存放在以下目录:


<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.

这是我目前拥有的代码。


   <?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){




        $i = htmlspecialchars($chunkItem['user_id']);

        $filename = "data/profile/$i/main/profile.jpg"; 

        if (file_exists($filename)) {

        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>';

        }else{

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


        } }


        echo '</div>';


        } ?> 

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


慕少森
浏览 90回答 1
1回答

ibeautiful

我正在复制您的完整代码,因为我更改了一些格式,这使代码更具可读性。插入的代码段使用变量$nrProfilesOnScreen您可能需要更改包含“??????”的行 ....😉<?php&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $conn = new mysqli("localhost", "root", "******", "test");&nbsp; &nbsp; $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";&nbsp; &nbsp; $result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);&nbsp; &nbsp; $limit = 6;&nbsp;&nbsp; &nbsp; $chunks = array_chunk($result, $limit);&nbsp; &nbsp; $nrProfilesOnScreen = 0;&nbsp; &nbsp; foreach($chunks as $chunk){&nbsp; &nbsp; &nbsp; &nbsp; echo '<div id="category_case_holder">';&nbsp; &nbsp; &nbsp; &nbsp; foreach($chunk as $chunkItem){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i = htmlspecialchars($chunkItem['user_id']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename = "data/profile/$i/main/profile.jpg";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (file_exists($filename)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($chunkItem['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;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nrProfilesOnScreen++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (; $nrProfilesOnScreen % $limit !=0; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div id="prime"><a href="profile.php?id='.'?????'.'"><img src="data/profile/0/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>' . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nrProfilesOnScreen++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;echo '</div>';&nbsp; &nbsp; }?>&nbsp;运算%符在这里解释:https ://www.php.net/manual/en/language.operators.arithmetic.php这for (;$nrProfilesOnScreen % $limit;) { } 可能看起来很奇怪,它是while ($nrProfilesOnScreen % $limit !=0) {}
随时随地看视频慕课网APP
我要回答