简体   繁体   English

计算表中的行数并更改单列的宽度..?

[英]count the rows in a table and change the width of a single column ..?

i need to get the counting of the table rows in order as a column in column "N" also i need to make the column"N" width looks smaller than others columns i think i have done everything but this is the only point i couldn't achieved我需要按顺序对表格行进行计数,作为“N”列中的一列,我还需要使“N”列的宽度看起来比其他列小我认为我已经做了所有事情,但这是我唯一能做的没有达到

the output should be as the picture: output 应该如图所示:

在此处输入图像描述

i hope you could help me guys, thank you here is my codes:我希望你们能帮助我,谢谢,这是我的代码:

<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<?php
  // DataBase connection
  $host     = "localhost";
  $user     = "root";
  $password = "";
  $db       = "worksheet9";

  $con = mysqli_connect($host, $user, $password, $db);

  //verfiy Connection
  if( mysqli_connect_errno()) {
    echo " Connection Error:" . mysqli_connect_error();
  }
  if(isset($_POST['submit'])) { 

    $task=$_POST['task'];
    
  // insert sql query 
    $sql = "INSERT INTO dolist (task) VALUES ('$task')";
    
    $result=mysqli_query($con, $sql);
    
  // verify query 
    if ($result) {
      echo "New record created successfully";
    } else {
      echo "Error: " . $sql . "<br>" . mysqli_error($con);
    }
  }  
?> 
<form method='POST'>
<input type="text" name="task" id="task" required 
        placeholder="add task" oninvalid="this.setCustomValidity('you must fill the task')"
        oninput="this.setCustomValidity('')"> 
<input type="submit"name="submit"value="add task" ><!-- comment -->

</form> 

<table border="1">
  <tr> 
    <th> N </th>
    <th>ID </th> 
    <th>task</th> 
    <th>delete</th><!-- comment -->
    <th>update</th>
  </tr>

<?php
  //  Select sql query 
  $sql    = "SELECT ID,task FROM dolist";
  $result = mysqli_query($con, $sql);

  while ($rows = mysqli_fetch_array($result)) {
  echo"<form method ='POST'>";
    echo "<tr>";
    echo "<td>" . "<input type='number' readonly name='number' value='" . $rows[''] . "'></td>"; 
    echo "<td>" . "<input type='text' readonly name='id' value='" . $rows['ID'] . "'></td>"; 
    echo "<td>" . "<input type='text' name='task' value= '" . $rows['task'] . "'></td>";
    echo "<td>" . "<input type='submit' name='delete' value='x'>" . "</td>";
    echo "<td>" . "<input type='submit' name='update' value='update'>" . "</td>";
    echo "</tr>";
    echo "</form>";
  }
  
  if(isset($_POST['update'])) {
    $sql    = "UPDATE dolist SET task='$_POST[task]'WHERE ID=$_POST[id]";
    $result = mysqli_query($con,$sql) or die(mysqli_connect_errno());
    if($result){
      echo "updated";
    } else {
      echo "update faild";
    }
  } 
  //perform only when the user click on Delete
  if(isset($_POST['delete'])) {
    $sql    = "DELETE FROM dolist WHERE ID=$_POST[id]";
    $result = mysqli_query($con,$sql) or die(mysqli_connect_errno());
    if($result){
      echo "DELETED";
      }else{
      echo "delete faild";
    }
  }
?>  
</table>
</body>
</html>

For row numbering add a counter to your PHP code:对于行编号,在您的 PHP 代码中添加一个计数器:

$rowNumber = 1;
while ($rows = mysqli_fetch_array($result)){
echo"<form method ='POST'>";
    echo "<tr>";
       echo "<td>" . $rowNumber++. "</td>"; 

       // echo other fields...
               
    echo"</tr>";

For formatting, give your table an ID (not essential but it makes the CSS styling more specific)对于格式化,给你的表格一个 ID(不是必需的,但它使 CSS 样式更具体)

This should then generate HTML like this:这应该生成 HTML ,如下所示:

<table id="results">
    <tr><th>N</th><th>ID</th><th>task</th><th>Delete</th><th>Update</th></tr>
    <tr><td>1</td><td>11</td><td>task</td><td>X</td><td>Update</td></tr>
    <tr><td>2</td><td>12</td><td>task</td><td>X</td><td>Update</td></tr>

</table>

Now you can style it with CSS:现在,您可以使用 CSS 对其进行样式设置:

   /* Border around the table */
   #results {     
        border: 1px solid grey;
    }

    /* Border around the individual cells */
    #results th, #results td {
        border: 1px solid gray;
    }

    /* Set the width of the first TH. Also sets the width of the res of the column */
    #results th:first-of-type {
        width:1em;
    }
    /* Set the width of the 3rd column (the 'task' column */
    #results th:nth-of-type(3) {
        width:10em;
    }

Output: Output:

在此处输入图像描述

Note: you could do this by adding class attributes to the <th> elements and styling those.注意:您可以通过将 class 属性添加到<th>元素并设置它们的样式来做到这一点。 It makes for easier maintenance later.它使以后的维护更容易。

Use of formatting attributes in HTML tags (like BORDER="1" ) is considered bad practice and should be avoided.在 HTML 标签(如BORDER="1" )中使用格式属性被认为是不好的做法,应该避免。

Don't use JS or PHP to count something pure CSS can help you with不要使用 JS 或 PHP 来计算纯 CSS可以帮助您

  • Use CSS Counters使用CSS 计数器
  • Use <thead> and <tbody> table elements使用<thead><tbody>表格元素
  • Use width: 0%;使用width: 0%; for the td you want really condensed in width对于你想要真正压缩宽度的td

 .tbodyCount { counter-reset: rowIndex; }.tbodyCount tr { counter-increment: rowIndex; }.tbodyCount td:nth-child(1)::before { width: 0%; /* Make as smaller as possible */ content: counter(rowIndex); /* Learn about counters*/ }
 <table> <thead> <tr> <th>N</th> <th>ID</th> </tr> </thead> <tbody class="tbodyCount"> <tr> <td></td> <td>Foo</td> </tr> <tr> <td></td> <td>Bar</td> </tr> <tr> <td></td> <td>Baz</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM