0

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 = "&nbsp;";
        }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">&nbsp;&nbsp;'.$value.'&nbsp;&nbsp;</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">&nbsp;&nbsp;'.$value.'&nbsp;&nbsp;</font></td>';
        }
    }

    if ($row == 1) {
        echo '</tr>';
    }else{
        echo '</tr>';
    }
    $row++;
}

echo '</tbody></table>';
echo '</center>';   
fclose($handle);
}
?>
mattew
  • 203
  • 1
  • 5
  • 15

1 Answers1

0

Check if the current value is a URL and wrap it with a A HREF tag:

<a href='$value'>$value</a>    
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108