Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a link from a CSV

Tags:

php

csv

This codes pulls data from a CSV file and displays them as a table

<?php
                $lines = file('graphdata/Dimensions.csv');      

                foreach ($lines as $lineNum => $line) {
                    if($lineNum == 0) {
                        continue;
                    }
                    print "         <tr id=\"tr" . $lineNum . "\">";

                    $tokens = str_getcsv($line);
                    print "<td style=\"width: 200px;\">" . trim($tokens[0])  "</td>";           
                    print "<td style=\"width: 100px;\">" . trim($tokens[1]) . "</td>";  
                    print "</script>\n";
                }
            ?>

The first column is actually an hyperlinked text while the link itself may appear as a cell in the relevant row in the CSV

Is there away to make the text in the first column to appear as hyperlink?

Thanks

like image 682
user3387046 Avatar asked Dec 06 '25 07:12

user3387046


1 Answers

General syntax:

echo '<a href="' . $linktTarget . '">' . $linkName . '</a>';

More clear way to do it is just to combain '' and "":
edit: Oh you wanted to whole <td></td>, be the linke, here you go:

              <?php
                    $lines = file('graphdata/Dimensions.csv');      

                    foreach ($lines as $lineNum => $line) {
                        if($lineNum == 0) {
                            continue;
                        }
                        print '<tr id="tr' . $lineNum . '">';

                        $tokens = str_getcsv($line);
                        print '<td style="width: 200px;"><a href="' . trim($tokens[0]) . '">' . trim($tokens[0]) . '</a></td>';           
                        print '<td style="width: 100px;">' . trim($tokens[1]) . '</td>';  
                        print '</script><br />';
                    }
                ?>

Also what for is:

print '</script><br />';

at the end?

I have found that the previous way wasn't working in anything other than IE, simplest way to do it across all browsers, is to just add below to your css file:

CSS code:

td a{width:100%;display:block;}
like image 151
fsn Avatar answered Dec 07 '25 19:12

fsn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!