douhan4093 2016-06-05 13:25
浏览 99
已采纳

在PHP RecursiveIteratorIterator中重复第一个键值

I am generating a dynamic HTML table based on my DATABASE using the PHP RecursiveIteratorIterator function like this:

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>" . "
";
    } 
} 

The following foreach loop generates each new row and passes the data to the iterator above.

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

I execute the PDO prepared $stmt where the first value I pull from the database is always the ID of the row.

All this works just great. Now, the problem I have is that I want to generate an HTML name parameter on each <td> element in the current() function of the iterator. And I need this name parameter to be always the first key of the current RecursiveIteratorIterator cycle (which in my case would be the ID column value for each row).

Something like this, so I can have the ID of the row in each of its <td> elements:

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

EDIT: The whole 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>" . "
";
    } 
} 

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: The example table that I would like to have generated:

<!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>

I hope someone can shed some light for me on this matter.

  • 写回答

1条回答 默认 最新

  • dougu1990 2016-10-08 12:51
    关注

    Finally, with the help from a good old friend, I have the answer for my question and I just wanted to share it for anyone else that may encounter the same problem. The provided solution is an if statement into the current() RecursiveIteratorIterator function, which checks if the parent::key is the one you need, and if it is PHP will assign the parent::current() value to $this->id. This way you can fetch the values later in the return function, like in the sample code below.

    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>";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。