Welche Zweck haben Iteratoren-Klassen und was machen diese Klassen anders oder besser als eine Schleife?
Wie setz man diese sinnvoll ein?
Wie setz man diese sinnvoll ein?
<?php
/**
* @author kaminbausatz
* 2017-07-19
*/
class myTable extends RecursiveIteratorIterator {
public $myList = '';
public $colCount = 1;
public $playerCols = 4;
public function beginIteration(){
$this->myList .= '<table>';
}
public function endIteration(){
$this->myList .= '</table>';
}
public function beginChildren() {
if ($this->getDepth()==2)
$this->colCount=1;
}
public function endChildren() {
if ($this->getDepth()==2){
$this->myList.= str_repeat('<td>', $this->playerCols-$this->colCount+1);
}
}
public function nextElement() {
if ($this->getDepth()==0)
$this->myList .= '<tr><td colspan="'.($this->playerCols+1).'" class="fat">'.$this->key();
if ($this->getDepth()==1)
$this->myList .= '<tr><td class="kursiv">'.$this->key();
if ($this->getDepth()==2){
$this->myList .= '<td>'.$this->current();
if ($this->colCount%$this->playerCols==0){
$this->colCount=0;
$this->myList .= '<tr><td>';
}
$this->colCount++;
}
}
}
$sqlCreate = <<<ENDSQL
CREATE TABLE spieler (id INTEGER PRIMARY KEY, ort TEXT, team TEXT, spieler TEXT);
INSERT INTO spieler (ort, team, spieler) VALUES
('Berlin', 'Team A', 'Thomas'), ('Hamburg', 'Team B', 'Peter'), ('Hamburg', 'Team B', 'Hans'),
('Hamburg', 'Team B', 'Andreas'),('Hamburg', 'Team C', 'Hans'),('Frankfurt', 'Team D', 'Ben'),
('Frankfurt', 'Team D', 'Paul'),('Frankfurt', 'Team D', 'Jonas'),('Frankfurt', 'Team D', 'Felix'),
('Frankfurt', 'Team D', 'Emil'),('Frankfurt', 'Team D', 'Max');
ENDSQL;
$memDB = new PDO('sqlite::memory:');
$memDB->exec($sqlCreate);
$result = $memDB->query('SELECT * FROM spieler ORDER BY team, spieler');
foreach($result as $row){
$tableArray[$row['ort']][$row['team']][] = $row['spieler'];
}
$myTableHTML = new myTable(new RecursiveArrayIterator($tableArray), RecursiveIteratorIterator::SELF_FIRST);
foreach($myTableHTML as $key => $list_element);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
table {
border-collapse: collapse;
font-family: "Calibri", sans-serif;
}
table td {
border: 1px solid #ccc;
width: 100px;
}
.fat{
font-weight:bold;
}
.kursiv{
font-style: italic;
}
</style>
</head>
<body>
<?php echo $myTableHTML->myList; ?>
</body>
</html>
<table> <tbody> {% for city in cities %} <tr> <th class='city-label' colspan='5'> {{ city.name }} </th> </tr> {% for team in teamsByCity[city.id] %} {% set totalPlayers = playersByTeam[team.id] | length %} <tr> <td class='team-label'> {{ team.name }} </td> {% for player in playersByTeam[team.id] %} <td> {{ player }} </td> <!-- break rows after every 4th player --> {% if loop.index != totalPlayers and (loop.index + 1) % 5 == 0 %} </tr><tr><td></td> {% endif %} {% endfor %} <!-- fill up with empty cells --> {% if totalPlayers % 4 != 0 %} {% for i in range(1, 4 - (totalPlayers % 4)) %} <td></td> {% endfor %} {% endif %} </tr> {% endfor %} {% endfor %} </tbody> </table>
Kommentar