Jump to content

Hyperlink on email address from database element


MrsVodes

Recommended Posts

So this is the code prior to trying to add a hyperlink

echo "<p>Name: " . $item["first_name"] . " " . $item["last_name"] . "<br> Email: " . $item ["email"]  . "</p>";

I thought that putting the <a href around the $item['email'] would make the email address a hyperlink but it does not ... it skips the email addresses completely and hyperlinks the names because all fields are $item.

echo "<p>Name: " . $item["first_name"] . " " . $item["last_name"] . "<br> Email: " . "<a href=\"mailto: {$item ["email"]} \">"  . "</p>";   

Thank you for any assistance.

Link to comment
Share on other sites

First off, I would suggest eliminating all the escaping gymnastics. It is harder to read, more prone to error and not as clean as not doing it. As to your problem, you are missing the basic syntax of a hyperlink, the closing </a>.

Here is a cleaned up version. Take note of the single quotes on the elements.

echo "<p>Name:{$item['first_name']} {$item['last_name']}<br> Email: <a href='mailto: {$item['email']}'>{$item['email']}</a></p>";  

 

Edited by benanamen
Link to comment
Share on other sites

While Benanamen's re-write is a better approach I thought I could help you understand the original problem with this tid bit.  The reason your email address 'disappeared' is because when you built the anchor tag around it, you left out the anchor tag piece that shows you "the link" literal.  That's the part that Benanamen added during his re-write of your code, between the <a......> and the </a>.

Another good way (IMHO) of writing this code is to use heredocs construct which is great for outputting a lot of html and php code that is mixed in.

$code= <<<heredocs
<p>
Name: $item['first_name'] $item['last_name']
<br> 
Email: <a href="mailto:$item['email']">$item['email']</a>
</p>
heredocs
echo $code;

  

For more in the PHP manual on using 'heredocs' go HERE

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.