How can I print a chess table in PHP without using for loops, is it possible to do it with conditional and logical statements only?
<table width = "270px" cellspacing = "0px" cellpadding = "0px" border = "1px" bordercolor = "gray">
<?php
$value = 0;
for($col = 0; $col < 8; $col++) {
echo "<tr>";
$value = $col;
for($row = 0; $row < 8; $row++) {
if($value%2 == 0) {
echo
"<td height=40px width=20px bgcolor=black></td>";
$value++;
}
else {
echo
"<td height=40px width=20px bgcolor=white></td>";
$value++;
}
}
echo "</tr>";
}
?>
You could use str_repeat:
<table width="270px" cellspacing="0" cellpadding="0" border="1px" bordercolor="gray">
<?php
$td = '<td height="40px" width="20px" bgcolor=';
$black = "$td'black'></td>";
$white = "$td'white'></td>";
echo str_repeat("<tr>" . str_repeat($black . $white, 4) . "</tr><tr>"
. str_repeat($white . $black, 4) . "</tr>", 4);
?>
</table>
One solution is to use build-in loops derived from some functions, here array_walk(only to avoid explicit for).But it the end the algorithm is the same. It's only a trick !
<?php
$arr = array_fill(0,64,1);
array_walk($arr,"construct");
array_walk($arr,"display");
function construct(&$item, $key)
{
$color=$key%2;
$i=($key-$key%8)/8 + 1;
if($i%2==0)
$color = ($color+1)%2;
$color = $color%2==0 ? "white" : "black";
$j=$key%8+1;
$item = new Square($i,$j,$color);
}
function display($item, $key)
{
if($key%8==0) echo "<br>";
echo $item." ";
}
class Square
{
public $i;
public $j;
public $color;
public function __construct($i,$j,$color)
{
$this->i=$i;
$this->j=$j;
$this->color=$color;
}
public function __toString()
{
return "[".$this->i.",".$this->j."]=".($this->color);
}
}
?>
Output:
[1,1]=white [1,2]=black [1,3]=white [1,4]=black [1,5]=white [1,6]=black [1,7]=white [1,8]=black
[2,1]=black [2,2]=white [2,3]=black [2,4]=white [2,5]=black [2,6]=white [2,7]=black [2,8]=white
[3,1]=white [3,2]=black [3,3]=white [3,4]=black [3,5]=white [3,6]=black [3,7]=white [3,8]=black
[4,1]=black [4,2]=white [4,3]=black [4,4]=white [4,5]=black [4,6]=white [4,7]=black [4,8]=white
[5,1]=white [5,2]=black [5,3]=white [5,4]=black [5,5]=white [5,6]=black [5,7]=white [5,8]=black
[6,1]=black [6,2]=white [6,3]=black [6,4]=white [6,5]=black [6,6]=white [6,7]=black [6,8]=white
[7,1]=white [7,2]=black [7,3]=white [7,4]=black [7,5]=white [7,6]=black [7,7]=white [7,8]=black
[8,1]=black [8,2]=white [8,3]=black [8,4]=white [8,5]=black [8,6]=white [8,7]=black [8,8]=white
For html grid, just adapt display.
function displayHtml($item, $key)
{
if($item->i==1 && $item->j==1)
echo '<table width="270px" cellspacing="0" cellpadding="0" border="1px" bordercolor="gray">';
if($item->j%8==1)
echo "<tr>";
echo "<td height=40px width=20px style='background-color:".$item->color."'></td>";
if($item->j%8==0)
echo "</tr>";
if($item->i==8 && $item->j==8)
echo "</table>";
}
I am learning php and in doing it I had a problem.
I have to insert in a table of the payment data taken from a second table but only after checking that the id of the person corresponds to the id of the person who made the payment.
<?php
3 Query ...
while($rowSocio = mysqli_fetch_array($mostraSocio)) {
$idSocio = $rowSocio['socio_id'];
$nomeSocio = $rowSocio['socio_nome'];
$cognomeSocio = $rowSocio['socio_cognome'];
echo "<tr>";
echo "<td>{$cognomeSocio}</td>";
echo "<td>{$nomeSocio}</td>";
$i = 0;
$print = "0";
while($row = mysqli_fetch_array($mostraOperazione)) {
$id = $row['socio_id'];
$nome = $row['socio_nome'];
$cognome = $row['socio_cognome'];
$importo = $row['opfin_importo'];
$quota = $row['quota_nome'];
if($idSocio === $id) {
if($quota === "Prima Rata" && $print !== "1") {
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "1";
} elseif($quota === "Seconda Rata" && $print !== "2") {
echo "<td></td>";
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "2";
} elseif($quota === "Terza Rata" && $print !== "3") {
echo "<td></td>";
echo "<td></td>";
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
$i++;
$print = "3";
} elseif($quota === "Quota Stagionale" && $print !== "4") {
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "4";
echo "<td class='align-rx'>€ {$importo}</td>";
} else {
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
$i++;
}
} else {
echo "</tr>";
$i++;
}
}
}
?>
so I only print the names of the people and not the payments they make
output:
enter image description here
I have a MySQL table of results of collected responses on a form that I want to output in a table.
The user rates certain variables such as Sleep from good (1) to bad (7) and each users results are on each row.
I did that successfully, but to make it more readable I want to colour code the scores based on score.
E.g., if you score 2 or below the table cell should be green, and if you score 6 or above it is coloured red.
There are 5 different variables being rated so not sure if the method I am using would work or if something more suitable.
$sql = "SELECT * FROM Responses";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
echo '<tr>';
if($row['Sleep'] <= 2)
{
echo "<td style='background-color: green;'>" . $row['Sleep'] . "</td>";
}
elseif ($row['Sleep'] >= 6)
{
echo "<td style='background-color: red;'>" . $row['Sleep'] . "</td>";
}
else
{
echo "<td>" . $row['Sleep'] . "</td>";
}
echo '</tr>';
}
No output
Maybe so
function getColor($number)
{
if ($numner <= 2)
return 'green';
else if ($numner > 2 && $var < 6)
return 'black';
else if ($number >= 6)
return 'red';
}
$sql = "SELECT * FROM Responses";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td>' . $row["Name"] . '</td>';
echo '<td style="background-color: "' . $getColor($row["Sleep"]) . '">' . $row["Sleep"] . '</td>';
}
You just put your different variables in an array
function getColor($number)
{
if ($number <= 2)
return 'green';
else if ($number > 2 && $number < 6)
return 'none';
else if ($number >= 6)
return 'red';
}
echo "<table>";
while ($row = mysqli_fetch_array($result))
{
//replace var* with your variable
$variables = array("Sleep", "var2", "var3", "var4", "var5");
echo '<tr>';
echo '<td>' . $row["Name"] . '</td>';
$i=0;
while($i<5)
{
$score=$row[$variables[$i]];
echo '<td style="background-color: ' . getColor($score). ';">' . $score . '</td>';
$i++;
}
echo '</tr>';
}
echo "</table>";
Please be lenient on me as I am yet to learn the intricacies of PHP coding. Still at baby steps.
I am trying to practice my PHP coding skills and started with trying to create a Marks aggregation system wherein school teachers can, visually get to know the ups and downs in student's performance, based on academic scores.
Scenario: Student's Marks are stored in MySQL table and the teacher needs to look at 30(assume) students' records. My thought is to show the marks by 'coloring the individual marks(foreground-only: the color of digits are to stand out for easier reference' based on a coloring system.
That is, for UT1(unit test 1), if a student has scored:
Greater than 81, score should displayed in green color.
Greater than 60 but below 80, score should displayed in orange color.
Simply put
Marks >= 81 and <=100 should be green in color(100 marks is the benchmark)
Marks >=61 and <=80 should be orange in color
Marks >=41 and <=60 should be red in color
My code trials until now have come up to here:
<?php
// color code function
function color_marks($marks) {
$color_code = "";
// echo "Marks: " .$marks;
/* if($marks>=0 && $marks<=40) { return $color_code = "FF0000";}
elseif($marks>=41 && $marks<=60) { return $color_code = "FF4444";}
elseif($marks>=61 && $marks<=80) { return $color_code = "FF8800";}
elseif($marks>=81 && $marks<=100) { return $color_code = "00FF00";}
*/
if ($marks >= 81) {
$color_code = "00FF00";
echo "<br />Returning color:".$color_code;
return $color_code = "00FF00";
}
elseif ($marks >= 61) {
return $color_code = "FF8800";
}
elseif ($marks >= 41) {
return $color_code = "FF4444";
}
else {
return $color_code = "FF0000";
}
echo "<br />Returning color:".$color_code;
//if($marks<=60) { return $color_code = "GREEN";} else return $color_code = "RED";
/* if($marks>=81 && $marks<=100) { return $color_code = "338800";}
else { if($marks>=61 && $marks<=80) { return $color_code = "FF8800";}
else { if($marks>=41 && $marks<=60) { return $color_code = "FF4444";}
else { if($marks>=0 && $marks<=40) { return $color_code = "FF0000";}
} }} */
}
// START DEFAULT DATATABLE
echo "<div class='panel panel-default'>";
echo "<div class='panel-heading'>";
echo "<h3 class='panel-title'>Marks of X<sup>th</sup> A - Section</h3>";
echo "</div>";
echo "<div class=panel-body'>";
echo "<table class='table datatable'>";
echo "<thead>";
echo "<tr>";
echo "<th style='text-align:center;'>Roll No.</th>";
echo "<th>Student Name</th>";
echo "<th style='text-align:center;'>Unit 1</th>";
echo "<th style='text-align:center;'>Unit 2</th>";
echo "<th style='text-align:center;'>Quarterly</th>";
echo "<th style='text-align:center;'>Unit 3</th>";
echo "<th style='text-align:center;'>Half-Yearly</th>";
echo "<th style='text-align:center;'>Unit 4</th>";
echo "<th style='text-align:center;'>Annually</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
// Get data from the table from here
include('../config.inc.php');
$sql_query="SELECT * FROM learn_php.2015_2016_test_marks";
$result = mysqli_query($connecDB, $sql_query);
$color_code = '';
while($row = mysqli_fetch_array($result)) {
echo " ut1_marks - ".color_marks($row['ut1_marks'])." - ".$row['ut1_marks'];
echo " ut2_marks - ".color_marks($row['ut2_marks'])." - ".$row['ut2_marks'];
// lets find what color the marks are
if ($row['ut1_marks'] >0 ) { $color_code = color_marks($row['ut1_marks']); }
if ($row['ut2_marks'] >=0 ) { $color_code = color_marks($row['ut2_marks']); }
/*
if ($row['quarterly_marks'] >=0 ) { $color_code = color_marks($row['quarterly_marks']); }
if ($row['ut3_marks'] >=0 ) { $color_code = color_marks($row['ut3_marks']); }
if ($row['half_yearly_marks'] >=0 ) { $color_code = color_marks($row['half_yearly_marks']); }
if ($row['ut4_marks'] >=0 ) { $color_code = color_marks($row['ut4_marks']); }
if ($row['annual_marks'] >=0 ) { $color_code = color_marks($row['annual_marks']); }
*/
// echo "<font color='#".$color_code."'>". $row['ut1_marks'] . "</font>";
// Ok, got the color codes - lets show the magic in the table
echo "<tr>";
echo "<td style='text-align:center;'>" . $row['student_uid'] . "</td>";
echo "<td>" . $row['student_name'] . "</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut1_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut2_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['quarterly_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut3_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['half_yearly_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut4_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['annual_marks'] . "</font>"."</td>";
echo "</tr>";
}
echo "</table>";
?>
Present scenario: Every field value is taking the color-input from the function. But, the color_code value is turning out be constant.
I've been trying to crack this for the past few days, but unable to do so.
Please, if someone can give me a second thought on what I had done wrong, that would be an enormous leap.
Thanks a ton in advance.
the roblem is where there is the punch commentator
i need when the column has INTER name or OUTER name to format the content of the $cell
with $cell=number_format($cell,2,',','.')
i'm just starting using php so don be too specific thanks
<?php
// printing table rows
$rigapadi = 1 ;
while($row = mysql_fetch_row($result))
{
echo "<tr>";
$rigapadi=$rigapadi+1;
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
if ($rigapadi % 2 == 0) {
# if column name = 'INTER' or 'OUTER' $cell = number_format($cell, 2, ',', '.');
echo "<td align=\"center\">$cell</td>";
} else {
echo "<td bgcolor=\"#E9EEF5\" align=\"center\">$cell</td>";
}
echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
echo "<p/>";
?>
The mysql_ are obsoltes. You must not use them. And please indent your code properly when you ask for help.
<?php
$rigapadi = 1;
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
$rigapadi++;
foreach($row as $name => $cell) {
if($rigapadi % 2 === 0) {
if($name === 'INTER' || $name === 'OUTER') {
echo '<td align="center">' . number_format($cell, 2, ',', '.') . '</td>';
} else {
echo '<td align="center">' . $cell . '</td>';
}
} else {
echo '<td align="center" bgcolor="#E9EEF5">' . $cell . '</td>';
}
}
echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
?>
Note that I use fetch_assoc to get the $name.
PS: <p/> does not exists and align and bgcolor and not valids but acceptable if this is a HTML code for an e-mail.