简体   繁体   中英

echo a specific set of rows from database

I'm a front end dev playing around with back end so bear with. I'm just creating simple app that displays the team line ups for a football match. I've created the tables in the database and can loop through all 22 players for a match. However, I only want to display the first 11 in one div and the second 11 in another.

Here's my code so far:

    <?php

try {

    $handler = new PDO('mysql:host=localhost;dbname=euro_missing_men','root','');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {
    echo $e->getMessage();
    die('Sorry, database issue');
}




$query = $handler->query('SELECT * FROM `game` g 
    JOIN team t on (g.id = t.game_id) 
    JOIN team_name tn on (t.team_id = tn.id)
    JOIN position p on (t.position_id = p.id)
');

$results = $query->fetch(PDO::FETCH_OBJ);

while($row = $query->fetch(PDO::FETCH_OBJ)) {
    echo $row->player, "<br/>";
}


?>

This produces the following:

McKimmie
Calderwood
Hendry
Boyd
McKinlay
McCall
McAllister
Collins
Spencer
Durie
Seaman
Neville
Adams
Southgate
Pearce
Anderton
Ince
Gascoigne
McManaman
Sheringham
Shearer

But I want to separate them. I understand there's probably a better way to do this but for the purpose of the exercise I'd like to echo the first 11 names in one div and the second 11 names in another.

Thanks.

// create a counter
$i = 0;

// echo the opening div
echo "<div>";
while($row = $query->fetch(PDO::FETCH_OBJ)) {
    echo $row->player, "<br/>";
    if ( $i++ == 11 ) echo "</div><div>"; // increment the counter and at the 11th iteration close the div and start a new one
}
echo "</div>";

This assumes you might have more than 22. If you only have 22 exactly, then @pamblam has the simpler answer.

If you wanted to divide them into groups of 11, you might use the modulus operator:

var $count = 1;
while($row = $query->fetch(PDO::FETCH_OBJ)) {
    // After every 11th row, echo a closing div / open a new div
    if ( ! ($count++ % 11) ) {
        echo '</div><div>';
    }

    // Don't use break tags, you can't style them well.
    echo '<p>' . $row->player . "</p>";
}

Or, another way would be to split up the results into arrays:

$results = $query->fetchAll();

$divs = array_chunk($results, 11);

foreach( $divs AS $div ) {
    echo '<div>';
    foreach ( $div AS $row ) {
        echo '<p>' . $row->player . '</p>';
    }
    echo '<div>';
}

Simple split the results? then loop each one?

$results = $query->fetchAll();

$team1 = array_slice($results, 0, 11);

$team2 = array_slice($results, 11);

as suggested array_chunck

$results = $query->fetchAll();

$teams = array_chunk($results, 11);

And to output this

$results = $query->fetchAll(PDO::FETCH_OBJ);

$teams = array_chunk($results, 11);

foreach($teams as $team) {
    echo '<div>';
    foreach($team as $player) {
        echo $player->name . '<br />';
    }
    echo '</div>';
}

You could always do something basic like this:

   <?php

try {

    $handler = new PDO('mysql:host=localhost;dbname=euro_missing_men','root','');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {
    echo $e->getMessage();
    die('Sorry, database issue');
}




$query = $handler->query('SELECT * FROM `game` g 
    JOIN team t on (g.id = t.game_id) 
    JOIN team_name tn on (t.team_id = tn.id)
    JOIN position p on (t.position_id = p.id)
');

$results = $query->fetch(PDO::FETCH_OBJ);
$upto = 0;
echo "<div>"; //Start the divs
while($row = $query->fetch(PDO::FETCH_OBJ)) {
    $upto ++; //add 1 to upto
    if($upto % 11 == 0) //if 11 results
        echo "</div><div>"; //create new div
    echo $row->player, "<br/>";

}
echo "</div>"; //close div


?>

You can add increment. And if $i <=11 put player in first div. In another way put them to second div.

    $results = $query->fetch(PDO::FETCH_OBJ);
    $i = 0;

    while($row = $query->fetch(PDO::FETCH_OBJ)) {
     $i++;
     if($i <= 11){
        echo "{$i} of 22".$row->player."<br/>";
     }
     else{
        echo "{$i} of 22".$row->player."<br/>";
     }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM