英文:
HTML/PHP page alignment and buttons out of place
问题
你希望页面首先显示"Heading 1",然后是"Table1",然后是"Heading 2",接着是"Table2",最后是返回按钮。要实现这个顺序,你可以按照以下方式更改你的代码:
-
将 "Heading 1" 和与其相关的表格代码移到 "echo <table style='border: solid 1px black;'>" 之前,这样它们将首先显示在页面上。
-
在 "Heading 2" 和与其相关的表格代码之后,添加返回按钮的 HTML 代码,确保它在表格之后显示。
这样,你的代码将按照你希望的顺序显示内容。请注意,为了更清晰地理解代码结构,你可能需要缩进代码块,以使其层次更加明确。
英文:
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. What changes should I make?
答案1
得分: -1
I think you just have forgotten to close the HTML-Tables Tag </table>
. Please try the following:
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 -->
英文:
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 -->
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论