Search code examples
phphtmlhref

Cell Hyperlink in PHP using HTML


I searched for some answers, but nothing worked. I have an HTML table in my index.php and everything works fine. I now want to add a new <td> and want it to be a clickable link. This is the table now without the link:

<?php
    $file1 = "c:/tablename.txt";
    $file2 = "c:/tablestatus.txt";
    $file3 = "c:/tablelocation.txt";
    $file4 = "c:/userlist.csv";
    if(file_exists($file1) && file_exists($file2))
    {
        $line1= file($file1, FILE_IGNORE_NEW_LINES);
         $line2 = file($file2, FILE_IGNORE_NEW_LINES);
         $line3raw = file($file3, FILE_IGNORE_NEW_LINES);
         $line3 = array_map("utf8_encode", $line3raw );
         $line4 = file($file4, FILE_IGNORE_NEW_LINES);

         $html = '<table align="center">';
             $html .= '<tr><th width="460px"></th><th width="140px"></th><th width="340px"></th></tr>';
                for($i=0;$i<count($line1);$i++){
                    $html .= '<tr class="'.$line2[$i].'">'; 
                        $html .= '<td font-size:"90pt">'.$line1[$i].'</td>';
                        $html .= '<td font-size:"90pt">'.$line2[$i].'</td>';
                        $html .= '<td font-size:"90pt">'.$line3[$i].'</td>';
                        $html .= '</tr>';
                    }
                    $html .= '</table>';
                    echo $html;
                }else
                {
                    echo "Files missing.";
                }
?>

I found out how to make a cell as a link like this:

<td><a href="https://mypage.com" target="_blank">title</a>;</td>

That didn't work. This works:

$html .= '<a href="https://mypage.com\" target="_blank">'.$line1[$i].'</a>';

But obviously, it's not part of the table anymore. How can I put this hyperlink into the table?

If you need more information or I missed something, just tell. I hope we can solve this issue.


Solution

  • Here, it is:

     <?php
            $file1 = "c:/tablename.txt";
            $file2 = "c:/tablestatus.txt";
            $file3 = "c:/tablelocation.txt";
            $file4 = "c:/userlist.csv";
            if(file_exists($file1) && file_exists($file2))
            {
                $line1= file($file1, FILE_IGNORE_NEW_LINES);
                 $line2 = file($file2, FILE_IGNORE_NEW_LINES);
                 $line3raw = file($file3, FILE_IGNORE_NEW_LINES);
                 $line3 = array_map("utf8_encode", $line3raw );
                 $line4 = file($file4, FILE_IGNORE_NEW_LINES);
    
                 $html = '<table align="center">';
                     $html .= '<tr><th width="460px"></th><th width="140px"></th><th width="340px"></th></tr>';
                        for($i=0;$i<count($line1);$i++){
                            $html .= '<tr class="'.$line2[$i].'">'; 
                                $html .= '<td font-size:"90pt"><a href="https://mypage.com\" target="_blank">'.$line1[$i].'</a></td>';
                                $html .= '<td font-size:"90pt">'.$line2[$i].'</td>';
                                $html .= '<td font-size:"90pt">'.$line3[$i].'</td>';
                                $html .= '</tr>';
                            }
                            $html .= '</table>';
                            echo $html;
                        }else
                        {
                            echo "Files missing.";
                        }
        ?>