每隔3行MySQL表回调一个div

时间:2018-02-20 22:20:48

标签: php html

                    <?
                $conn = new mysqli($servername, $username, $password, $dbname);
                // Check connection
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                }

                $selection = "SELECT steamUsername, steamid64, twitter, discord, github FROM hnTeam";
                $result = $conn->query($selection);

                ?>

                <div class="card-group">

                <?

                if($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {  


                        /*if($result->num_rows === 1 || $result->num_rows % 3 === 0) {
                            echo "<div class='card-group'>";
                        }*/

                        echo "<div class='card'>";
                        echo "<img class='card-img-top' src='...' alt='Profile picture'>";
                        echo "<div class='card-body'>";
                        echo "<p class='card-text'>";
                        echo "<span id='social'><i class='fab fa-steam'></i> " . $row["steamUsername"] . "</span>" . "<br>";
                        echo "<span id='social'><i class='fab fa-discord'></i> " . $row["discord"] . "</span>" . "<br>";
                        echo "<span id='social'><i class='fab fa-twitter'></i> " . $row["twitter"] . "</span>" . "<br>";
                        echo "<span id='social'><i class='fab fa-github'></i> " . $row["github"] . "</span>" . "<br> <br>";
                        echo "</p>";
                        echo "</div>";
                        echo "</div>";

                        /*if($result->num_rows === 1 || $result->num_rows % 3 === 0) {
                            echo "</div>";
                        }*/
                    }
                }
                ?>

                </div>

                </div>

到目前为止我的代码。基本上,我需要做以下事情:

对于上下文,我将以下列为

                    echo "<div class='card'>";
                    echo "<img class='card-img-top' src='...' alt='Profile picture'>";
                    echo "<div class='card-body'>";
                    echo "<p class='card-text'>";
                    echo "<span id='social'><i class='fab fa-steam'></i> " . $row["steamUsername"] . "</span>" . "<br>";
                    echo "<span id='social'><i class='fab fa-discord'></i> " . $row["discord"] . "</span>" . "<br>";
                    echo "<span id='social'><i class='fab fa-twitter'></i> " . $row["twitter"] . "</span>" . "<br>";
                    echo "<span id='social'><i class='fab fa-github'></i> " . $row["github"] . "</span>" . "<br> <br>";
                    echo "</p>";
                    echo "</div>";
                    echo "</div>";

好的,所以我需要做的是,每3张需要放入这个div:<div class='card-group'>

我尝试了一些if语句,如代码块中所示,但无济于事。根据我的理解,我需要将它回显到一个div中,然后在每组3张卡中将其回显为div。我试图将if语句建立在$ result-&gt; num_rows上,但这不是程序性的,它不断显示为最终的卡数量,每次运行时都不会增加1。

我被困在这里,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你使用%运算符和$ result-&gt; num_rows,这是不正确的,你必须使用当前的迭代,所以我添加了一个$ c var。

if($result->num_rows > 0) {
    $c = 0;
    while($row = $result->fetch_assoc()) {  

        if ($c % 3 === 0) {
            if ($c > 0) {
                echo "</div>"; //Close before one
            }

            echo "<div class='card-group'>";
        }

        echo "<div class='card'>";
        echo "<img class='card-img-top' src='...' alt='Profile picture'>";
        echo "<div class='card-body'>";
        echo "<p class='card-text'>";
        echo "<span id='social'><i class='fab fa-steam'></i> " . $row["steamUsername"] . "</span>" . "<br>";
        echo "<span id='social'><i class='fab fa-discord'></i> " . $row["discord"] . "</span>" . "<br>";
        echo "<span id='social'><i class='fab fa-twitter'></i> " . $row["twitter"] . "</span>" . "<br>";
        echo "<span id='social'><i class='fab fa-github'></i> " . $row["github"] . "</span>" . "<br> <br>";
        echo "</p>";
        echo "</div>";
        echo "</div>";

        $c++;

        if ($c == $result->num_rows) {
            echo "</div>"; //Close last one
        }
    }
}