Reputation: 49
I'm creating a table dynamically from a mysql database that gets fields name, email, city. The email column has emails that I'd like to have as "mailto:" links. Currently they display the email without the hyperlink. There's a 'filter' form on the page as well that when submitted will display only results that correspond with a name specified in the form textbox by the user.
I'm very novice at creating anything dynamic. What's the easiest way to display the email column as hyperlinks? I was thinking about using javascript to do a getElementByID loop for the second td of every tr and amend a "mailto:" the beginning using string manipulation. Maybe this is more practically done in PHP instead?
edit: I realized the obvious solution to my question. I simply concatenated the a href mailto: before the echo of the php command that gets my email field from the sql db and displays it in the table.
Upvotes: 0
Views: 151
Reputation:
$db = new PDO(//your dsn, host and password);
$query = $db->prepare('SELECT name,email from users');
$query->execute(); while($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo '<a href="mailto:"'.$row['email'].'">'.$row['name'].'</a>';
}
Upvotes: 0
Reputation: 5213
This is, indeed, more practically done in PHP. You can use something like the following, assuming that $result
contains the result of a MySQL SELECT
statement which fetches the users.
while ($u = $result->fetch_assoc()) {
// Output other data
// ...
echo '<a href="mailto:' . $u['email'] . '">' . $u['email'] . '</a>';
// ...
}
If you're still using mysql
instead of mysqli
(which you shouldn't really be doing), then replace
while ($u = $result->fetch_assoc()) {
with
while ($u = mysql_fetch_assoc($result)) {
Upvotes: 1