【问题标题】:How to add a button after each row of a table from sql?如何在sql表的每一行之后添加一个按钮?
【发布时间】:2015-02-11 15:12:24
【问题描述】:

我有一个包含三列 date 、 title 、 message 的表格。

我将在每个表格中仅显示日期和标题,并希望在每行之后添加一个单独的按钮,然后我将显示该消息。

我试过了

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

        } 

        $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>";

        ?>

我得到的结果只是一个按钮,它也在表格的顶部。

【问题讨论】:

  • 我试过了,它在顶部每行显示 2 个按钮。
  • 在“每个表”中还是在每行中?
  • 每行之后的按钮,当单击任何按钮时,我想显示列消息中的数据

标签: php mysql button


【解决方案1】:

&lt;input&gt; 需要在 &lt;td&gt; 内,而 &lt;td&gt; 需要在 &lt;tr&gt;

HTML 表格中任何不在行和单元格中的内容都会呈现在表格之外。

要为每一行设置一个按钮,您需要将代码放入 TableRows 循环中。我建议编辑endChildren()函数,如下:

function endChildren() {
    echo '<td><input type="button" value = "View"/></td>';
    echo "</tr>" . "\n";
} 

【讨论】:

  • 感谢它的工作,现在对于每个单击的视图按钮,我需要在我的数据库的消息列中显示数据。
猜你喜欢
  • 2012-05-04
  • 1970-01-01
  • 2021-01-16
  • 2021-01-27
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
相关资源
最近更新 更多