Making a Clickable Link in PHP

Discussion in 'PHP' started by hooverweb, Jul 23, 2007.

  1. #1
    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.
     
    hooverweb, Jul 23, 2007 IP
  2. stOx

    stOx Notable Member

    Messages:
    6,426
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    230
    #2
    try relpacing that with
    <? echo "<a href='$result['url']' target='_BLANK'>$result['url']</a>";?>
    Code (markup):
     
    stOx, Jul 23, 2007 IP
  3. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #3
    I'd do it using concatenation:

    <?php
    echo "<a href=\"".$result['url']."\" target=\"_BLANK\">".$result['url']."</a>";
    ?>
    Code (markup):
     
    danielbruzual, Jul 23, 2007 IP
  4. in2clearsky

    in2clearsky Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is my way.

    
    <?php
    if($result['url']!="")
    {
    echo "<a href='".$result['url']."' target='_blank'>".$result['url']."</a>";
    }
    else
    {
    echo "N/A";
    }
    ?>
    
    Code (markup):
     
    in2clearsky, Jul 24, 2007 IP
  5. JPRuss

    JPRuss Peon

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How about just?

    <A TARGET="_BLANK" HREF="<?=$result['url'];?>"><?=$result['url'];?></A>
     
    JPRuss, Jul 24, 2007 IP
  6. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #6
    sprintf("<a href='%s' target='_blank' title=''>%s</a>", $result['url'],$result['url']);
    PHP:
    A easy way?
     
    exodus, Jul 24, 2007 IP
  7. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    HuggyCT2, Jul 24, 2007 IP
  8. JPRuss

    JPRuss Peon

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    JPRuss, Jul 26, 2007 IP
  9. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    target="_blank" is not valid XHTML.
     
    HuggyCT2, Jul 28, 2007 IP
  10. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #10
    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.
     
    bigrollerdave, Jul 30, 2007 IP
  11. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #11
    No way, the code is too neat and too close to naming and formatting conventions!
     
    krt, Jul 30, 2007 IP
  12. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I like it... Sure to use it on my scripts from now on :p
     
    HuggyCT2, Jul 30, 2007 IP
  13. JPRuss

    JPRuss Peon

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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
     
    JPRuss, Jul 30, 2007 IP
  14. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #14
    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.
     
    bigrollerdave, Aug 1, 2007 IP
  15. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    <a href="<? echo $result['url'] ?>" target='_BLANK'>>
    <? echo $result['url'] ?>
    </a>
     
    nagasharmi, Aug 1, 2007 IP
  16. pfek

    pfek Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #16
    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.
     
    pfek, Aug 1, 2007 IP
  17. Tarkan

    Tarkan Peon

    Messages:
    491
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #17
    <?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.
     
    Tarkan, Aug 1, 2007 IP
  18. sogastic

    sogastic Peon

    Messages:
    202
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #18
    :D. To confuse the customers later ?
     
    sogastic, Aug 1, 2007 IP