I have a mysql database and use PHP to display the results. There is a field for a web address that is text. I would like to make this a clickable link that opens in a new window. Can anyone tell me how to do this? The code I'm using to display the link is: <? echo $result['url'];?> Thanks.
try relpacing that with <? echo "<a href='$result['url']' target='_BLANK'>$result['url']</a>";?> Code (markup):
I'd do it using concatenation: <?php echo "<a href=\"".$result['url']."\" target=\"_BLANK\">".$result['url']."</a>"; ?> Code (markup):
This is my way. <?php if($result['url']!="") { echo "<a href='".$result['url']."' target='_blank'>".$result['url']."</a>"; } else { echo "N/A"; } ?> Code (markup):
sprintf("<a href='%s' target='_blank' title=''>%s</a>", $result['url'],$result['url']); PHP: A easy way?
LOL, Why do you all confuse the guy with soo many methods? Just code this mate: <a href="<?php echo $result['url']; ?>" target="_blank">Link Name</a> Code (markup): Just so you know using a target within the link code will render it invalid, you can write a js script which you can call upon click to make a new window.
Not sure what you mean?.. render it invalid? I use TARGET=_BLANK all the time, it works great. Javascript version might be blocked by a pop-up blocker.
I like to do it like this <?php $a = '<a'; $b = ' href="'; $c = $result['url']; $d = '">'; $e = 'Link Name'; $f = '</a'; $g = '>'; echo $a.$b.$c.$d.$e.$f.$g; ?> PHP: Lol, when I used to freelance on scriptlance I swear some of the code looked just like this.
Ah, I see, I use: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> Code (markup): Tks
Lol this is true. I once worked on a site that would build a custom layout for every ip that accessed the site. Say if someone visited the site with the ip of 111.111.111.111 it would create a folder for the ip then create it's own templates for that ip. So the guy had thousands of folders and templates and he was complaining about his site running slow.
You guys wrote a lot of different methods for printing links from php. I think none way is the best, altough some might work faster or be written in less lines. The most important thing is to be consistent and to assure that it will be easy for you to update it when it will be needed.
<?php if(!empty($result['url]))echo '<a href="'.$result['url'].'" title='Tooltip text' target="_blank">Link Name</a>'; ?> Short, checks to make sure its not a bad link, and efficient.