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.

1 Like

Assuming your database table contains a unique field name “id” and your readcompany.php script uses it as a GET parameter like so:

<?php /* readcompany.php - expects an integer GET parameter named "id" */

  // make sure id is passed into the script and convert it to an integer
if (!($id = (isset($_GET['id']) ? (int) $_GET['id'] : 0))) {
  echo 'Error: Company ID is required!';
else {
  // $id now contains at least a non-zero integer
  // read the company info from the database and display it if found

Then you can build your links in the calling script like so:

echo '<td><a href="readcompany.php?id='. $row['id']. '">'. $row['compname'].

  // or (harder to read IMHO)
echo "<td><a href="readcompany.php?id={$row['id']}">{$row['compname']}</a></td>";

Here’s a nice one-liner debug if you are just getting started:

echo '$row:<pre>'. print_r($row, 1). '</pre>';

Hope that helps

1 Like


It looks like declare(strict_types=1); was not declared because your script would have generated an error.

I include a Debug file with a function named fred(…) because it is easier to type:

file: /var/www/ci2/fred.php

	ini_set('display_errors', '1');

function fred
  $val       	= 'NO $val ???', 
  string $title = "Yes we have NO title"
  $prn = print_r($val, TRUE);
  $style = <<< ____EOT
	margin:2em auto; 
	background-color:#ffe; color:#00a; 
	padding: 0.88em;
	border: dotted 2px red;

  $result = <<< ____TMP
	<dl style="$style">
	  <dt>$title ==> </dt>
	  <dd> <pre > $prn </pre> </dd>
	echo $result;

	return '';


1 Like

Nice correction. I know of high level pro’s that didn’t know that “1” is not an acceptable return parameter. But then again, they never used strict_types.

1 Like

Sorry gents I thought I had replied. Thanks droopsnoot that worked a treat. :grin::+1:t2:

Thanks Jasteele that’s interesting. :+1:t2:

Thanks John. Please can you explain the purpose of declare strict types?

PHP for a very long time has allowed inexact function parameter variables and juggled any incorrect values to the required type.

Required type is integer but parameter passed is a string such as “12345”. The string would be converted to 12345 which is an integer.

PHP7 now has an optional declaration which validates the passed parameter and immediately raises an error if the type is not an exact match.

Compiled languages such as C and C++ require exact types otherwise compiler errors will prevent compilation. Compiled languages are usually a lot faster and hopefully PHP will introduce a compiler option which should drastically increase performance over the current line, by line interpreted language.

Please note the declaration is optional and also only applies only to the current file. This is essential because if the declaration was inherited the same as error_reporting(…); , ini_set(type, value); then any old included files would throw errors, preventing the application from running.

Omitting the declaration is cunningly backward compatible :slight_smile:

Yes, completely off-topic, and doesn’t help the original poster’s question at all. But since you picked out one single line of my post, please re-read the last 3 words of the sentence above it:

Here’s a nice one-liner debug if you are just getting started:

Yes, my example did not include strict types, but if someone doesn’t know how to create an anchor tag I can guarantee they aren’t either.

I’d venture to bet that 98%+ of existing PHP code would break with it declared (especially since it wasn’t added until 7.0.0). I love the fact that PHP doesn’t force type checking.

I know 5.x is end-of-life, but many shared hosting providers are still using it. I’m not condoning it, just stating a fact.

I’ve used a similar debug function (that doesn’t use echo and is usable for multiple output formats) for decades (PHP3) and have been a technical editor for multiple PHP books. Sometimes a quick non-strict one-liner beats a half-page of strict debug function, especially for a beginner.

I always found it ironic that the declare strict types uses = 1 when it’s obviously a boolean :stuck_out_tongue:

1 Like