urls in database to html anchor links

Discussion in 'PHP' started by webboy, Feb 26, 2008.

  1. #1
    Hi all I have a MYSQL database field which as some urls in the following format

    http://www.webstite.com/
    http://www.webstite.com/

    each line is terminated with CR

    I wanted to know if there is a way when I output the field wrap the <a href=" code around it to make them links when I query the database
     
    webboy, Feb 26, 2008 IP
  2. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sure. Assuming that $row is the mysql_fetch_assoc for the resulting DB query and 'url' is the name of the field, just do this:

    
    echo "<a href=\"{$row['url']}\">Anchor Text</a>";
    
    PHP:
     
    The Critic, Feb 26, 2008 IP
  3. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ah thanks for that
    but I have one field with 30 URLs not 30 Rows with URLs
    hence this small problem
     
    webboy, Feb 26, 2008 IP
  4. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I suggest somthing like:
    
    //Assume that $row is the query result:
    $urls = explode("\n",$row);
    foreach($urls as $url){
        echo '<a href="'.$url.'">'.$url.'</a><br />';
    }
    
    PHP:
     
    selling vcc, Feb 26, 2008 IP
  5. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank You Thank You
    now you have me thinking
    i will explore the explode command ..
     
    webboy, Feb 26, 2008 IP
  6. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    Great as i was up late last night i wanted to now have the name in the url

    I now have the data like
    htt://www.xx.com,site name,
    htt://www.xx.com,site name,

    so at a guess the delimiter is , explode(",",$row);

    how do i output each string in in the correct places

    so i have output like<A href='http://www.xx.com'>site name</a>
     
    webboy, Feb 28, 2008 IP
  7. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is really something that should have been done using unique fields in a separate table. It would be much easier to add/delete/modify urls in the future and saves you the trouble of having to hack something together to make it work the way you want. Anyway...

    
    //Assume that $row is the query result:
    $urls = explode("\n",$row);
    $count=count($urls);
    for($i=0;$i=$i+2;$i<=$count)
    {
    $j=$i+1;
    $url=$urls[$i];
    $name=$urls[$j];
    echo "<a href=\"{$url}\">{$name}</a><br />";
    }
    
    PHP:
     
    The Critic, Feb 28, 2008 IP
  8. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Please try this:

    A combination of explode() and list();

    <?php 
    //Assume that $row is the query result:
    $row=
    "http://www.xx1.com,site1 name,
    http://www.xx2.com,site2 name,
    http://www.xx3.com,site3 name,
    http://www.xx4.com,site4 name,";
    
    $lines = explode("\n",$row);
    foreach($lines as $line){
    	list($href,$anchorText)=explode(",",$line);
        echo '<a href="'.$href.'">'.$anchorText.'</a><br />';
    }
    ?>
    PHP:
     
    selling vcc, Feb 28, 2008 IP