HTML Table with inline PHP

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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>

回答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>


转载请标明出处:HTML Table with inline PHP
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!