Ntn-child not working when elements pulled from php

so i had this table and i would like to have every 2nd row to be different line

<?php include 'connection.php';
$sql= "SELECT Name, Description, Price, Type FROM Treatments WHERE Type ='facials for all ages'";
$result= mysqli_query($con, $sql);
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<table><tr><td><strong>" .$row['Name'].  "<br></strong>";
echo $row['Description'];
echo "</td><td>";
echo $row['Price'];
echo "</td></tr>";
echo "</table>";
}
mysqli_close($con);
?>	

with css
tr:nth-child(even) {
    background-color: #ffffff!important;
}

it worked till I implemented php, before was hardcoded and worked

Does the resulting html differ in any way to the hardcoded version?

I see it, you are targeting table rows, but each table only has one row.
If you need a single multi row table, you need to open the table before the while and close it after, so each database row creates a table row, not a new table.

<?php include 'connection.php';
$sql= "SELECT Name, Description, Price, Type FROM Treatments WHERE Type ='facials for all ages'";
$result= mysqli_query($con, $sql);
echo "<table>";
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr><td><strong>" .$row['Name'].  "<br></strong>";
echo $row['Description'];
echo "</td><td>";
echo $row['Price'];
echo "</td></tr>";
}
echo "</table>";
mysqli_close($con);
?>	
1 Like

You’re writing each row as a separate table. I suspect that’s the problem. I guess your table and /table tags need to be outside the while loop…

1 Like

yeah working now… thanks a lot

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.