Home > Software design >  How can i create a new section with only 5 elements in PHP?
How can i create a new section with only 5 elements in PHP?

Time:09-11

i have a PHP code that creates a team page with team member boxes on my website. The code for every team meber is fetched from the database, and it works fine, but time has passed and we are many now. So many, that I need to display only 5 team members per section. Like 5 team members in one line, the next 5 in the other line, .. - I tried a few things to do that, but I got stuck. Sometimes members were missing, sometimes he created many lines with the same members, and so on. That's why I'm asking here. I already searched a bit but couldn't find anything that helps me.

Currently, my code creates a new <section> for ALL team members. But that's not what I need. It should create a section for 5 members, the next section for the next 5 members, and so on. How can I do that?

(btw I'm a complete beginner in PHP and want to learn something from it so you can improve my code if you want.)

function team_helfer() {
    $db = new mysqli("ip", $username, $password, $database);
    $result = mysqli_query($db, "SELECT code FROM team_data WHERE rank = 'HELFER'");
        
    $team_list = "";
    $count = 0;
    while ($row = mysqli_fetch_assoc($result)) {
        foreach ($row as $field => $value) {
            $team_list .= "$value";
              $count;
        }
    }
    
    if ($count == 2) {
        
        $code = '<section  data-element_type="section">
                        <div  style="left: 17%;">
                        '.$team_list.'
                    </div>
        </section>';
        
    } elseif ($count == 1) {
        $code = '<section  data-element_type="section">
                            <div  style="left: 33%;">
                            '.$team_list.'
                        </div>
            </section>';
    } else {
        $code = '<section  data-element_type="section">
                            <div >
                            '.$team_list.'
                        </div>
            </section>';
    }
    
    mysqli_free_result($result);
    $db->close();
    return $code;
}
add_shortcode( 'team_helfer', 'team_helfer' );

Code from HTMHell's answer:```php

    function team_helfer() {
    $db = new mysqli("ip", $username, $password, $database);
    $result = mysqli_query($db, "SELECT code FROM team_data WHERE rank = 'HELFER'");

    $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
    $sections = array_chunk($rows, 5);

    foreach ($sections as $section) {
    $teamList = implode(",", array_map(function($row) {
        return $row['code'];
    }));
    $code = '<section  data-element_type="section">
    <div >'.$teamList.'</div></section>';
    }
    
    mysqli_free_result($result);
    $db->close();
    return $code;```

Sadly that doesn't help, all elements are vanished now, i can't see them.

CodePudding user response:

I think you are looking for array_chunk.

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$sections = array_chunk($rows, 5);

foreach ($sections as $section) {
    $teamList = implode(",", array_map(function($row) {
        return $row['code'];
    }, $section));
    echo "<section>$teamList</section>";
}

Here's a live example: https://3v4l.org/mXRtV

  • Related