duanping6698 2015-02-11 15:12
浏览 47
已采纳

如何在sql的每一行表后添加一个按钮?

i have a table with three columns date , title , message.

i will show only date and title in each table and want to add a seperate button after each row then i will show that message .

i have tried

            <?php

        echo "<table style='border: solid 3px aqua;'>";
        echo "<tr><th>Circular/Notice</th><th>DateGenerated</th></tr>";
        class TableRows extends RecursiveIteratorIterator { 
            function __construct($it) { 
                parent::__construct($it, self::LEAVES_ONLY); 
            }
         function current() {
                return "<td style='width:150px;border:1px solid red;'>" . parent::current(). "</td>";
            }

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

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

        } 

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "er";

        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $conn->prepare("SELECT title ,DateGenerated FROM messages"); 
            $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;
        echo '<input type ="button" value = "View"/>';
        echo "</table>";

        ?>

and the result am getting is only one button that too at top of the table. enter image description here

  • 写回答

1条回答 默认 最新

  • douti9253 2015-02-11 15:23
    关注

    The <input> needs to be inside a <td>, which needs to be inside a <tr>

    Anything inside a HTML table that isn't in a row and cell, gets rendered outside the table.

    To have a button for each row, you need to put the code inside your TableRows loop. I would suggest editing the endChildren() function, as follows:

    function endChildren() {
        echo '<td><input type="button" value = "View"/></td>';
        echo "</tr>" . "
    ";
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料