Jump to content

How do I get the results to show as a table with two side by side columns


Recommended Posts

I want the results to display with the th's on the left column and the results to show on the right in another column and I want the zebra stripe design for both th and result.

Here is the code:

<?php
// Establish a connection to the MySQL database
$servername = "";
$username = "";
$password = "";
$dbname = "";

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Collect the search query from a form input
$search_query = $_POST['search_query'];

// Construct an SQL query to search for data in the MySQL database
$sql = "SELECT * FROM animals WHERE animal_type LIKE '%$search_query%' OR animal_breed LIKE '%$search_query%' OR colour LIKE '%$search_query%' OR owner_name LIKE '%$search_query%' OR address LIKE '%$search_query%' OR telephone LIKE '%$search_query%' OR mobile LIKE '%$search_query%' OR email LIKE '%$search_query%' OR offence LIKE '%$search_query%' OR offence_date LIKE '%$search_query%' OR offence_location LIKE '%$search_query%' OR case_status LIKE '%$search_query%' OR case_ref LIKE '%$search_query%' OR action_required LIKE '%$search_query%' OR action_taken LIKE '%$search_query%' OR microchipped LIKE '%$search_query%' OR microchip_number LIKE '%$search_query%' OR aggressive LIKE '%$search_query%' OR dangerous LIKE '%$search_query%' OR lost LIKE '%$search_query%' OR date_lost LIKE '%$search_query%' OR location_lost LIKE '%$search_query%' OR stolen LIKE '%$search_query%' OR date_stolen LIKE '%$search_query%' OR location_stolen LIKE '%$search_query%' OR found LIKE '%$search_query%' OR date_found LIKE '%$search_query%' OR location_found LIKE '%$search_query%' OR other_information LIKE '%$search_query%'";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // Output the search results
    while($row = mysqli_fetch_assoc($result)) {
        echo "Animal Type: " .  $row["animal_type"] . "<br>";
        echo "Animal Breed: " . $row["animal_breed"] . "<br>";
        echo "Colour: " . $row["colour"] . "<br>";
        echo "Owner Name: " . $row["owner_name"] . "<br>";
        echo "Address: " . $row["address"] . "<br>";
        echo "Telphone: " . $row["telephone"] . "<br>";
        echo "Mobile: " . $row["mobile"] . "<br>";
        echo "Email: " . $row["email"] . "<br>";
        echo "Offence: " . $row["offence"] . "<br>";
        echo "Offence Date: " . $row["offence_date"] . "<br>";
        echo "Offence Location: " . $row["offence_location"] . "<br>";
        echo "Case Status: " . $row["case_status"] . "<br>";
        echo "Case Ref: " . $row["case_ref"] . "<br>";
        echo "Action Required: " . $row["action_required"] . "<br>";
        echo "Action Taken: " . $row["action_taken"] . "<br>";
        echo "Microchipped: " . $row["microchipped"] . "<br>";
        echo "Microchip Number: " . $row["microchip_number"] . "<br>";
        echo "Aggressive: " . $row["aggressive"] . "<br>";
        echo "Dangerous: " . $row["dangerous"] . "<br>";
        echo "Lost: " . $row["lost"] . "<br>";
        echo "Date Lost: " . $row["date_lost"] . "<br>";
        echo "Location Lost: " . $row["location_lost"] . "<br>";
        echo "Stolen: " . $row["stolen"] . "<br>";
        echo "Date Stolen: " . $row["date_stolen"] . "<br>";
        echo "Location Stolen: " . $row["location_stolen"] . "<br>";
        echo "Found: " . $row["found"] . "<br>";
        echo "Date Stolen: " . $row["date_stolen"] . "<br>";
        echo "Location Stolen: " . $row["location_stolen"] . "<br>";
        echo "Found: " . $row["found"] . "<br>";
        echo "Date Found: " . $row["date_found"] . "<br>";
        echo "Location Found: " . $row["location_found"] . "<br>";
        echo "Other Information: " . $row["other_information"] . "<br>";
        echo "<hr>";
        echo "<br>";
        echo "<br>";
    }
} else {
    echo "No Results Found";
}

// Close the database connection
mysqli_close($conn);
?>

 

Any help will be much appreciated, and if you could put all the code where it should be as Im new to PHP many thanks :)

Link to comment
Share on other sites

Alternatively

<?php
$result = $conn->query("SELECT * 
                        FROM animals
                        ");

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sample</title>
    <style type='text/css'>
        label {
            padding: 4px;
            width: 120px;
            display: inline-block;
            background-color: #ccc;
            border: 1px solid white;
            margin-right: 10px;
            font-variant: small-caps;
        }
    </style>
</head>
<body>
      <?php
            foreach ($result as $row)  {
                foreach ($row as $k => $v)  {
                    echo "<label>" . ucwords( str_replace('_', ' ', $k) ) . "</label>$v<br>";
                }
                echo '<hr>';
            }
      ?>
</body>
</html>

test table...

+----+-------------+--------------+--------+-------------+
| id | animal_type | animal_breed | colour | owner_name  |
+----+-------------+--------------+--------+-------------+
|  1 | Cat         | Persian      | white  | Lucy Lastik |
|  2 | Dog         | Labrador     | black  | Scott Chegg |
+----+-------------+--------------+--------+-------------+

results...

image.png.04d1c2c188908f8cbadc63f446df6454.png

Link to comment
Share on other sites

Why not an html table?

Here's the start of one:

echo "<table border=1 style='border-collapse:collapse;'>";
while (....)
{
	echo "
		<tr><td>Animal Type: </td><td>{$row['animal_type']}</td></tr>
		<tr><td>Animal Breed: </td><td>{$row['animal_breed']}</td></tr>
		<tr><td>Colour: </td><td>{$row['colour']}</td></tr>
		<tr><td>Owner Name: </td><td>{$row['owner_name']}</td></tr>
		....
		";
}
echo "</table>";

 

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.