简体   繁体   中英

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

the output should be as the picture:

在此处输入图像描述

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:

$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)

This should then generate HTML like this:

<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:

   /* 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:

在此处输入图像描述

Note: you could do this by adding class attributes to the <th> elements and styling those. It makes for easier maintenance later.

Use of formatting attributes in HTML tags (like BORDER="1" ) is considered bad practice and should be avoided.

Don't use JS or PHP to count something pure CSS can help you with

  • Use CSS Counters
  • Use <thead> and <tbody> table elements
  • Use width: 0%; for the td you want really condensed in width

 .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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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