How do you href on a php table

Hi All

Whats the best way to add live links to the data on a table.

For instance, the following table, i have outputting the company names from the database. How would i turn those company names into live links to view the company record if they are clicked on?

    echo "<td>" . $row['compname'] . "</td>";
    echo "<td>" . $row['address'] . "</td>";
    echo "<td>" . $row['street'] . "</td>";

Thanks in advance.

An anchor tag is the same wherever it is placed: <a href="...">link text</a>

Thanks, but how would i apply that inside the above?

If i put the following it breaks the page.

echo "<td>" . $row['<a href='compname'><a>'] . "</td>";

What are the keys of your $row[] array? Hint: try var_dump $arr.

I can’t imagine <a href='compname'></a> is a valid key.

Do you really have a table column named <a href='compname'><a> ?

It may look more like:-

echo '<td><a href="' . $row['url'] . '">' . $row['anchor'] . '</a></td>';

Goodness no haha. This is how it normally looks…

echo "<td>" . $row['compname'] . "</td>";

I was just trying to figure out how to make the company name a live link. Thanks for the suggestion. It has now given it the appearance of a live link but its giving me an error of undefined index?

Please can you suggest the proper way i should define the url. i’ve tried multiple variations of…
$row[‘readcompany.php?’] but its just not firing up.

You are using the name $row as the source array, which leads me to assume it is data pulled from a database.
If so the key (index) won’t be a file, like readcompany.php, it will be a table column name.
What should the URL be?
Where is that data coming from?

Ok thats quite interesting.

The data is coming from a table called “companies” and a the company name is in the column “compname”. The read page I’m trying to link to is readcompany.php.

Thanks in advance.

If the URL is static, then that’s easy, just write it as it is, as you would in straight HTML.

echo '<td><a href="readcompany.php">' . $row['anchor'] . '</a></td>';

But the url is a read page thats loaded and then populated via PHP from SQL based on the I’d of the record.

Try this before outputting the table. It should show the SQL $row results formatted with array items on a separate line:

echo '<pre>'; // adds line feeds to make it easier to read
echo '</pre>';
die; // halt browser execution

My blurb reworded to try and eliminate confusion :slight_smile:

OK. You can see how it’s easy to add the a href tags, and where to put them. You can see where the URL is, and you can see how to add the code to display the company name. It’s one extra step to add another display into the same line which shows the company id. Try that first, and then combine it to build the full URL for your href.