alternating row colors - php

I am looking for a solution to use the alternating colors row design in my table which is from a csv.
my code:
<?php
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n
<thead>
<tr>
<th>data</th>
</tr>
</thead>";
$f = fopen("local/datafiles.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr class='odds'>";
foreach ($line as $cell) {
echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table>";
?>
so how can i have the tr class to alternate from odds and even?
thanks

have a variable
int count =0;
increment count on every iteration and take modolous by
count % 2
if(count % 2 == 0)
color = red //(make color of row = red)
else
color = yellow //(make color of row = yello)
count=count+1;
thats just an idea, u can accomodate this in ur code

You could use the css pseudo-selector :nth-of-type(odd|even)
http://reference.sitepoint.com/css/pseudoclass-nthoftype
If the ID of the table is styledTable, your stylesheet would look like:
#styledTable {
text-align: left;
width: 99%;
border: 1px solid black;
}
#styledTable tr:nth-of-type(even) {
background-color: red;
}
#styledTable tr:nth-of-type(odd) {
background-color: blue;
}

<?php
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n
<thead>
<tr>
<th>data</th>
</tr>
</thead>";
$f = fopen("local/datafiles.csv", "r");
$i = 0;
while (($line = fgetcsv($f)) !== false) {
echo "<tr style='background-color: ".(($i%2)?'green':'blue').";'>";
foreach ($line as $cell) {
echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
$i++;
}
fclose($f);
echo "\n</table>";
?>
Or you could assign a class just the same way and style it using css. This is much better practice.

I will give you a better but much simpler solution, through CSS. First of all uniquely identify your table, i will show you a more general option.
table tr { backgroun: #ddd; }
table tr:nth-child(2n) { background: #ccc; }

Related

Hi, I am new to php and I cant find a way to make alternate table color in my code. It always show the wrong output that is required for me to follow

I did the nth child rule in css but it the colors are not alternate. Colors are all the same each rows and columns. My code is a multiplication table with a supposed alternate color. Im having a hard time knowing what I should do to modify or add in my php code to create the desired output (alternate colors in rows and columns in the multiplication table).
Below is my php code:
<?php
for($row=0; $row<=10; $row++){
echo "<tr>";
for($column=0; $column<=10; $column++){
if($row==0 && $column==0){
echo "<td></td>";
}
else{
echo "<td>". $row * $column. "</td>";
}
}
}
echo "</tr>";
?>
CSS code:
table, th, td {
border: 1px solid black;
}
td {
padding: 15px;
}
.bold{
font-weight: bold;
font-size: 1.3em;
}
table tr:nth-child(odd) {
background-color: yellow;
}
table tr:nth-child(even) {
background-color: red;
}
you forget to use the <table> tag and your CSS address it from the table.
<?php
echo "<table>";
for($row=0; $row<=10; $row++){
echo "<tr>";
for($column=0; $column<=10; $column++){
if($row==0 && $column==0){
echo "<td></td>";
}
else{
echo "<td>". $row * $column. "</td>";
}
}
echo "</tr>";
}
echo "</table>";
?>

X & Y Marking Table Using Php

I'm trying to make an x and y marking table using PHP. I used array and loops to this task.
Please refer to the sample code and image. I almost spent 2hrs finding some solutions still need help.
Very much appreciated for some ideas.
<?php
$x_arr = array();
$y_arr = array();
$n_arr = array();
$x_arr = [5,5,9,14];
$y_arr = [1,4,3,3];
$n_arr = [1,1,1,1];
$x = 16;
$y = 4;
$gap = 3;
$data = "";
?>
<div style="padding:30px; background:#242424;">
<table style="border:20px solid #660000;" align="center">
<?php
//STRIP
for($i=$y; $i>=1; $i--){
?>
<tr style="background:#fff;">
<?php
for($ii=$x; $ii>=1; $ii--){
//echo "<td style='border:1px solid black;'>x".$ii." : y".$i."</td>";
for($iii=0; $iii<=count($x_arr); $iii++){
if(isset($x_arr[$iii]) && isset($y_arr[$iii])){
$x_tmp = $x_arr[$iii];
$y_tmp = $y_arr[$iii];
if($i==$y_tmp && $ii==$x_tmp){
$data= "<td style='border:1px solid black; width:50px; height:30px; text-align:center;'><span data-feather='circle'></span></td>";
echo $data;
}else{
$data = "";
}
}
}
if($data == ""){
echo "<td style='border:1px solid black;'>x".$ii." : y".$i."</td>";
}
//echo "<td style='border:1px solid black;'>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>";
//echo "<td style='border:1px solid black; width:50px; height:30px; text-align:center;'><span data-feather='circle'></span></td>";
if($ii>$gap){
$gap = $gap+$gap;
}
}
?>
</tr>
<?php
}
?>
</table>
</div>
Result Error :
Current Result
Expected Result :
Target Result

Matrix creation failed in PHP

I am trying to build a 4x4 matrix like structure in PHP using HTML table.
There can be n number of elements. So calling it 4x4 matrix is kinda appropriate. There can be any number of rows. But a row can have only 4 columns.
Here's what I am trying to make.
Fiddle for this structure is here.
And this is what I am getting as output.
PHP Code:
<table class="tablematrix content table-striped">
<?php
$total=7;
if($total%4==0)
{
$tr=$total/4;
}
else
{
$tr_temp=$total/4;
$tr=$tr_temp+1;
}
for($i=1;$i<=$tr;$i++)
{
echo '<tr>';
for($j=1;$j<=$total;$j++)
{
echo '<td>'.$j.'</td>';
}
echo '</tr>';
}
?>
</table>
CSS
<style>
.tablematrix {
border-collapse:collapse;
table-layout:fixed;
}
.tablematrix * {
height:50px;
width:50px;
min-width:50px;
min-height:50px;
margin:0px;
padding:0px;
}
.tablematrix th,
.tablematrix td {
text-align: center;
border: 1px solid #dddddd;
}
.tablematrix th {
font-weight: bold;
}
.tablematrix tbody > tr:nth-child(odd) > td,
.tablematrix tbody > tr:nth-child(odd) > th {
background-color: #f9f9f9;
}
</style>
Output here
I am kinda stuck here. Don't know how to proceed. Any suggestions?
This code should work
<table class="tablematrix content table-striped">
<?php
$total=7;
$tr=$total;
$count=1;
for($i=1;$i<=$tr;$i++)
{
echo '<tr>';
for($j=1;$j<=4;$j++)
{
echo '<td>'.$count.'</td>';
$count=$count+1;
}
echo '</tr>';
}
?>
</table>
Please try this. You are making a logical mistake. I didn't test my code. So let me know if my code is not working. Thanks.
You can check my ideone here https://ideone.com/STHC7H. Its showing your desired output. So as you said $total is the total number of elements instead of total number of rows the following code will give you correct result
<?php
$total=7;
if($total%4==0)
{
$tr=$total/4;
}
else
{
$tr_temp=$total/4;
$tr=$tr_temp+1;
}
$count=1;
for($i=1;$i<=$tr;$i++)
{
echo '<tr>';
for($j=1;$j<=4;$j++)
{
if($count==$total+1)
break;
echo '<td>'.$count.'</td>';
$count=$count+1;
}
echo '</tr>';
}
?>
I hope it will solve your problem
Check this code:
<table class="tablematrix content table-striped">
<?php
$row=6;
$col=4;
$total=$row*$col;
echo "<tr>";
for($i=1;$i<=$total;$i++)
{
echo "<td>".$i."</td>";
if($i%$col==0)
{ echo '</tr>';
if($i!=$total)
echo '<tr>';
}
}
?>
</table>
OR
<table class="tablematrix content table-striped">
<?php
$total=24;
echo "<tr>";
for($i=1;$i<=$total;$i++)
{
echo "<td>".$i."</td>";
if($i%4==0)
{ echo '</tr>';
if($i!=$total)
echo '<tr>';
}
}
?>
</table>
Amount of columns = "" => 4.
Amount of Rows = "" = ?
Look at this part of your code
echo '<tr>';
for($j=1;$j<=$total;$j++)
{
echo '<td>'.$j.'</td>';
}
echo '</tr>';
you have set $total to 7 so you are runing 7 times and creating 7 columns instead of row which would have made more sense so basically you need to transpose your matrix
Here you have:
<?php
$rows = 6;
$cols = 4;
for ($i = 1; $i <= $rows; $i++) {
echo '<tr>';
for ($l = 1; $l <= $cols; $l++) {
echo '<td>'.((($i - 1) * $cols) + $l).'</td>';
}
echo '</tr>';
}
?>
Tested, should do the job..
You can use this function. It can be optimised not to use $tmp variable
function createHtmlMatrix ($w, $h) {
$matrixHtml = '<table class="tablematrix content table-striped">';
$tmp = 0;
for ($i = 0 ; $i < $h ; ++$i) {
$matrixHtml .= '<tr>';
for ($j = 0 ; $j < $w ; ++$j) {
$matrixHtml .= '<td>'.(++$tmp).'</td>';
}
$matrixHtml .= '</tr>';
}
$matrixHtml .= '</table>';
return $matrixHtml;
}
echo createHtmlMatrix(4,5);
try this
<?php
$row = 6;
$column = 3;
echo "<table style = 'border: 1px solid black'>";
for($x= 0; $x<= $row; $x++){
echo "<tr>";
for($y=0; $y<= $column; $y++){
echo "<td>";
echo $y;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>

How to embed icons inside an HTML table obtained from a csv?

The php code below extracts the data from a .csv file and generates an HTML table. It works fine. I wonder if it is possible to embed in the html table some icons, such as a red or green dot (reddot.gif , greendot.gif) in correspondence of some specific words (example: red , green) in the csv file.
In other words, when in the csv file appear red or green in a specific column (example: column 3), should appear the reddot.gif or greendot.gif in the generated html file.
Thanks in advance. Mat
<?php
$row = 1;
if (($handle = fopen("example.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
// ------------- head row --------
echo '<td style="border-top: 1px solid rgb(111,180,224); border-left: 1px solid rgb(111,180,224); border-bottom: 1px solid rgb(111,180,224);" align="left" bgcolor="#0066cc" height="36" valign="middle" ><b><font color="#ffffff" size="2"> '.$value.' </font></b></td>';
}else{
// ------------- Generic row -------
echo '<td style=" border-bottom: 1px solid rgb(111,180,224);" sdval="9" sdnum="1040;" align="left" bgcolor="#ffffff" height="25" valign="middle"><font color="#000000" size="2"> '.$value.' </font></td>';
}
}
if ($row == 1) {
echo '</tr>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
echo '</center>';
fclose($handle);
}
?>
declare this function outside of any loops, just at the beginning of the if, or even at the beginning or ending of the script:
function img($img){ return "<img src='{$img}dot.gif'/>";}
this goes in the else:
$value = preg_replace(array('/red/i', '/green/i'), array(img('red'), img('green')), $data[$c]);
If you need more colors just follow the logic and add them in the two arrays inside of the preg_replace.
If you need to add attributes to the images add them in the function declared.
Hope it works for you
Please give this a try.
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
switch(strtolower(trim($value))){
case 'green': $value = '<img src="greendot.gif" '
.'alt="green" height="32" width="32">';
break;
case 'red': $value = '<img src="reddot.gif" '
.'alt="red" height="32" width="32">';
break;
// you can add other cases here like blue, triangle etc :)
}
}

Applying css to a php table

I have a generated php table which I would like to apply style in my style sheet, so for example top:15px, left:10px ect..., not sure how call the table and link it with css -
echo "<table border=1>";
for ($i=0;$i<count($calls);$i++){
for ($j=0;$j<count($days);$j++){
$k = $days[$i].$times[$j];
if (array_key_exists($k,$date)){
echo "<td colspan='{$date[$k][1]}'>".
"{$date[$k][0]}</td>";
$j+=$date[$k][1]-1;
}else
echo "<td style='color:gray'>$k</td>";
}
echo "</tr>";
}
echo "</table>";
any help much appreciated, thank you
echo '<table style="top: 15px; left:10px;">';
or
echo '<table class="someClass">';
and then use CSS
.someClass{
top: 15px;
left: 10px;
}
Give the table an id
echo "<table id=\"my_table\" border=1>";
And then use this in the <head> tag of the document:
<style type="text/css">
#my_table {
top: 15px;
...
}
</style>
echo "<table style=\"border: 1px; top:15px; left: 10px;\">";
Is this statement not working?
echo "<table border=1 style=\"top:15px; left:10px\">";
for ($i=0; $i<count($calls); $i++) {
for ($j=0;$j<count($days);$j++) {
$k = $days[$i].$times[$j];
if (array_key_exists($k,$date)) {
echo "<td colspan='{$date[$k][1]}'>".
"{$date[$k][0]}</td>";
$j+=$date[$k][1]-1;
} else {
echo "<td style='color:gray'>$k</td>";
}
}
echo "</tr>";
}
echo "</table>";

Categories