如何限制PHP中显示的数量?

时间:2015-04-24 08:39:01

标签: php

现在我在PHP中显示表格,但我不想这样做,我想将视图限制为主要是HTML和少量的PHP。有什么好办法吗?

有没有办法做到这一点?

   echo"<table border='0' class='content-display'>
        <tr>
        <th class='table-header'>Name</th>
        <th class='table-header'>Date</th>
        <th class='table-header'>Protected</th>
        <th class='table-header'>Final</th>
        <th class='table-header'>Make New</th>
       </tr>";


                    while ($row = mysqli_fetch_assoc($Result)){

                        $name= $row['name'];
                        $date= $row['date'];
                        $pro= $row['protected'];
                        $fianl= $row['final'];


                        echo"<tr>
                            <td>
                                $name
                            </td>
                            <td>
                                $date
                            </td>
                            <td>
                                $pro
                            </td>
                            <form method='post' action='check.php'>
                            <td>

                                    <input type='text' name='viewkey' value='$name' hidden >

                                    <button class='main-buttons' type='submit'>Create Time Entry</button>

                            </td>
                            </form> 
                            <form method='post' action='check.php'>
                            <td>

                                    <input type='text' name='viewkey' value='1' hidden >
                                    <input type='text' name='proname' value='2' hidden >
                                    <button class='main-buttons' type='submit'>View Checks</button>


                            </td>
                            </form>
                        </tr>";

                    }


        echo"</table>";

3 个答案:

答案 0 :(得分:0)

您不必将所有HTML都包含在echo语句中。 您可以根据需要关闭和打开php标签。这使代码更容易阅读,混合更好。

考虑以下文件代码与您的相比:

 <table border='0' class='content-display'>
    <tr>
        <th class='table-header'>Name</th>
        <th class='table-header'>Date</th>
        <th class='table-header'>Protected</th>
        <th class='table-header'>Final</th>
        <th class='table-header'>Make New</th>
    </tr>

    <?php
    while($row = mysqli_fetch_assoc($Result)):

        $name= $row['name'];
        $date= $row['date'];
        $pro= $row['protected'];
        $fianl= $row['final'];
                                            ?>
        <tr>
            <td><?php echo $name; ?></td>
            <td><?php echo $date; ?></td>
            <td><?php echo $pro; ?></td>
            <td>
                <form method='post' action='check.php'>
                    <input type='text' name='viewkey' value='<?php echo $name; ?>' hidden >
                    <button class='main-buttons' type='submit'>Create Time Entry</button>
                </form>
            </td>
            <td>
                <form method='post' action='check.php'>
                    <input type='text' name='viewkey' value='1' hidden >
                    <input type='text' name='proname' value='2' hidden >
                    <button class='main-buttons' type='submit'>View Checks</button>
                </form>
            </td>
        </tr>
    <?php endwhile; ?>
</table>

答案 1 :(得分:0)

据我了解你的问题,我认为你想要这样的事情:

<table border='0' class='content-display'>
    <tr>
        <th class='table-header'>Name</th>
        <th class='table-header'>Date</th>
        <th class='table-header'>Protected</th>
        <th class='table-header'>Final</th>
        <th class='table-header'>Make New</th>
    </tr>

    <?php
    while ($row = mysqli_fetch_assoc($Result)) {
        $name  = $row['name'];
        $date  = $row['date'];
        $pro   = $row['protected'];
        $fianl = $row['final'];
        ?>

        <tr>
            <td><?php echo $name; ?></td>
            <td><?php echo $date; ?></td>
            <td><?php echo $pro; ?></td>
            <td>
                <form method='post' action='check.php'>
                    <input type='text' name='viewkey' value='<?php echo $name; ?>' hidden >
                    <button class='main-buttons' type='submit'>Create Time Entry</button>
                </form> 
            </td>
            <td>
                <form method='post' action='check.php'>
                    <input type='text' name='viewkey' value='1' hidden >
                    <input type='text' name='proname' value='2' hidden >
                    <button class='main-buttons' type='submit'>View Checks</button>
                </form>
            </td>
        </tr>
        <?php
    }
    ?>
</table>

但你应该考虑到Seer的评论。

答案 2 :(得分:0)

Just separate the php code and HTML code as 
table border='0' class='content-display'>
<tr>
    <th class='table-header'>Name</th>
    <th class='table-header'>Date</th>
    <th class='table-header'>Protected</th>
    <th class='table-header'>Final</th>
    <th class='table-header'>Make New</th>
</tr>

<?php
while ($row = mysqli_fetch_assoc($Result)) {
    $name  = $row['name'];
    $date  = $row['date'];
    $pro   = $row['protected'];
    $fianl = $row['final'];
    ?>

    <tr>
        <td><?php echo $name; ?></td>
        <td><?php echo $date; ?></td>
        <td><?php echo $pro; ?></td>
        <td>
            <form method='post' action='check.php'>
                <input type='text' name='viewkey' value='<?php echo $name; ?>' hidden >
                <button class='main-buttons' type='submit'>Create Time Entry</button>
            </form> 
        </td>
        <td>
            <form method='post' action='check.php'>
                <input type='text' name='viewkey' value='1' hidden >
                <input type='text' name='proname' value='2' hidden >
                <button class='main-buttons' type='submit'>View Checks</button>
            </form>
        </td>
    </tr>
    <?php
}
?>