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 :)
}
}
Related
This is the output of my current code:
I was wondering if its possible to have the header on the left and the data from a csv file to be on the right?
For reference, here is my code:
<?php
$row = 1;
if (($handle = fopen("Book1.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
This is how my csv file looks like:
This can be done with a table with vertical headings, here is a basic example:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Vertical Headings:</h2>
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
<td>Steve Jobs</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 854</td>
<td>123 77 854</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 855</td>
<td>543 77 855</td>
</tr>
</table>
</body>
</html>
To populate the table with php you can do it like this:
$file = fopen("Book1.csv","r");
$data = array();
while($row = fgetcsv($file)) {
$data[] = $row; //Get all the data
}
if($data){
$n_columns = count($data[0]); //Get number of columns
}
echo '<table border="1">';
for ($col = 0; $col < $n_columns; $col++) {
echo '<tr>';
foreach ($data as $row_k => $row) {
if ($row_k == 0) {
echo '<th>';
} else {
echo '<td>';
}
echo $row[$col] ?? '';
if ($row_k == 0) {
echo '</th>';
} else {
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
fclose($file);
Result:
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>";
?>
The code below converts a .csv table into html: the content of every cell is shown as it is, plain text.
However I need to display a linked text in HTML, how can I do?
For example, my csv file has 4 columns as below:
_________________________________________________________________________
| SITENAME | URL | PAGENAME | URL2 |
--------------------------------------------------------------------------
| mysite |http://www.mysite.com| mynicepage | http://www.pg.com|
--------------------------------------------------------------------------
| hersite |http://www.site.com | hernicepage | http://www.ab.com|
--------------------------------------------------------------------------
The resulting HTML table should have 2 columns: SITENAME and PAGENAME. Each element of these two columns should open the corresponding link (in a new page) appearing in the right side. How to do?
Thanks in advance.
<?php
$row = 1;
if (($handle = fopen("test.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
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{
// ------------- body
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);
}
?>
Check if the current value is a URL and wrap it with a A HREF tag:
<a href='$value'>$value</a>
i'm using the code below to show a csv file as html table with php.
My issue is how to show only specific columns of the csv file. For example I'd show the columns number 1,5,9,15.
How can be modified the code to select that specific fields?
Thanks in advance, Mattew
<?php
$row = 1;
if (($handle = fopen("donors.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) {
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{
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);
}
?>
// before your while loop
$wantedColumns = array(1,5,9,15);
// ...
for ($c=0; $c < $num; $c++) {
if (!in_array($c,$wantedColumns)) continue;
// ....
turn your CSV string into an array:
$data_as_array = explode(',',$data_as_csv);
echo "This is the value in column #2: ".$data_as_array[1];
This solution doesn't consider yet the number of rows, this post does (see accepted answer):
How to create an array from a CSV file using PHP and the fgetcsv function
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; }