Saturday, February 14, 2009

Weekly Source Code: Create A Table With Hyperlinks In PHP

A friend is busy with a website and asked me how to create a table with hyperlinks using PHP/mySQL so in this Weekly Source Code edition I will explain just how.

First you need to connect to the database, for this you use the mysql_connect method, then you run your select SQL query and ouput the result in HTML.

See the sample code.

If you wish to open the result in a popup window then call a Javascript function from the anchor tag's onclick event, in the Javascript function you can open a window with window.open().

Please Note:

- I pass the record id to the updateuser.php page, this is so that the page know which user I wish to update.

- I refer to the record in the row via its field name, you can also refer via its record position in the result for example: $row[1] refer to "Firstname" field.

- This will display all the records in the table, you can add some paging to the table to only display like 10 records at a time.

- For basic PHP you output HTML this way where your PHP and HTML code get mixed, its generally a good idea to use a template framework like Smarty where application code is seperated from the presentation.

References:

PHP mySQL Functions
PHP Manual

No comments: