-4

My code currently outputs the row by row from the information collected by an sql query.

The output looks like this at the moment:

First Name: test2 - Last Name: test2 - Appointment Time: 12041312
First Name: Jim - Last Name: Bob - Appointment Time: 13051130
First Name: John - Last Name: Smith - Appointment Time: 13051230

I am trying to sort it into a HTML table. Any help appreciated.

Code for output:

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "First Name: " . $row["FirstName"]. " - Last Name: " . $row["LastName"]. " - Appointment Time: " . $row["AppTime"]. "<br>";
    }
} else {
    echo "No results, please try again";
}
8
  • 2
    Add ORDER BY columname to the query. Let MySQL do all the work for you Commented Nov 23, 2019 at 16:56
  • PS you can make that ECHO easier to read echo "First Name: $row[FirstName] - Last Name: $row[LastName] - Appointment Time: $row[AppTime]<br>"; Commented Nov 23, 2019 at 16:58
  • yuk, i do not like that variable interpolation suggestion which you made.... @RiggsFolly it might be faster or slower on some PHP versions..But on PHP 7 this method seams to be faster, which i found wierd.. Edit oh there ` {$<...>}` was benchmarked which is different then variable interpolation Commented Nov 23, 2019 at 17:04
  • @RaymondNijland Had no idea on speed. Its just easier to read/maintain that all that concatenation Commented Nov 23, 2019 at 17:05
  • 1
    @RaymondNijland "Had no idea on speed" means I did not even consider if it may have any effect on execution speed. "think i would even separate the single line on multiple lines....." Absolutely agree. But I didnt want to take an obvious newbie and baffle them with anything more than they appeared to be capable of at the current place on their learning curve Commented Nov 23, 2019 at 17:24

1 Answer 1

0

This is how you have to add the data into a table.

if (mysqli_num_rows($result) > 0) {
    echo "<table>";
    echo "<tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Appointment Time</th>
        </tr>";
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr>
                <td>{$row['FirstName']}</td>
                <td>{$row['LastName']}</td>
                <td>{$row['AppTime']}</td>
            </tr>";
    }
    echo "</table>";
} else {
    echo "No results, please try again";
}

If the issue is related to your query speed, please consider adding pagination. Or try to Index your DB table.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.