如何将查询结果放在正确的视图/表中

时间:2018-03-04 14:24:41

标签: php

您好,我有一个查询显示用户输入数据的结果,但我想将其显示在带有标题的漂亮表格中。查询按以下顺序显示具有值的记录“id”,“name”,“start”,“end”。我不知道如何在眼睛上创建一个漂亮的表而不是数据js

<body style="background-image: url(427.jpg); background-repeat: no-repeat; background-size: cover;">
     <div id="header"  style="background-image: url(background.jpg); background-repeat: no-repeat; background-size: cover;">
         <img src="slot365.png"; height=220 width="470">
     </div>

<?php
    // make sure the form has actually been submitted
    // and its not just been loaded from a click on a link etc
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if ( isset($_POST['start'], $_POST['end']) ) {

            $db = new PDO('sqlite:daypilot.sqlite');

            $sql = "SELECT * FROM events WHERE start > ? AND end < ?"; 
            $stmt = $db->prepare($sql); 
            $stmt->execute([$_POST['start'], $_POST['end']]); 
            $events = $stmt->fetchAll(PDO::FETCH_ASSOC); 
            $table = '<table>';
            foreach ($events as $event) {
                $table .= '<tr>';
                $table .= '<td>' . $event['id'] . '</td>';
                $table .= '<td>' . $event['name'] . '</td>';
                $table .= '<td>' . $event['start'] . '</td>';
                $table .= '<td>' . $event['end'] . '</td>';
                $table .= '</tr>';
            }
            $table .= '</table>';
            echo $table;
        }
    }
?>
    <form method="POST">
        1st DATE: <input type="text" name="start" /><br />
        2nd DATE: <input type="text" name="end" /> 
        <input type="submit" name="submit" />
    </form>
</body>

1 个答案:

答案 0 :(得分:1)

表格标题可以像在表格顶部添加另一行一样简单

<body style="background-image: url(427.jpg); background-repeat: no-repeat; background-size: cover;">
    <div id="header"  style="background-image: url(background.jpg); background-repeat: no-repeat; background-size: cover;">

        <img src="slot365.png"; height=220 width="470">

    </div>

<?php
    // make sure the form has actually been submitted
    // and its not just been loaded from a click on a link etc
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if ( isset($_POST['start'], $_POST['end']) ) {

            $db = new PDO('sqlite:daypilot.sqlite');

            $sql = "SELECT * FROM events WHERE start > ? AND end < ?"; 
            $stmt = $db->prepare($sql); 
            $stmt->execute([$_POST['start'], $_POST['end']]); 
            $events = $stmt->fetchAll(PDO::FETCH_ASSOC); 
            $table = '<table>';

            // add a table heading
            $table .= '<thead>';
            $table .= '<tr>';
            $table .= '<th>ID</td>';
            $table .= '<th>Name</td>';
            $table .= '<th>Start Date</td>';
            $table .= '<th>End Date</td>';
            $table .= '</tr>';
            $table .= '</thead>';
            $table .= '<tbody>';

            foreach ($events as $event) {
                $table .= '<tr>';
                $table .= '<td>' . $event['id'] . '</td>';
                $table .= '<td>' . $event['name'] . '</td>';
                $table .= '<td>' . $event['start'] . '</td>';
                $table .= '<td>' . $event['end'] . '</td>';
                $table .= '</tr>';
            }

            $table .= '</tbody>';
            $table .= '</table>';

            echo $table;
        }
    }
?>
    <form method="POST">
        1st DATE: <input type="text" name="start" /><br />
        2nd DATE: <input type="text" name="end" /> 
        <input type="submit" name="submit" />
    </form>
</body>