0

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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lasse
  • 597
  • 2
  • 10
  • 33
  • You're going to nee an HTML form (with checkboxes), and some additional PHP code to store the values passed back from the HTML form. – DragonYen May 15 '15 at 14:49
  • Yeah i know that, but i only want the checkboxes to generate, when there is a row from the database – Lasse May 15 '15 at 14:51

1 Answers1

1

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>
  • That code looks wary promising, but when i run it, i get a bit of a warning hell.. Illegal string offset.. – Lasse May 15 '15 at 15:12
  • I don't know the structure of the database, so the `$row` array indexes may be all wrong. If you could give us some insight into your db... – Ondřej Hlaváček May 15 '15 at 15:15
  • Ohh sure, no problem. The row of the event is structured this way: eid(event id), uid, cid(Consultant id), lid(location id), date, hours, type, comment and accepted. Sorry for the lag of info – Lasse May 15 '15 at 15:18
  • Well i have found the error.. it's the foreach loop. When you take the values from $row it cannot find the value. But if you take the value from $rows['date']; as an example it can process it. – Lasse May 15 '15 at 15:31
  • Note that i also changed the `$rows` value content and there's no `$row` variable in my example. You want to iterate over an array, not over a single record. – Ondřej Hlaváček May 15 '15 at 15:38
  • But, now a new problem emerges.. i get 16 rows, but in the database there is only one ?.. – Lasse May 15 '15 at 15:39
  • Do the rows have different `eid`? – Ondřej Hlaváček May 15 '15 at 15:40
  • No, it prints out the same row, 16 times. – Lasse May 15 '15 at 15:42
  • The array looks like this: `Array ( [0] => 6 [eid] => 6 [1] => 4 [uid] => 4 [2] => 0 [cid] => 0 [3] => 3 [lid] => 3 [4] => 2015-05-06 [date] => 2015-05-06 [5] => 2 [hours] => 2 [6] => 0 [type] => 0 [7] => test [comment] => test [8] => 0 [accepted] => 0 )` – Lasse May 15 '15 at 15:44
  • A little bit of remote debugging, can you do `var_export($rows);`? – Ondřej Hlaváček May 15 '15 at 15:49
  • I've got it! I took the liberty of changing the code you so kindly presented to me, and made it intro something else. So instead of the "foreach" loop, i made a "For" loop looking this way: for `($i=0; $i – Lasse May 15 '15 at 15:50
  • No problem, still wondering, where was the error and why foreach didn't work in this case :-) – Ondřej Hlaváček May 15 '15 at 15:51
  • That is also a bit of a mystery for me to.. but could you be so kind as to update your code ? Just to help others ? Thanks again :) – Lasse May 15 '15 at 15:53
  • Fixed. Is that what you meant? – Ondřej Hlaváček May 15 '15 at 15:58