用PHP制作表格

时间:2017-04-11 01:21:56

标签: php html

基本问题是我试图制作一个类似于这个的表

https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_caption_test

我的网页:http://lamp.cse.fau.edu/~mcuervo5/p6/

在我的php文件中为“name”& “内容”

通过使我的男孩桌和女孩桌的桌子全部边界和整齐地分开

但是我第一次使用边距和填充来使空间整洁而且这里有很多试验和错误似乎无法解决问题

试图取代这个:

$myTable = "<table>

用这个:

$myTable = "<table style="margin: 5px; border: 1px; border-color: black;">

但得到错误代码:(。 您可以右键单击“检查”页面或从此处从我的PHP代码中获取它到目前为止。谢谢你,如果你能帮我这个

    <?php
require_once './php/db_connect.php'; // Will call this php file
?>



    <!DOCTYPE html>
<html lang="en">
  <head>

刚添加此内容以制作边框线

      <style>
          table, th, td 
          {
    border: 2px solid black;
          }</style>

结束新编辑

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Maundy | Comming Soon Page</title>
    <meta name="description" content="Free Bootstrap Theme by BootstrapMade.com">
    <meta name="keywords" content="free website templates, free bootstrap themes, free template, free bootstrap, free website template">
    <link href='https://fonts.googleapis.com/css?family=Lobster|Open+Sans:400,400italic,300italic,300|Raleway:300,400,600' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/animate.css">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>


    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.countdown.min.js"></script>
    <script src="js/wow.js"></script>
    <script src="js/custom.js"></script>
    <script src="contactform/contactform.js"></script>


    <body>

<div class="col-lg-12">



<form method="post" action="index.php">
        <label>Enter Name for Baby:</label>
        <input type="text" name="baby_name" />
        <label>Select Gender:</label>
        <select name="gender">
            <option value="B">Boy</option>
            <option value="G">Girl</option>
        </select>
        <input type="submit" value="Sumbit!" />
    </form>

</div>



<?php 
if (isset($_POST['gender'])&& isset($_POST['baby_name']) )
    {
        $gender = $_POST['gender'];
    $val_input = $_POST['baby_name'];


// Add two rows to the table
$selectStmt = 'SELECT  `Votes`
FROM  `Free_Names` 
WHERE  `Gender` =  \''.$gender.'\' AND `Name` = \''.$val_input.'\';';

$result = $db->query($selectStmt); //query the "select" statement and update the result
if($result->num_rows > 0) { //if has a results

while($row = $result->fetch_assoc()) { //updates the statments

$UpdateStmt = 'UPDATE `Free_Names`  
SET `Votes` = '.($row["Votes"] +1).'
WHERE `Gender` =  \''.$gender.'\' AND `Name` = \''.$val_input.'\';';
$db->query($UpdateStmt); //this will run, but wont work

}


}


else
{

$insertStmt = 'INSERT INTO `Free_Names` (`Name`, `Gender`, `Votes` )' . PHP_EOL
            . '  VALUES (\''.$val_input.'\',\''.$gender.'\',1);'; 

$db->query($insertStmt); //


}

}

?>




<?php
// Get the rows from the table, and \\ for single quote to show up
$selectStmt = 'SELECT  `Name` ,  `Votes` 
FROM  `Free_Names` 
WHERE  `Gender` =  \'B\'
ORDER BY  `Votes` DESC 
LIMIT 5';
?>
      <div class="col-md-6">
          <div class="mx-auto" style="width: 200px;">

这里是名称

的表格
     BOY NAMES
</div>
<?php
$result = $db->query($selectStmt); //query the "select" statement and store the result
if($result->num_rows > 0) {
  $myTable = "<table>
  <thead>
  <tr><th>Name</th>
  <th>votes</th></tr>
  </thead><tbody>";        

    while($row = $result->fetch_assoc()) {
        $myTable.= "<tr>
        <td>".$row["Name"]."</td>
        <td>".$row["Votes"]."</td>
        </tr>";                       

    }
    $myTable.= "</tbody></table>";
    echo $myTable;
} else {
    echo '        <div class="alert alert-success">No Results</div>' . PHP_EOL;
}
?>
      </div>



<?php
// Get the rows from the table, and \\ for single quote to show up
$selectStmt = 'SELECT  `Name` ,  `Votes` 
FROM  `Free_Names` 
WHERE  `Gender` =  \'G\'
ORDER BY  `Votes` DESC 
LIMIT 5';
?>
       <div class="col-md-6">
          <div class="mx-auto" style="width: 200px;">
  GIRL NAMES
</div>
<?php
$result = $db->query($selectStmt); //query the "select" statement and store the result
if($result->num_rows > 0) {

    $myTable = "  <table>
  <thead>
  <tr >
    <th>Name</th>
    <th>votes</th>
  </tr>
  </thead><tbody>";        

    while($row = $result->fetch_assoc()) {
        $myTable.= "<tr>
        <td>".$row["Name"]."</td>
        <td>".$row["Votes"]."</td>
        </tr>";                       

    }
    $myTable.= "</tbody></table>";
    echo $myTable;
} else {
    echo '        <div class="alert alert-success">No Results</div>' . PHP_EOL;
}
?>
      </div>

    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

我只是使用W3Schools的例子。说到cellpadding,请将样式添加到<td>。说到单元格边距,请使用<table>将样式添加到border-spacing

td{
  padding: 12px 6px;
}
table{
  border-spacing: 10px;
}

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
}

td{
  padding: 12px 6px;
}
table{
  border-spacing: 10px;
}

</style>
</head>
<body>

<table>
  <caption>Monthly savings</caption>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$50</td>
  </tr>
</table>

</body>
</html>
&#13;
&#13;
&#13;