Lasse
Lasse

Reputation: 601

HTML Table with inline PHP

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 ?

Code so far:

<?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>

Upvotes: 0

Views: 714

Answers (1)

Ondřej Hlav&#225;ček
Ondřej Hlav&#225;ček

Reputation: 506

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>

Upvotes: 1

Related Questions