you can try
echo "<table>";
foreach($fileContentsArray as $one_link)
{
echo '<tr>';
list($name, $link) = explode(",", $one_link);
echo "<td><a href=\"$link\">$name</a></td>";
echo '</tr>';
}
echo "</table>";
Question
I am attempting to load a simple txt file, and print out it's contents on the page.
This text file basically just contains the sitemap of my website.
For example:
Home,index.php
Services,services.php
Register,register.php
Contact,contact.php
Login,login.php
My Account,my-account.php
And this is the code I have so far:
$fileContentsArray = file("sitemap.txt");
echo "<table>";
foreach($fileContentsArray as $one_link)
{
echo '<tr>';
$splitted = preg_split('/,/', $one_link);
foreach ($splitted as $one)
{
echo "<td>$one</td>";
}
echo '</tr>';
}
echo "</table>";
Although, this is printing out the file in the following format:
Home index.php
Services services.php
Register register.php
Contact contact.php
Login login.php
My Account my-account.php
I need the format to be slightly changed, so that the title of the page, links to the link provided. For example, Home must link to index.php and Services must link to services.php. I am not interested in just having the full anchor link tag within my sitemap.txt file since that just defeats the purpose.
I am completely stuck on this one, and have no idea on how to do this. I'm extremely new to PHP, and built my code piece from piece by searching the net.
All help is greatly appreciated.
Solution
you can try
echo "<table>";
foreach($fileContentsArray as $one_link)
{
echo '<tr>';
list($name, $link) = explode(",", $one_link);
echo "<td><a href=\"$link\">$name</a></td>";
echo '</tr>';
}
echo "</table>";
OTHER TIPS
You don't need the loop. Just change:
foreach ($splitted as $one)
{
echo "<td>$one</td>";
}
with:
echo "<td>
<a href=\"$splitted[1]\" title=\"$splitted[0]\">
$splitted[0]
</a>
</td>";
where an anchor (a
) is inserted in the cell (td
).