Avatar of Lennart Ericson
Lennart Ericson
Flag for Sweden asked on

How to get chosen background-color on every line?

I'd like to show a page where all dates from day_1 to day_n are populated with data from a database:
varv1_ny.php
This file prints background-colors; every second file should be colored #E5EDF6 the others should be colored white.
The date  field is colored correctly while the other fields are left white if there is no appropriate data for that very date existing in the database.
Please see http://ljushagen.se/lbs/public/general/demo.php 
I'd like to have every second line colored #E5EDF6 all its length whether or not data is existing.
How can that be done?
PHPCSSHTML

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

Please see the example here: https://www.w3.org/Style/Examples/007/evenodd.en.html

You would use CSS something like this
tr:nth-child(even) {background: #e5edf6}
tr:nth-child(odd) {background: white}

Open in new window

Julian Hansen

Look at the markup your page is generating for the <tr> (Rows) that have only 1 date in
<tr class="rand"><td class="style35" height="25px"><strong>2017-05-01</strong></td></tr>

Open in new window

There is only one column - you can style that until you are blue in the face it won't work. You have invalid markup which you should fix.
Here is the markup for a fully populated row
<tr class="rand"><td class="style35" height="25px"><strong>2017-05-12</strong></td>
<td><span class="style36">7</span></td>
<td><span class="style36">Peter</span></td>
<td><span class="style36">Avik</span></td>
<td><span class="style36">073-9832263</span></td>
<td><span class="style36"></span></td>
</tr>

Open in new window

You should be using either <td></td> empty cells on the first example to pad out the cells without data
OR
Use colspan on a new cell to make it span the other cols.
As you have it your row (<tr>) does not span the table and will therefore not carry the colour across.
In your PHP you can do this
Option 1 (Pad the cells)
if (!$ret1)
{ $err = "QUERY FAIL: ".$SQL1. ' ERRNO: '. $mysqli->errno.' ERROR: '.$mysqli->error; trigger_error($err, E_USER_ERROR); }
while ($row = $ret1->fetch_array()) {

        $fornamn = utf8_encode($row["fornamn"]);
		$efternamn = utf8_encode ($row["efternamn"]);
		$medlemsnr = $row['medlemsnr'];
		$telnr = $row['telnr'];
		$mobilnr = $row['mobilnr'];
		$dag_bev1 = $row['dag_bev1'];
		$dag_bev2 = $row['dag_bev2'];
		$bryggplats = $row['bryggplats'];

echo "
<td><span class=\"style36\">$bryggplats</span></td>
<td><span class=\"style36\">$fornamn</td>
<td><span class=\"style36\">$efternamn</span></td>
<td><span class=\"style36\">$telnr</span></td>
<td><span class=\"style36\">$mobilnr</span></td>
</tr>";
}
else {
	// Pad out the row with empty cells.
	echo <<< ROW
		<td></td><td></td><td></td><td></td><td></td>
ROW;
}

Open in new window

Option 2 (colspan)
if (!$ret1)
{ $err = "QUERY FAIL: ".$SQL1. ' ERRNO: '. $mysqli->errno.' ERROR: '.$mysqli->error; trigger_error($err, E_USER_ERROR); }
while ($row = $ret1->fetch_array()) {

        $fornamn = utf8_encode($row["fornamn"]);
		$efternamn = utf8_encode ($row["efternamn"]);
		$medlemsnr = $row['medlemsnr'];
		$telnr = $row['telnr'];
		$mobilnr = $row['mobilnr'];
		$dag_bev1 = $row['dag_bev1'];
		$dag_bev2 = $row['dag_bev2'];
		$bryggplats = $row['bryggplats'];

echo "
<td><span class=\"style36\">$bryggplats</span></td>
<td><span class=\"style36\">$fornamn</td>
<td><span class=\"style36\">$efternamn</span></td>
<td><span class=\"style36\">$telnr</span></td>
<td><span class=\"style36\">$mobilnr</span></td>
</tr>";
}
else {
 	// use colspan to fill out the row
	echo <<< ROW
		<td colspan="5"></td>
ROW;
}

Open in new window


You might also want to take a look at PHP's HEREDOC syntax for your HTML output - it will make it a lot easier as it supports both single and double quotes in the string (without escaping) as well as variable evaluation.
Lennart Ericson

ASKER
Julian Hansen,
Thanks for your reply.
I try to understand the expression

echo <<< ROW
            <td colspan="5"></td>
ROW;

My knowledge is seemingly too little. Would you, please, explain it.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Read the link Ray posted.
HEREDOC is PHP syntax that allows you to have single quotes, double quotes and variables without escaping
the syntax is
$variables = "variables";

//left side of expression (echo, variable)
$output =  <<< LABEL
<div>You can put "anything" you like here you don't have to escape quotes (', ") 
and you can include {$variables} as well.</div>
<p>Makes for much neater code and less errors</p>
<p>The LABEL can be any string - the closing LABEL must be identical to the 
opening LABEL and must be flush against the margin. Both labels must have
 no trailing spaces - even after the ';' (in the case of the closing).</p>
<p>That is all there is to it</p>
LABEL; // No spaces here newline immediately after '; and LABEL flush against the margin
echo $output;

Open in new window

Lennart Ericson

ASKER
Ray and Julian,
Thanks for your instructions and comments.

From Julian's first post I understand there must be five empty  cells added to the row should there not be anything appropriate for that day in the database. And I understand from your explanations of HEREDOC syntax the code starting with "<<< ROW..." does that.

When I apply the suggested code I get a 'syntax error code' message in my editor at the "else":

"<td><span class=\"style36\">$mobilnr</span></td>
</tr>";
}  
else... " <- this "else"

If you, please, take a look at that and suggest a code alteration, I'd appreciate it very much.

I tried to move the right hand bracket from
"error; trigger_error($err, E_USER_ERROR);  }"

and placed it just before the "else", it fills the row OK, but it doesn't print the text after the date.
I don't know what to do.
Julian Hansen

That was caused because I put the else after the closing for the while instead of the if should be
if (!$ret1) {
  $err = "QUERY FAIL: ".$SQL1. ' ERRNO: '. $mysqli->errno.' ERROR: '.$mysqli->error; trigger_error($err, E_USER_ERROR); }
  while ($row = $ret1->fetch_array()) {
    $fornamn = utf8_encode($row["fornamn"]);
    $efternamn = utf8_encode ($row["efternamn"]);
    $medlemsnr = $row['medlemsnr'];
    $telnr = $row['telnr'];
    $mobilnr = $row['mobilnr'];
    $dag_bev1 = $row['dag_bev1'];
    $dag_bev2 = $row['dag_bev2'];
    $bryggplats = $row['bryggplats'];
    echo <<< HTML
      <td><span class="style36">{$bryggplats}</span></td>
      <td><span class="style36">{$fornamn}</td>
      <td><span class="style36">{$efternamn}</span></td>
      <td><span class="style36">{$telnr}</span></td>
      <td><span class="style36">{$mobilnr}</span></td>
      </tr>
HTML;
  }
}
else {
 	// use colspan to fill out the row
	echo <<< ROW
		<td colspan="5"></td>
ROW;

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Lennart Ericson

ASKER
Julian and Ray,
Thanks for your excellent help!
Lennart (lericson)
Ray Paseur

For anyone coming upon this question in the future, the correct answer about alternating row colors is to use CSS.  For some reason this was not selected as a part of the answer.
https://www.experts-exchange.com/questions/29016902/How-to-get-chosen-background-color-on-every-line.html?anchorAnswerId=42097678#a42097678