-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";
}
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
NI186
  • 1
  • 1
  • 2
    Add `ORDER BY columname` to the query. Let MySQL do all the work for you – RiggsFolly Nov 23 '19 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]
    ";`
    – RiggsFolly Nov 23 '19 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](https://stackoverflow.com/a/58917133/2548147), which i found wierd.. Edit oh there ` {$<...>}` was benchmarked which is different then variable interpolation – Raymond Nijland Nov 23 '19 at 17:04
  • @RaymondNijland Had no idea on speed. Its just easier to read/maintain that all that concatenation – RiggsFolly Nov 23 '19 at 17:05
  • @RiggsFolly *"Had no idea on speed"* well it's micro optimisation really... Anyhow *"Its just easier to read/maintain that all that concatenation "* i think i would even separate the single line on multiple lines grouped on fixed string and variable generated string value feels for me more readable and maintainable.. – Raymond Nijland Nov 23 '19 at 17:11
  • I mean into an actual html table using ect. – NI186 Nov 23 '19 at 17:12
  • Are you asking how to write some HTML to output data into an HTML table? – RiggsFolly Nov 23 '19 at 17:22
  • 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 – RiggsFolly Nov 23 '19 at 17:24

1 Answers1

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.

Prifulnath
  • 463
  • 7
  • 24