PHP MySQL - 每6行插入一个div?

时间:2018-01-28 23:26:14

标签: php mysql

PHP:

<?php
$stmt = $db->prepare("SELECT * FROM `users`");
$stmt->execute();
$result = $stmt->get_result();

//<div class="ui stackable six cards">

if($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    echo $one[] = "image div".$row["avatar"]."image div";
    echo $two[] = "content div".$row["username"]."content div";
  }
  echo "</table>";
}
$stmt->close();
?>

HTML:

<div class="ui center aligned stackable grid container" style="padding: 4em !important;">
                <div class="center aligned row">
                    <div class="column">
                        <h2 class="header">Members </h2>
                    </div>
                </div>

                <div class="ui stackable six cards">
                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>

                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>
                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>
                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>

                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>

                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>
                </div>

                <div class="ui stackable six cards">
                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>

                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>
                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>
                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>
                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>

                    <div class="ui card small">
                        <div class="image">
                            <img src="assets/img/players/.jpg">
                        </div>

                        <div class="content">
                            <a class="header"></a>
                        </div>
                    </div>
                </div>
            </div>

基本上我需要弄清楚的是,我究竟会得到什么

//<div class="ui stackable six cards">

每次达到6行时回显!这可能吗?

我假设我会使用foreach循环?

我需要做的是回复数据库中的头像和用户名并将其放在我的HTML代码中,我可以做得很好。

但问题是我不知道如何每6行(以及最后一行)添加此代码

P.S。我不会说英语,所以我真的希望你明白我的意思!

3 个答案:

答案 0 :(得分:0)

我会参考这个问题,因为这是你需要的: checking if a number is divisible by 6 PHP

你的逻辑基本上应该是:如果一个数字可以被6整除,那么做一些其他事情做其他事情。

if ($row_number % 6 == 0) {
    // Row number is 6, 12, 18 and so on
} else {
    // Other rows
}

答案 1 :(得分:0)

我建议你查看http://php.net/manual/en/function.array-chunk.php

示例:

$rows = array_chunk($yourArrayfetch, '3'); // 3 = column count;
foreach ($rows as $columns) {
    echo "<div class='row'>";
    foreach ($columns as $column) { 
        echo "<div class='column'>$column</div>"; 
    }
    echo "</div>";
}

答案 2 :(得分:0)

请参阅我的内联评论,了解我的答案是如何运作的。

此外,您并非真的需要为您的案例准备好的声明,并且您应该只包括您打算在SELECT子句中使用的列。

代码:(Demo

$resultset=[
    ['avatar'=>1,'username'=>'A'],
    ['avatar'=>2,'username'=>'B'],
    ['avatar'=>3,'username'=>'C'],
    ['avatar'=>4,'username'=>'D'],
    ['avatar'=>5,'username'=>'E'],
    ['avatar'=>6,'username'=>'F'],
    ['avatar'=>7,'username'=>'G']
];
if(sizeof($resultset)){  // your num rows
    $counter=0;  // initialize the counter
    foreach($resultset as $row){ // you while loop
        if($counter%6==0){  // see if counter is evenly divisible by 6
            if($counter){
                echo "</div>";  // if counter does not equal zero, close previous group
            }
            echo "<div class='ui stackable six cards'>";  // start new group
        }
        echo "<div class='ui card small'>";
            echo "<div class='ui card small'><div class='image'><img src='".$row["avatar"]."'></div>";
            echo "<div class='content'><a class='header'>Dias".$row["username"]."</a></div></div>";
        echo "</div>";
        ++$counter;  // increment counter
    }
    echo "</div>";  // always close the final group
}

输出:

<div class='ui stackable six cards'>
    <div class='ui card small'>
        <div class='ui card small'><div class='image'><img src='1'></div>
        <div class='content'><a class='header'>DiasA</a></div></div>
    </div>
    <div class='ui card small'>
        <div class='ui card small'><div class='image'><img src='2'></div>
        <div class='content'><a class='header'>DiasB</a></div></div>
    </div>
    <div class='ui card small'>
        <div class='ui card small'><div class='image'><img src='3'></div>
        <div class='content'><a class='header'>DiasC</a></div></div>
    </div>
    <div class='ui card small'>
        <div class='ui card small'><div class='image'><img src='4'></div>
        <div class='content'><a class='header'>DiasD</a></div></div>
    </div>
    <div class='ui card small'>
        <div class='ui card small'><div class='image'><img src='5'></div>
        <div class='content'><a class='header'>DiasE</a></div></div>
    </div>
    <div class='ui card small'>
        <div class='ui card small'><div class='image'><img src='6'></div>
        <div class='content'><a class='header'>DiasF</a></div></div>
    </div>
</div>
<div class='ui stackable six cards'>
    <div class='ui card small'>
        <div class='ui card small'><div class='image'><img src='7'></div>
        <div class='content'><a class='header'>DiasG</a></div></div>
    </div>
</div>