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>";  

 

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

Thank you both for your assistance. I am unable to get either of the responses to work.  It hits a page not working error on the first and has a formatting error in the second so I did not try to upload to URL.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.