I need to add alternating row color to my table

Hi, I am a newbie learning PHP at school and I have an assignment to create a multiplication table which I have done with the code below:
Part of the task is to display the table with alternating color rows. Can you please help me to do that with the code below and I have included the list of requirements. I don’t think our lecturer knows how to do all this as he cant teach us…:slight_smile:
Really appreciate any help.

<html lang="en">

	<meta charset="utf-8">
	<title>Multiplication Table</title>
				<h1>Multiplication Table</h1>
			<table border ="1">
			//set alternating row colors.
			// Create first row of table headers
			echo '<tr>';
			echo '<th>&nbsp;</th>';
			for ($col = 1; $col <= 12; $col++):
				echo "<th>$col</th>";
			echo '</tr>';
			// Create the remaining rows
			for ($row = 1, $col = 1; $row <= 12; $row++):
				echo '<tr>';
			// First cell is a table header.
				if ($col == 1) {
					echo "<th>'$row</th>";
				while ($col <= 12) :
					echo '<td>' . $row * $col++ . '</td>';
				echo '</tr>';
			// reset column at the end of each row
				$col = 1;

Assignment Requirements

Alternate rows shall be colored using html attribute names
Every cell containing the result of the square of a number (1x1, 2x2, 3x3 etc) shall also have distinctive background using a html attribute name
Create your times table from 1 to12. (ie: 1x1 … 12x12) - for both times tables.
Please display only the result (i.e: 1, 2, 4, 144) for the FOR loop table and display the calculation and result (i.e: 1x1=1, 2x2=4, etc) for the WHILE loop table.
The table shall be appropriately marked up with column and row headers and a caption depicting which type of loop was used.
The page must be valid HTML5
The code for both loops shall be appropriately commented using PHPDoc, and normal commenting techniques
The variables must be named according to the conventions used within the PHP community
NOTE: for this exercise CSS3 selectors and properties are forbidden.

How will your lecturer know whether you have done it correctly, then?

In any case, as CSS3 selectors are forbidden (seems like a great idea, teach people how to not use up-to-date methods), I would look at the row counter you already have, check to see whether the current row is divisible by two, and use that information to decide how to colour the row.


What does this even mean? Are they wanting you to use in-line styles?

That’s a fail already.
The validator will tell you to “Use CSS instead”.

What nonsense!
Sorry not to me more helpful, but your Assignments Requirements don’t make a lot of sense to me. They seem keen on the idea of valid html and php convention, but dismissive of the role of css in styling and presentation, which by convention and best practice is its role.


Does it mean to use the html attribute bgcolor? Nobody should be using that anymore. They should be using CSS. I would question whether this course is really up-to-date and whether you really want to be learning this way, IMHO.


Again, that would not pass HTML5 validation, meaning fulfilling the requirements is impossible.
The only way to do this without (real) CSS and be HTML5 valid is to use in-line style attributes like <th style="background:#d00">
But would that be considered “Properties”? And again it’s a frowned upon, out-dated method.

Or; when they say CSS3, does that mean CSS 1 & 2 is OK? So you could use a background property and “normal” selectors from CSS1, but you can’t use CSS3 selectors specifically, like nth-child()?
It is all quite unclear exactly what they are asking of you.

I seems like they don’t know very much at all.

1 Like

That’s how I read it - OP is free to use CSS to set the colours, but has to use the row-count to decide the class rather than letting CSS3 make it easy. Obscure way of teaching them how to figure out whether the number is odd or even, perhaps.


If that is the case and you may use proper CSS, albeit excluding newer properties and selectors, then it does make a bit more sense.
It’s just a case of testing if each $row is odd or even, then applying a class to the <tr> accordingly.
Then for the squares, check if the $row === $col and again apply a class.

It’s just this bit that doesn’t sound right.

But the instructions keep saying to use html attributes, which leads me to think they want them to use the old attributes that used to be common before the use of CSS to style tables, which really concerns me about the course curriculum.

1 Like

…and then say it should pass html5 validation, which is not possible with those attributes included. :upside_down:
So if that is the case, I would be very concerned about the course, as they clearly don’t have a clue what they are talking about.

Well class is an html attribute, so that’s what I might use. :shifty:
But the wording says “html attribute names” which does not make sense to me.

Without any clarification from the OP (if they understand what the lecturer is asking for) we can’t say how to interpret the requirements.


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