PHP MySQL - For every 6 rows insert a div?

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

PHP:

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

//
3条回答
  •  野趣味
    野趣味 (楼主)
    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 "
    "; // if counter does not equal zero, close previous group } echo "
    "; // start new group } echo "
    "; echo ""; echo "
    "; ++$counter; // increment counter } echo "
    "; // always close the final group }

    Output:

    
    
    

提交回复
热议问题