HTML/PHP页面对齐和按钮位置不正确。

huangapple go评论67阅读模式
英文:

HTML/PHP page alignment and buttons out of place

问题

你希望页面首先显示"Heading 1",然后是"Table1",然后是"Heading 2",接着是"Table2",最后是返回按钮。要实现这个顺序,你可以按照以下方式更改你的代码:

  1. 将 "Heading 1" 和与其相关的表格代码移到 "echo <table style='border: solid 1px black;'>" 之前,这样它们将首先显示在页面上。

  2. 在 "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:

&lt;?php

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }
    function current() {
        return &quot;&lt;td style=&#39;width:150px;border:1px solid black;&#39;&gt;&quot; . parent::current(). &quot;&lt;/td&gt;&quot;;
    }
    function beginChildren() {
        echo &quot;&lt;tr&gt;&quot;;
    }
    function endChildren() {
        echo &quot;&lt;/tr&gt;&quot; . &quot;\n&quot;;
    }
}


try {
    $path = &#39;sqlite:movies.db&#39;;
    $conn  = new PDO($path) or die(&quot;cannot open the database&quot;);
}
catch(PDOException $e) {
    echo &quot;Error: &quot; . $e-&gt;getMessage();
}


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

            $stmt1 = $conn-&gt;prepare(&quot;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 = &#39;$id&#39; group by name, year order by year desc&quot;);

            $stmt2 = $conn-&gt;prepare(&quot;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 = &#39;$id&#39;) group by name order by cnt desc;&quot;);

            $stmt1-&gt;execute();
            $stmt2-&gt;execute();

            if($stmt1-&gt;fetchColumn() &gt; 0) {
                echo &quot;&lt;script&gt;alert(&#39;ID Found&#39;)&lt;/script&gt;&quot;;
                echo &quot;Heading 1&quot;;
                echo &quot;&lt;table style=&#39;border: solid 1px black;&#39;&gt;&quot;;
                echo &quot;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Number of movies&lt;/th&gt;&lt;th&gt;Year&lt;/th&gt;&lt;/tr&gt;&quot;;

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

                echo &quot;Heading 2&quot;;
                echo &quot;&lt;table style=&#39;border: solid 1px black;&#39;&gt;&quot;;
                echo &quot;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Number of movies&lt;/th&gt;&lt;/tr&gt;&quot;;

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

    catch(PDOException $e) {
        echo &quot;Error: &quot; . $e-&gt;getMessage();
    }
}


?&gt;

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

    &lt;title&gt;Univlarge DB&lt;/title&gt;
&lt;/head&gt;

&lt;body bgcolor=&quot;#00897B&quot;&gt;
    
    &lt;form action=&#39;checkmoviespre.php&#39; method=&quot;POST&quot;&gt;
        &lt;input type=&#39;submit&#39; value=&quot;Back&quot; name=&quot;t_submit&quot; id=&quot;t_submit&quot;/&gt;
    &lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;

This is the output

HTML/PHP页面对齐和按钮位置不正确。

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 &lt;/table&gt;. Please try the following:

&lt;?php        
echo &quot;&lt;script&gt;alert(&#39;ID Found&#39;)&lt;/script&gt;&quot;;
echo &quot;Heading 1&quot;;
echo &quot;&lt;table style=&#39;border: solid 1px black;&#39;&gt;&quot;;
echo &quot;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Number of movies&lt;/th&gt;&lt;th&gt;Year&lt;/th&gt;&lt;/tr&gt;&quot;;

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

echo &quot;Heading 2&quot;;
echo &quot;&lt;table style=&#39;border: solid 1px black;&#39;&gt;&quot;;
echo &quot;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Number of movies&lt;/th&gt;&lt;/tr&gt;&quot;;

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

huangapple
  • 本文由 发表于 2023年5月7日 04:18:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定