1

I have managed to take information from my database of the website, however I have no idea how to present the results in a table and how to style it. I tried putting <table> outside of the whole PHP, clearly did not work. I tried echoing a <table> tag before the result echo and a closing </table> tag after it, but that did not do it. This is the code I am working with:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "onlib";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    //Takes all the results from the table with genre 5.
    $sql = "SELECT name, description, content FROM books WHERE genre='5'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<span style='color:white;'>"."<br> Name: ".$row["name"]."<br> Description: ".$row["description"]."<br> Content: ".$row["content"] ."<br>"."</p>";
        }
    } else {
        echo "0 results";
    }

    $conn->close();
?>

I am still new in PHP, trying to understand how the whole thing works. Thanks in advance!

Saral
  • 1,087
  • 1
  • 8
  • 18
  • 2
    Show us what you actually tried, instead of just telling us vague stories about what didn’t work … _“I am still new in PHP”_ - this has rather little to do with the specific language, and is more or less just a general issue of logical thinking … What is the structure I _want_ to create, and _where_ do the necessary tags have to be output for that to happen. – CBroe Jun 08 '18 at 12:01
  • Do you get any output at all from the script as it is currently? – fightstarr20 Jun 08 '18 at 12:02
  • I searched for a few things, could not get it to work. The code as it is right now shows the information from the database as it is supposed to. Name: Alice in Wonderland Description: A nice book Content: This is the whole content. If I add another book, it will show it under this one with a line between them. – Todor Yakov Jun 08 '18 at 12:05
  • if ($result->num_rows > 0) { // output data of each row echo ""; while($row = $result->fetch_assoc()) { echo ""."
    Name: ".$row["name"]."
    Description: ".$row["description"]."
    Content: ".$row["content"] ."
    ".""; echo "
    "; } } else { echo "0 results"; } This is the whole if I tried to do, but a table is not created.
    – Todor Yakov Jun 08 '18 at 12:08
  • 1
    Basically PHP just creates plain HTML. I'd suggest to create a pure HTML without PHP and then replace the dynamic part (which should get repeated gor each roe, normally the ... part) with the PHP code. – Herbert Scheffknecht Jun 08 '18 at 12:09

5 Answers5

0

Try this:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "onlib";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    //Takes all the results from the table with genre 5.
    $sql = "SELECT name, description, content FROM books WHERE genre='5'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        echo "<table>";
        echo "<thead>
          <tr>
             <th>Name</th>
             <th>Description</th>
          </tr>
         </thead>";
        echo "<tbody>";
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>".$row["name"]."</td>";
            echo "<td>".$row["description"]."</td>";
            echo "</tr>";            
        }
        echo "</table>";
    } else {
        echo "0 results";
    }

    $conn->close();
?>
  • Thank you. This does style it in a table! I have forgotten to include the information about each row. Thank you. – Todor Yakov Jun 08 '18 at 12:26
0

Try this approach:

[... some HTML header and code ...]

<table>
    <thead>
        <tr><th>Name</th><th>Description</th><th>Content</th></tr>
    </thead>
    <tbody>
    <?php
        [ ... extract something from the database ...]

        while($row = $result->fetch_assoc()) 
        {
            echo "<tr><td>$row[name]</td><td>$row[description]</td><td>Content</td></tr>\n";
        }
    }
    ?>
    </tbody>
</table>

[... some HTML footer ...]

Obviously, replace the [...] sections with your own code. The idea is here to use PHP to output the dynamic part of your HTML code, and is the power of PHP combined with HTML.

I try to avoid "echoing" to much HTML in my PHP, but it does work (reference other answers).

Detail: when setting up a <table> you should setup a header section <thead> <tfoot> (if applicable) and body section <tbody>. Ref What is the purpose for HTML's tbody?

Nic3500
  • 8,144
  • 10
  • 29
  • 40
0
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo '<table>';
    echo '<tr><td>Name</td><td>Description</td><td>Content</td></tr>'
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["name"]."</td><td>".$row["description"]."</td><td>".$row["content"] ."</td></tr>";
    }
    echo '</table>';
} else {
    echo "0 results";
}

$conn->close();

?>

  • Thank you. This does style it in a table! I have forgotten to include the information about each row. Thank you. – Todor Yakov Jun 08 '18 at 12:25
0

The basic table format is this

for example:

<table>
    <tr>
        <td>row 1 item 1</td>
        <td>row 1 item 2</td>
    </tr>
    <tr>
        <td>row 2 item 1</td>
        <td>row 2 item 2</td>
    </tr>
</table>

so try:

if ($result->num_rows > 0) {
    echo "<table>";
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>".$row["name"]."</td>";
        echo "<td>".$row["description"]."</td>";
        echo "<td>".$row["content"]."</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
0
<?php

function tableV1 ($row) {
    echo '<tr>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['content'] . '</td>';
    echo '</tr>';
}

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } 
?>

Always do Database Connection first, before Outputting anything, that way, you can create custom error message to show instead of a failed database conntection or no content at all.

<style type="text/css">
table {}
tbody {}
td {}
th {}
thead {}
tr {}
</style>

Style is used inside the <head></head> to style the table, CSS it's called.

<table>
    <thead>
        <th>Name</th>
        <th>Description</th>
        <th>Content</th>
    </thead>
    <tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

    // Output data of each row
    while($row = $result->fetch_assoc()) {
        tableV1($row);
    }

} else {
    echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
    </tbody>
</table>

Output contents from database.

<?php

$conn->close();

?>

Close database connection in the end. All together:

<?php

function tableV1 ($row) {
    echo '<tr>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['content'] . '</td>';
    echo '</tr>';
}

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } 
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style type="text/css">
        table {}
        tbody {}
        td {}
        th {}
        thead {}
        tr {}
        </style>
    </head>
    <body>
        <table>
            <thead>
                <th>Name</th>
                <th>Description</th>
                <th>Content</th>
            </thead>
            <tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

    // Output data of each row
    while($row = $result->fetch_assoc()) {
        tableV1($row);
    }

} else {
    echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
            </tbody>
        </table>
    </body>
</html>
<?php

$conn->close();

?>
Anuga
  • 2,619
  • 1
  • 18
  • 27