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?
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?
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.
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.
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'].
'</a></td>';
// 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:
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.
PHP for a very long time has allowed inexact function parameter variables and juggled any incorrect values to the required type.
Example:
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.
Edit:
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
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