简体   繁体   中英

PHP echo mysql table column names for html table header

RESOLVED

Works perfectly! Here is my final code:

<table>
  <thead>
    <tr>
      <?php
      $row = mysql_fetch_assoc($result);
            foreach ($row as $col => $value) {
                echo "<th>";
                echo $col;
                echo "</th>";
            }
      ?>
      <th>Edit</th>
    </tr>
  </thead>
  <tbody>
    <?php
  // Write rows
  mysql_data_seek($result, 0);
    while ($row = mysql_fetch_assoc($result)) {
        ?>
    <tr>
      <?php         
    foreach($row as $key => $value){
        echo "<td>";
        echo $value;
        echo "</td>";
    }
    ?>
      <td><button id="edit_project_button(<?php echo $row['ID']; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row['ID']; ?>)">Edit</button></td>
    </tr>
    <?php } ?>
  </tbody>
</table>

I wish to echo out a HTML table using mysql_fetch functions appropriately. I plan on making a thead to contain the mysql table column names and a tbody to contain the mysql table resultset. The SQL query selects a couple of columns from the table, with default limit set.

The issue: It doesn't seem to print the first row of table data, everything else displays (record #1 missing)

It displays the with column names echo'd within each , it then skips the first record and successfully echo's the 2nd row onward. For example:

| id | firstname | lastname | date_start | date_end   | clientid | members | edit          |
|  2 | Cal       | Clark    | 2012-12-12 | 2012-12-12 | 22       | Rob     | (edit button) |
|  3 | Rob       | Robin    | 2012-12-12 | 2012-12-12 | 33       | Cal     | (edit button) |

I'm 100% sure that the first record will display from my query in phpmyadmin.

Here is my code:

<table>
  <thead>
    <tr>
      <?php
        $row = mysql_fetch_assoc($result);
            foreach ($row as $col => $value) {
                echo "<th>";
                echo $col;
                echo "</th>";
            }

      ?>
      <th>Edit</th>
    </tr>
  </thead>
  <?php
  // Write rows
  while ($row = mysql_fetch_array($result)) {
    ?>
  <tr>
    <td><?php echo $row[0]; ?></td>
    <td><?php echo $row[1]; ?></td>
    <td><?php echo $row[2]; ?></td>
    <td><?php echo $row[3]; ?></td>
    <td><?php echo $row[4]; ?></td>
    <td><?php echo $row[5]; ?></td>
    <td><?php echo $row[6]; ?></td>
    <td><button id="edit_project_button(<?php echo $row[0]; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row[0]; ?>)">Edit</button></td>
  </tr>
  <?php } ?>
</table>

I feel so oblivious right now =/

make a rewind of your data first!!

mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result)) {
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM