Jump to content

putting links into table


matthew9090

Recommended Posts

i'm back again and i am not very good with tables as the <tr> and <td> tags confuse me  :confused:

i need to put 2 sets of links into 2 columns in the table and they come out jumbled up.

 

<table>
<tr>
<th>column 1</th>
<th>column 2</th>
</tr>
<?php

$sites = array_map('trim',file('websites.txt')); //read the whole file into an array & trim the newline character from the end of each line
foreach ($sites as $link) {
   echo "<tr><td><a href='$link'>$link</a></td>";
}


$sites = array_map('trim',file('websites2.txt')); //read the whole file into an array & trim the newline character from the end of each line
foreach ($sites as $link) {
   echo "<td><a href='$link'>$link</a></td></tr>";
}

?>

</table>

 

Link to comment
https://forums.phpfreaks.com/topic/232566-putting-links-into-table/
Share on other sites

How do you expect it come out exactly? The way you have it set up right now you'll just have a ton of columns with one link inside each.

 

<tr> is a row. <td> is a column. Each <tr> (row) of a table can have multiple <td> (column) inside, and of course you have to end the <tr> in order to get a new row.

 

it needs to look like this:

Header

Header

website list 1

website list 2

website list 1

website list 2

 

 

and i've got this code:

 

<?php

$file1 = array_map('trim',file('websites.txt')); 
foreach ($file1 as $link) {
  echo "<a href='$link'>$link</a>";
}
?>
<?php

$file2 = array_map('trim',file('websites2.txt'));
foreach ($file2 as $link2) {
   echo  "<a href='$link2'>$link2</a>";
}
?>

 

but i don't know how to put it into a table that looks like above.

<table>
<tr>
<th>column 1</th>
<th>column 2</th>
</tr>
<?php

$file1 = array_map('trim',file('websites.txt'));
$file2 = array_map('trim',file('websites2.txt'));

for($i=0; $i<count($file1); $i++) {
    echo "<tr>\n";
    echo "  <td><a href='$file1[$i]'>$file1[$i]</a></td>\n";
    echo "  <td><a href='$file2[$i]'>$file2[$i]</a></td>\n";
    echo "</tr>\n";
}

?>
</table>

Just make sure both files have the same number of links (or turn off warnings with error_reporting()).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.