带内联PHP的HTML表 [英] HTML Table with inline PHP

查看:53
本文介绍了带内联PHP的HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个HTML表,但是这些行应该从mysql数据库中的值生成.问题是我想要一个布尔框,用户可以在其中标记它,然后按一个按钮以更新数据库中的表.我该怎么做?

I want to create a HTML Table, but the rows should be generated from the values from a mysql database. The problem is that I want to have a boolean box where the user can mark it, and then press a button to update the table in the database. How do I do such a thing ?

到目前为止的代码:

<?php 
session_start();
require("connectToEvent_log.php");
$connectToEvent = connect2db();
$uid = '2'; // for the filnal version: @$_SESSION['uid'];

$view_event = "SELECT * FROM event_log WHERE uid = $uid";
$view_event_query = mysqli_query($connectToEvent, $view_event);
$row = mysqli_num_rows($view_event_query);
$print = mysqli_fetch_array($view_event_query);
?>

<html>
<head>
<title>Events</title>
</head>
    <body>
        <form action="viewEvents.php" method="POST">
                <table border = '1'>
                    <tr> 
                    <?php
                        while($row != 0){
                        echo "<td>".$print['hours']."</td>";
                        echo "<td>".$print['date']."</td>";
                    }
                    ?>
                    </tr>
                </table>
        <form/>
        </form>
    </body>
</html>

推荐答案

您可以轻松地遍历mysqli_fetch_array函数的结果以创建表行.创建复选框标记很容易完成,我假设表具有主键id,并且存储复选框值(0或1)的列称为checkbox.

You can easily iterate over the result from the mysqli_fetch_array function to create the table rows. Creating a checkbox markup is done easily, i assume that the table has a primary key id and the column, that stores the checkbox value (0 or 1) is called checkbox.

<?php 
session_start();
require("connectToEvent_log.php");
$connectToEvent = connect2db();
$uid = '2'; // for the filnal version: @$_SESSION['uid'];

$view_event = "SELECT * FROM event_log WHERE uid = $uid";
$view_event_query = mysqli_query($connectToEvent, $view_event);
$num_rows = mysqli_num_rows($view_event_query);
$rows = mysqli_fetch_array($view_event_query);

?>

<html>
<head>
    <title>Events</title>
</head>
    <body>
        <form action="viewEvents.php" method="POST">
            <table border="1">
                <thead>
                    <tr>
                        <td>Id</td>
                        <td>Date</td>
                        <td>Hours</td>
                        <td>Checkbox</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    for ($i = 0; $i < count($num_rows); $i++) {
                    ?>
                        <tr>
                            <td><?php print $rows[$i]["eid"]; ?></td>
                            <td><?php print $rows[$i]["date"]; ?></td>
                            <td><?php print $rows[$i]["hours"]; ?></td>
                            <td><input type="checkbox" name="row[<?php $rows[$i]["eid"]?>][checkbox]" value="1" <?php if ($rows[$i]["accepted"]) print ' checked="checked"'; ?>/></td>
                        </tr>
                    <?php
                    }
                    ?>
                </tbody>
            </table>
            <input type="submit" />
        </form>
    </body>
</html>

这篇关于带内联PHP的HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆