PHP MySQL - For every 6 rows insert a div?

后端 未结 3 864
忘了有多久
忘了有多久 2021-01-29 12:28

PHP:

prepare(\"SELECT * FROM `users`\");
$stmt->execute();
$result = $stmt->get_result();

//
相关标签:
3条回答
  • 2021-01-29 12:38

    I recommend you check this http://php.net/manual/en/function.array-chunk.php

    Example:

    $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>";
    }
    
    0 讨论(0)
  • 2021-01-29 12:50

    See my inline comments for how my answer works.

    Also, you don't really need a prepared statement for your case and you should only include the columns that you intend to use in your SELECT clause.

    Code: (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
    }
    

    Output:

    <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>
    
    0 讨论(0)
  • 2021-01-29 12:55

    I will reference this question since this is what you need: checking if a number is divisible by 6 PHP

    Your logic you should basically be: If a number is divisible by 6 then do something otherwise do something else.

    if ($row_number % 6 == 0) {
        // Row number is 6, 12, 18 and so on
    } else {
        // Other rows
    }
    
    0 讨论(0)
提交回复
热议问题