John Davidson

php - count the rows in a table and change the width of a single column ..?

0 comments
Message:


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>


My Answer:
To count the rows in a table and change the width of a single column, you can modify your PHP code as follows:

1. Count the rows in the table and display the count in a new column "N":
php
// Select sql query
$sql = "SELECT ID,task FROM dolist";
$result = mysqli_query($con, $sql);

$count = 1; // Initialize row count
while ($rows = mysqli_fetch_array($result)) {
echo "
";
echo "";
echo "" . $count . ""; // Display row count in column N
echo "" . "" . "";
echo "" . "" . "";
echo "" . "" . "";
echo "" . "" . "";
echo "";
echo "
";

$count++; // Increment row count
}


2. To change the width of the column "N", you can add a style attribute to the `` tag for that column:
php
echo "" . $count . ""; // Set width of column N to 50px


By adding the above code, you will be able to display the row count in a new column "N" and change the width of that column to make it look smaller than the other columns.

Rate this post

4 of 5 based on 3862 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us