AlphaOneIntelligence Posted June 24, 2023 Share Posted June 24, 2023 Can anyone provide their method of getting the results to show as a table as the results it showing now is an unreadable mess, I'd also like the following css styling as follows: Padding: 20px Border: 1px solid grey th to be in light grey Id be greatfull if someone could put the code into a table as its driving me nuts lol Here is the code for showing the results: $sql = "SELECT animal_type, animal_breed, colour, owner_name, address, telephone, mobile, email, offence, offence_date, offence_location, case_status, case_ref, action_required, action_taken, microchipped, microchip_number, aggressive, dangerous, lost, date_lost, location_lost, stolen, date_stolen, location_stolen, found, date_found, location_found, other_information FROM `animals` WHERE 1"; $result = $conn->query($sql); if ($result->num_rows > 0){ while($row = $result->fetch_assoc() ){ echo $row["animal_type"]." ".$row["animal_breed"]." ".$row["colour"]." ".$row["owner_name"]." ".$row["address"]." ".$row["mobile"]." ".$row["email"]." ".$row["offence"]." ".$row["offence_date"]." ".$row["offence_location"]." ".$row["case_status"]." ".$row["case_ref"]." ".$row["action_required"]." ".$row["action_taken"]." ".$row["microchipped"]." ".$row["microchip_number"]." ".$row["aggressive"]." ".$row["dangerous"]." ".$row["lost"]." ".$row["date_lost"]." ".$row["location_lost"]." ".$row["stolen"]." ".$row["date_stolen"]." ".$row["location_stolen"]." ".$row["found"]." ".$row["date_found"]." ".$row["location_found"]." ".$row["other_information"]."<br>"; } } else { echo "0 records"; } Quote Link to comment https://forums.phpfreaks.com/topic/317004-how-do-i-get-the-results-to-show-as-a-table/ Share on other sites More sharing options...
ginerjm Posted June 24, 2023 Share Posted June 24, 2023 Do you mean to show it as an HTML table? <table> <tr><th>heading</th><th>heading</th></tr> <tr><td>data</td><td>data</td></tr> </table> That is roughly the structure you have to use. There is some more modern items that now belong in there but I still use (and get away with) this simple old-style structure. Add your styling using css from a style section, not in each element. Assign a different class to your td tags and your th tags as well as your table tag itself to do different settings for each. Quote Link to comment https://forums.phpfreaks.com/topic/317004-how-do-i-get-the-results-to-show-as-a-table/#findComment-1609975 Share on other sites More sharing options...
Solution Barand Posted June 24, 2023 Solution Share Posted June 24, 2023 Output the results into an HTML table Put each result row in a new table <tr> row. Put each column into a table <td> cell. EG <?php // CONNECT TO DB HERE $sql = "SELECT animal_type , animal_breed , colour , owner_name , address , telephone , mobile , email , offence , offence_date , offence_location , case_status , case_ref , action_required , action_taken , microchipped , microchip_number , aggressive , dangerous , lost , date_lost , location_lost , stolen , date_stolen , location_stolen , found, date_found , location_found , other_information FROM `animals` -- WHERE 1 (utterly useless, just leave it out) "; //------------------------------------------------------------------------------ // I find this function useful for testing // queries when I haven't got MySql Workbench open function query2HTML($db, $sql) { $output = "<table border='1'>\n"; // Query the database $result = $db->query($sql); if ($result->num_rows == 0) return "No matching records"; // get the first row and display headings $row = $result->fetch_assoc(); $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n"; // display the data do { $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } while ($row = $result->fetch_assoc()); $output .= "</table>\n"; return $output; } ?> <!DOCTYPE html> <html lang='en'> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 19.5 (Build 19523, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="04/09/2022"> <style type='text/css'> table { border-collapse: collapse; margin: 20px auto; width: 95%; } td, th { padding: 4px 8px; } th { background-color: #ccc; } </style> </head> <body> <?= query2html($conn, $sql) ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/317004-how-do-i-get-the-results-to-show-as-a-table/#findComment-1609976 Share on other sites More sharing options...
AlphaOneIntelligence Posted June 24, 2023 Author Share Posted June 24, 2023 Thanks Barand, I used your method, thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/317004-how-do-i-get-the-results-to-show-as-a-table/#findComment-1609977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.