2

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 =/

2
  • Not sure about the question at hand but you realize you're in a while loop there right you really don't have to explicitly call each row with $row[x], nor should you be since that would mean setting up your HTML without knowing how many records you're going to have at any given point. Either use an increment value or output the array to $row then do a foreach on that. Commented Dec 12, 2012 at 12:25
  • Thanks, makes sense and will revise that part. Commented Dec 12, 2012 at 13:09

1 Answer 1

7

make a rewind of your data first!!

mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result)) {
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.