SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hyperlinking a single variable with commas.

    Hello,

    I was wondering how I'd go about hyperlinking a single variable with commas.

    Well basically I have a variable '$tags', say that the output for $tags is:

    Code:
    circle, triangle, square
    I want it so that each tag is hyper-linked like so:

    HTML Code:
    <a href="/tag/circle/">circle</a>,
    <a href="/tag/triangle/">triangle</a>,
    <a href="/tag/square/">square</a>
    but I'm entirely puzzled as to how I'd achieve it.

    Thanks.
    Last edited by paulwilde; Apr 30, 2006 at 16:37.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    as normal, the comma wont affect the output (it shouldn't anyway!)

    PHP Code:
    echo '
    <a href="/tag/circle/">circle</a>,
    <a href="/tag/triangle/">triangle</a>,
    <a href="/tag/square/">square</a>'

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops! I made a mistake in my post. I meant tags is a variable, not an echo.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I meant tags is a varticle, not an echo.
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

  5. #5
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

  6. #6
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone have a clue?

    Sorry to be so troublesome, but I'm kinda needing this asap, heh.

  7. #7
    SitePoint Enthusiast Anne Thrax's Avatar
    Join Date
    Oct 2005
    Location
    Sydney, Australia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if I understood your problem but try this -
    PHP Code:
    $tags "circle, triangle, square";
    $tag explode(", "$tags);
    echo 
    "<a href=\"/tag/$tag[0]/\">$tag[0]</a>,
    <a href=\"/tag/
    $tag[1]/\">$tag[1]</a>,
    <a href=\"/tag/
    $tag[2]/\">$tag[2]</a>"

  8. #8
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or better still:

    PHP Code:
    $tags 'circle, square, diamond';
    $tags explode', '$tags );

    foreach( 
    $tags as $tag ) {
     echo 
    '<a href="/tag/'$tag'">'$tag'</a>';

    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  9. #9
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's almost it, but when I said circle, triangle, square I meant this could be a possible output of $tags.

    Basically I have the variable '$tags' inside of an echo, and $tags will output 'foo, bar' etc so I need 'foo, bar' etc to be separated into hyperlinks when it echoes '$tags'.

    You're on the right direction though.

    edit:
    The following code works perfectly, except I'm left with a leading comma on the end, how would I get rid of it?

    PHP Code:
    $tags "$tags"
    $tags explode(", "$tags); 

    foreach( 
    $tags as $tag ) { 
    echo 
    '<a href="/tag/'$tag'/''">'$tag'</a>'', ';



  10. #10
    SitePoint Enthusiast Anne Thrax's Avatar
    Join Date
    Oct 2005
    Location
    Sydney, Australia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show us your code so far, please? Still not understanding what you want to do.

    Edit: Sorry, posted this before seeing you had edited your previous post.

  11. #11
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 

    include 'db.inc.php';

    $result mysql_query('SELECT id, title, blogdate, blog, tags FROM blog order by id desc limit 10');
    if (!
    $result) {
        echo(
    '<div class="warning"><b>Error:</b> ' mysql_error() . '</div>');
    }

    while (
    $row mysql_fetch_array($result)) {
      
    $title=$row["title"];
      
    $blog=$row["blog"];
      
    $blogdate=$row["blogdate"];
      
    $tags=$row["tags"];
      
    $id=$row["id"];

    echo 
    "<h1><a href=\"/journal_entry/$id/\">$title</a></h1>\n
    <h2>
    $blogdate</h2>\n
    <h3><b>tags:</b> "
    ;

    $tags "$tags"
    $tags explode(", "$tags); 

    foreach( 
    $tags as $tag ) { 
    echo 
    '<a href="/tag/'$tag'/''">'$tag'</a>'', '

    }

    echo 
    "</h3>\n
    <p>
    $blog</p>\n\n";

    ?>

  12. #12
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for($i 0$i count($tags); $i++) {
        echo 
    '<a href="/tag/' $tags[$i] . '/">' $tags[$i] . "</a>\n";
        if(
    $i < (count($tags) - 1)) echo ", ";

    Last edited by trib4lmaniac; May 1, 2006 at 05:46.

  13. #13
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    trib41maniac: that just shows one character, and that's all..

  14. #14
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $tags explode(', '$tags);

    foreach( 
    $tags as $tag ) {
     
    $out .= '<a href="/tag/'.$tag.'/'.'">'.$tag.'</a>'.', ';

    $out substr$out0, -);
    echo 
    $out
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  15. #15
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Karl: That works - except when I have multiple entries, the tags begin to add the previous entries tags onto it until the bottom one is left with every single tag used.

  16. #16
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paulwilde
    trib41maniac: that just shows one character, and that's all..
    I don't see why it should, it's basically doing the same thing as the foreach.
    I've corrected a silly mistake in my if statement though.

  17. #17
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paulwilde
    Karl: That works - except when I have multiple entries, the tags begin to add the previous entries tags onto it until the bottom one is left with every single tag used.
    Not quite sure what you mean there, you mean if you have multiple blocks of this code in the same page? If so, then you just need to set $out = ''; after each block.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  18. #18
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Karl
    Not quite sure what you mean there, you mean if you have multiple blocks of this code in the same page? If so, then you just need to set $out = ''; after each block.
    Great, that fixed it.

    Thanks everyone.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •