Jump to content

How do I get the results to show as a table


Go to solution Solved by Barand,

Recommended Posts

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";
}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

  • Solution

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.