2016-06-05 5 views
0

ich eine dynamische HTML-Tabelle basierend auf der Datenbank Erzeugen der PHP RecursiveIteratorIterator Funktion wie diese unter Verwendung von:Repeat erster Schlüsselwert in PHP RecursiveIteratorIterator

class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
     parent::__construct($it, self::LEAVES_ONLY); 
    } 

    function current() { 
     return "<td>".parent::current()."</td>"; 
    } 

    function beginChildren() { 
     echo "<tr>"; 
    } 

    function endChildren() { 
     echo "</tr>" . "\n"; 
    } 
} 

Die folgenden foreach Schleife erzeugt jede neue Zeile und übergibt die Daten an den Iterator oben.

foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
    echo $v; 
} 

führe ich die PDO $stmt hergestellt, wobei der erste Wert I aus der Datenbank ziehen ist immer die ID der Zeile.

Das alles funktioniert einfach großartig. Nun, das Problem, das ich habe, ist, dass ich einen HTML name Parameter für jedes <td> Element in der current() Funktion des Iterators generieren möchte. Und ich brauche diesen name Parameter, um immer der erste key des aktuellen RecursiveIteratorIterator-Zyklus zu sein (was in meinem Fall der ID-Spaltenwert für jede Zeile wäre).

So etwas wie dies, so kann ich die ID des row in jedem seiner <td> Elemente:

function current() { 
    return "<td name='".***FIRST ITERATOR KEY VALUE HERE***."'>".parent::current()."</td>"; 
} 

EDIT: Der gesamte Code

class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
     parent::__construct($it, self::LEAVES_ONLY); 
    } 

    function current() { 
     /* Here I need to add the value from the ID column to the <td name="... parameter */ 
     return "<td name='".***FIRST ITERATOR KEY VALUE HERE***."'>".parent::current()."</td>"; 
    } 

    function beginChildren() { 
     echo "<tr>"; 
    } 

    function endChildren() { 
     echo "</tr>" . "\n"; 
    } 
} 

try 
{ 
    $conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $stmt = $conn->prepare("SELECT 
            id, 
            ddate, 
            day, 
            month, 
            year, 
            arrival, 
            departure, 
            serial_number, 
            time_mark, 
            observations 
            FROM ROUTS WHERE ddate = '".$datecode."'"); 
    $stmt->execute(); 
    // set the resulting array to associative 
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
     echo $v; 
    } 
} 

catch(PDOException $e) 
{ 
    echo "Error: " . $e->getMessage(); 
} 
$conn = null; 

EDIT: Die Beispieltabelle, die ich gerne generiert hätte:

<!DOCTYPE html> 
<html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="table-styles.css"> 
     <title>Register</title> 
    </head> 
    <body> 
     <form method='post' onsubmit="return confirm('Are you sure you want to submit?');"> 
      <table id='table-left'> 
       <caption>11/22/33</caption> 
       <caption>ROUTS</caption> 
       <tr> 
        <th>ID</th> 
        <th>Date</th> 
        <th>Day</th> 
        <th>Month</th> 
        <th>Year</th> 
        <th>Arrival</th> 
        <th>Departure</th> 
        <th>S/N</th> 
        <th>Time Mark</th> 
        <th>Observation</th> 
       </tr> 
       <tr> 
        <td name="id-1">1</td> <!-- The value of this <td> tag needs to populate in the name of each next <td> 'name' parameter for this row!--> 
        <td name="ddate-1">03062016</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="day-1">Day</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="month-1">Month</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="year-1">Year</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="arrival-1">Arrival</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="departure-1">Departure</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="sn-1">S/N</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="time-mark-1">Time Mark</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
        <td name="observation-1">Observation</td> <!-- Here I want to add this number "1" to the name of the <td> element.--> 
       </tr> 
       <tr> 
        <td name="id-2">2</td> <!-- The value of this <td> tag needs to populate in the name of each next <td> 'name' parameter for this row!--> 
        <td name="ddate-2">03062016</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="day-2">Day</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="month-2">Month</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="year-2">Year</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="arrival-2">Arrival</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="departure-2">Departure</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="sn-2">S/N</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="time-mark-2">Time Mark</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
        <td name="observation-2">Observation</td> <!-- Here I want to add this number "2" to the name of the <td> element.--> 
       </tr> 
       <tr> 
        <td name="id-3">3</td> <!-- The value of this <td> tag needs to populate in the name of each next <td> 'name' parameter for this row!--> 
        <td name="ddate-3">03062016</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="day-3">Day</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="month-3">Month</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="year-3">Year</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="arrival-3">Arrival</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="departure-3">Departure</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="sn-3">S/N</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="time-mark-3">Time Mark</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
        <td name="observation-3">Observation</td> <!-- Here I want to add this number "3" to the name of the <td> element.--> 
       </tr> 
       <tr> 
        <td><input class='hdl' type='text' name='id' value=''></input></td> 
        <td><input class='hdl' type='text' name='date' value=''></input></td> 
        <td><input class='hds' type='text' name='day' value=''></input></td> 
        <td><input class='rm' type='text' name='month' value=''></input></td> 
        <td><input class='hm' type='text' name='year' value=''></input></td> 
        <td><input class='ob' type='text' name='arr' value=''></input></td> 
        <td><input class='pre' type='text' name='dep' value=''></input></td> 
        <td><input class='prs' type='text' name='sn' value=''></input></td> 
        <td><input class='vol' type='text' name='tm' value=''></input></td> 
        <td><input class='obs' type='text' name='ob' value=''></input></td> 
       </tr> 
      </table> 
      <input id='submit' type='submit' name='submit' value='Submit' /> 
     </form> 
    </body> 
</html> 

Ich hoffe, jemand kann etwas Licht für mich in dieser Sache werfen.

+0

Und 'RecursiveIteratorIterator :: key()' funktioniert nicht? – dbf

+0

Es funktioniert, aber es gibt den Namen der Spalte für jeden Wert zurück: /. Ich brauche es immer als ersten Schlüssel des aktuellen Zyklus. – Logixor

+0

Ich habe den Test so gemacht: 'function current() { \t Rückgabe" ". Parent :: current()." "; \t} ' – Logixor

Antwort

1

Schließlich, mit der Hilfe von einem guten alten Freund, habe ich die Antwort für meine Frage und ich wollte es nur für alle anderen, die das gleiche Problem auftreten können, teilen. Die bereitgestellte Lösung ist eine if Anweisung in die current()RecursiveIteratorIterator Funktion, die prüft, ob der parent::key derjenige ist, was Sie brauchen, und wenn es wird PHP zuweisen den parent::current() Wert $this->id. Auf diese Weise können Sie die Werte später in der Funktion return wie im folgenden Beispielcode abrufen.

function current() { 
    if(parent::key() == 'id') { /* Check if parent::key is what you are looking for */ 
     $this->id = parent::current(); /* Assign parent::current to $this->id if true */ 
    } 
     return "<td class='".parent::key()."'>".parent::current()."</td>"; 
    } 
Verwandte Themen