HTML/PHP page alignment and buttons out of place

I’m fairly new to HTML and PHP programming and as a part of a project, I have to create a website. I cannot get the alignment right in some pages, and they have similar issues. Before a php generated table, I want a heading on top of it and another heading between table 1 and table 2, with a Back button at the bottom of the page. Here’s my code for the page:

<?php

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }
    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
    }
    function beginChildren() {
        echo "<tr>";
    }
    function endChildren() {
        echo "</tr>" . "\n";
    }
}


try {
    $path = 'sqlite:movies.db';
    $conn  = new PDO($path) or die("cannot open the database");
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}


if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['id_submit'])) {
    try{
        if (isset($_POST['movie_id'])) {
            $id = $_POST['movie_id'];

            $stmt1 = $conn->prepare("select name, count(movie_id) as cnt, year from movies join people join stars
                                    on stars.movie_id=movies.id and people.id=stars.person_id
                                    where person_id = '$id' group by name, year order by year desc");

            $stmt2 = $conn->prepare("select name, count(*) as cnt from directors join people
                                    on directors.person_id=people.id where directors.movie_id in
                                    (select movie_id from people join stars on people.id=stars.person_id
                                    where person_id = '$id') group by name order by cnt desc;");

            $stmt1->execute();
            $stmt2->execute();

            if($stmt1->fetchColumn() > 0) {
                echo "<script>alert('ID Found')</script>";
                echo "Heading 1";
                echo "<table style='border: solid 1px black;'>";
                echo "<tr><th>Name</th><th>Number of movies</th><th>Year</th></tr>";

                // set the resulting array to associative
                $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
                foreach(new TableRows(new RecursiveArrayIterator($stmt1->fetchAll())) as $k=>$v) {
                    echo $v;
                }

                echo "Heading 2";
                echo "<table style='border: solid 1px black;'>";
                echo "<tr><th>Name</th><th>Number of movies</th></tr>";

                // set the resulting array to associative
                $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
                foreach(new TableRows(new RecursiveArrayIterator($stmt2->fetchAll())) as $k=>$v) {
                    echo $v;
                }
            } else {
                echo "<script>alert('This person has not starred in any movie')</script>";
                echo "<script> location.href='http://localhost/Project/checkmoviespre.php'; </script>";
                exit;
            }
        }
    }

    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}


?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset = "UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style.php"/>

    <title>Univlarge DB</title>
</head>

<body bgcolor="#00897B">
    
    <form action='checkmoviespre.php' method="POST">
        <input type='submit' value="Back" name="t_submit" id="t_submit"/>
    </form>

</body>

</html>

This is the output

I want the page to show Heading 1 first, then table1, then heading 2, then table2, then the back button. Please, suggest me the changes I should make.

>Solution :

I think you just have forgotten to close the HTML-Tables Tag </table>. Please try the following:

<?php        
echo "<script>alert('ID Found')</script>";
echo "Heading 1";
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Name</th><th>Number of movies</th><th>Year</th></tr>";

// set the resulting array to associative
$result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt1->fetchAll())) as $k=>$v) {
    echo $v;
}
echo "</table>" <!-- INSERT THIS -->

echo "Heading 2";
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Name</th><th>Number of movies</th></tr>";

// set the resulting array to associative
$result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt2->fetchAll())) as $k=>$v) {
    echo $v;
}
echo "</table>" <!-- INSERT THIS -->
?>

Leave a Reply