1

Can someone help me please? I need a loop for a checkerboard pattern project.

Right now i have this:

<?php
$uitvoer="<table summary=''>\n";    
$j=0; 
$uitvoer .= "\t<tr>\n";
for($i=0;$i<8;$i++)
{
 $uitvoer .= "\t\t<td class='kleur".(($i+$j)%2)."'>&nbsp;</td>\n";
}    
echo <<<END
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>    
<title>Page title</title>
<style type="text/css">
<!--
td
{
 width:  50px;
 height: 50px;
 border:4px groove red;
}
td.kleur0
{
 background-color: white;
}
td.kleur1
{
 background-color: black;
}  
-->
</style>    
</head>
<body>
$uitvoer
</body>
</html>
END;
?>

Then i get this: Situation now

So far so good.

But i need 8 rows with different lines and colors The result need to be like this: End result should be like this

How can i do that the easiest and fastest way in a loop or a array??

1 Answer 1

1

You just need a second loop that adds table rows (<tr>). The beginning of your file should look like this:

$uitvoer="<table summary=''>\n";

for ($j=0; $j < 8; $j++) {
  $uitvoer .= "\t<tr>\n"; 
  for($i=0;$i<8;$i++) {
   $uitvoer .= "\t\t<td class='kleur".(($i+$j)%2)."'>&nbsp;</td>\n";
  }
  $uitvoer .= "\t</tr>\n"; 
}

So, inside the loops, $i will hold your table cell (<td> tag) and $j will hold your table row (<tr> tag).

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

Comments

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.