1

I have a html table received from this query:

SELECT proj_title, CONCAT(projectNo, " ", proj_title) AS title
FROM (
        SELECT projectNo, CONCAT("Project ", title) AS proj_title, 0 AS a FROM project p1
        UNION ALL
        SELECT DISTINCT projectNo, process, 1 AS a FROM process p2
    ) t
ORDER BY projectNo, a, title

And table:

<table class="paginated" style=" margin-right:10%;">
<?php
$header ='<th>Title</th>';
echo "<thead>$header</thead>";
while ($data = mysqli_fetch_assoc($query)) 
{
        $projectNo = $data['proj_title'];


        $body = '<td>'.$projectNo.'</td>';
        echo "<tbody>$body</tbody>";

}
?>    
</table>

The table looks like this:

|     title       |
+-----------------+
| Project test    |
| ANM             |
| BLD             |
| Project test2   |
| ANM KEY         | 
| BLD             | 

Is there any way to style only certain rows like: | Project test || Project test2 |

How it can be done?

2
  • Why not add that asterisks in the echo?Or add a class to the td that holds it Commented Mar 10, 2017 at 2:25
  • so every td is going to be contained by a tbody? Commented Mar 10, 2017 at 2:31

1 Answer 1

2

Yes, you certainly can style individual rows in a table with CSS. Your provided structure doesn't actually include any table rows (<tr> elements), so you might want to fix that up first. <tbody> is not a valid child of <td>, and <td> must be contained inside a <tr>.

Once you fix up your table structure, you can target every nth row with the pseudo-selector :nth-of-type:

tr:nth-of-type(3n) {
  color: #f00;
}
<table>
  <tr>
    <td>One</td>
  </tr>
  <tr>
    <td>Two</td>
  </tr>
  <tr>
    <td>Three</td>
  </tr>
  <tr>
    <td>Four</td>
  </tr>
  <tr>
    <td>Five</td>
  </tr>
  <tr>
    <td>Six</td>
  </tr>
</table>

In the above sample, 3n represents that it should target every third row, and change the colour.

You could also alternatively add a class to the specific rows that you wish to target through PHP itself, and then simply target that class directly with CSS:

echo "<tr class='red'><td>$data</td></tr>";

.red {
  color: #ff0;
} 

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

3 Comments

what if it is not exactly every 3rd row. it can be different. it depends on amount of processes inside each project
Then you would need to alter you PHP to assign the class specific to the elements that you wish to target, based on some sort of conditional (which only you would know). Check my second option, which adds the class red to specific rows. You'll need to use that within a condition that specifies which rows to target :)
Thanks for an idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.