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>
Related
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;'>          </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
I have a question regarding hiding empty rows in my table.
I want to hide an entire row in the case a field says N/A which means the field is empty.
Example:
In the event that the field is empty or N/A, I want to hide the entire row. I will also welcome javascript solutions.
<tbody style="
white-space: nowrap;
">
<?php
foreach($student_subject_list AS $SSL)
{
$subject_name = $SSL["name"];
if(in_array($subject_name, array_keys($result)))
{
$total_score = 0;
$avg = count($result[$subject_name]);
$test_results = "";
$i = 1;
foreach($result[$subject_name] AS $SN)
{
if($i == 1)
{
$ie = "1<sup>st</sup>";
}
elseif($i == 2)
{
$ie = "<br>2<sup>nd</sup>";
}
elseif($i == 3)
{
$ie = "<br>3<sup>rd</sup>";
}
else
{
$ie = "<br>4<sup>th</sup>";
}
$test_results .= "$ie Test: $SN ";
$total_score += $SN;
$avg_score = $total_score/$avg;
$i++;
}
}
else
{
$total_score = $test_results = "N/A";
}
?>
<tr>
<td style="border: 1px solid black; font-size:11px;width:120px;white-space: nowrap;height:30px;"><?=$subject_name?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$test_results?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$avg_score?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$remark?></td>
</tr>
<?php
}
?>
</tbody>
Could you make the html where you create the row conditional on $test_results NOT being equal to "N/A"? Something like this:
<?php if(!$test_results === "N/A"){ ?>
<tr>
<td> etc etc
<td> etc etc
<td> etc etc
<td> etc etc
</tr>
<?php } ?>
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 :)
}
}
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; }