【问题标题】:PHP/HTML Not aligningPHP/HTML 不对齐
【发布时间】:2021-10-21 14:37:09
【问题描述】:

我决定重新尝试学习 PHP,并且我以前创建过这种东西,但我认为已经有一段时间了,所以我决定尝试再做一个。我正在尝试制作数据库表/页面。但是表格中的数据并没有正确对齐。这是它的样子 https://prnt.sc/1qcrdo8 以下是我的代码:

index.php

<?php 
main();
function main() {

include('index.html');
require('../Content/dbconnect.php');
$sql="SELECT * FROM Orders";
  $result=mysqli_query($conn,$sql);
  $count = mysqli_num_rows($result);
  if ($count>0) {

     while ($row = $result->fetch_row()) {
     echo "<table class='content'>";
     echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td></tr>";
     echo "</table>";
  }

  }
}

还有我的 index.html:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="Style/Style.css">
    <title>Admin Panel</title>
    
</head>
<body>
    <table>
  <div class="content">
    <table id="content" class="content">
        <tr>
            <th>ID</th>
            <th>CustomerName</th>
            <th>Order ID</th>
            <th>Customer Address</th>
            <th>Problems Described</th>
            <th>Notes for the customer</th>
        </tr>
</table>
</body>
</html>

【问题讨论】:

  • 可以使用 CSS 设置对齐方式
  • 当您说右对齐时,您的意思是您希望文本右对齐吗?还是要正确对齐?如果正确,在这种情况下什么是正确的?此外,这是一个地狱般的地址。
  • @ADyson 是的,我确实尝试过,因此为什么回显的文本在中间,它只是没有与行对齐

标签: php html css sql


【解决方案1】:

您需要从循环中删除您的表格标签。 - 它还有助于放入表头以识别您的列。使用您的代码原样..每次循环迭代都会创建一个新表..使表数据不对齐。您需要创建表 ONCE .. 创建标题 ONCE .. 循环并生成您的 &lt;tr&gt; &lt;/tr&gt; -- 并关闭您的表 ONCE ..

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="Style/Style.css">
    <title>Admin Panel</title>

</head>
<body>


<!--  I am commenting this out, because we echo it below in php.  
You can remove that echo, and uncomment this for the same result

<table id="content" class="content">
    <tr>
        <th>ID</th>
        <th>CustomerName</th>
        <th>Order ID</th>
        <th>Customer Address</th>
        <th>Problems Described</th>
        <th>Notes for the customer</th>
    </tr>

-->

<?php
require('../Content/dbconnect.php');
$sql="SELECT * FROM Orders";
$result=mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
if ($count>0) {

   echo "<table class='content'>";
   echo "<tr><th>Row 0 header</th>
             <th>Row 1 header</th>
             <th>Row 2 header</th>
             <th>Row 3 header</th>
         </tr>";

   while ($row = $result->fetch_row()) {       
      echo "<tr><td>" . $row[0] . "</td>
                <td>" . $row[1] . "</td>
                <td>" . $row[2] . "</td>
                <td>" . $row[3] . "</td>
            </tr>";
   }

   echo "</table>";
}
?>

</body>
</html>

【讨论】:

  • 感谢您的帮助,我不需要 index.html 但我确实需要将 CSS 添加到表格行中,但除了表格数据之外,它似乎没有让我这样做跨度>
  • 我为你编辑了我的答案。你可以在php 文件中轻松地移入和移出 html ......无论如何它应该让你开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 2017-11-18
  • 2014-08-10
相关资源
最近更新 更多