1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

varialbe in URL string

Discussion in 'PHP' started by mnymkr, Jun 21, 2007.

  1. #1
    This is driving me insane

    I swear is seems like I am doing something right one day and wrong the next


    I am trying to pass a variable into a URL string in a while loop

    echo "<a href=$go'sort='$rows->name'&order=ASC>UP</a>";
    Code (markup):
    i have tried every combination

    <a href='.$go.'sort='.$rows->name.'&order=ASC>UP</a>
    Code (markup):
    <a href=$go'sort='$rows->name'&order=ASC>UP</a>
    Code (markup):
    <a href=$go'sort=$rows->name&order=ASC>UP</a>
    
    
    Code (markup):
    the last one almost works but the problem is that I can't take out the apostraphe after $go because it will make $gosort

    Can someone help me out and please explain why I think the first one should work.
     
    mnymkr, Jun 21, 2007 IP
  2. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    echo '<a href="' . $go . 'sort=' . $rows->name . '&amp;order=ASC>UP</a>';
    PHP:
    this should do :)
     
    UnrealEd, Jun 21, 2007 IP
    nevetS likes this.
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    echo "<th><a href=''.$go.'sort='.$rows->name.'&amp;order=ASC'>UP</a>"
    Code (markup):

    I am losing the end of it.....


    sort and order do not display
     
    mnymkr, Jun 21, 2007 IP
  4. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    the one i suggested should do just fine (i did forget a double quote after ASC though :():
    echo '<th><a href="' . $go . 'sort=' . $rows->name . '&amp;order=ASC'">UP</a>';
    PHP:
    you just have to make sure you're opening and closing the correct quotes: if you wrap the entire string in single quotes, you have to place the variables outside the string, otherwise the value of the variable won't be inserted into the string. If you use double quotes, it doesn't matter.
     
    UnrealEd, Jun 21, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    You know you should familiarize yourself with sprintf and family, you avoid all this nonsense when you use these functions.

    
    <?
    $go = "http://krakjoe.com/post-ajax.php?";
    $rows = new stdClass ;
    $rows->name = 'TESTING';
    
    printf( '<a href="%ssort=%s&order=ASC">UP</a>', $go, $rows->name );
    ?>
    
    PHP:
     
    krakjoe, Jun 21, 2007 IP
  6. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #6
    just learning here

    what are the advantage / disadvantages of either way?
     
    mnymkr, Jun 21, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    The biggest advantage for web programmers using sprintf over echo and print would be as you see here, I just wrote the url, no messing with '. $this .' sort of nonsense.

    There are others too, sprintf can format characters in many different ways without the use of auxiallary functions, for example changing integers to hex values, like rgb colours to hex representations of the same, which is useful for many things and is just a one liner

    
    <?
    printf("#%02X%02X%02X", 15, 15, 15 );
    ?>
    
    PHP:
    I had something else aswell but its gone out of my head and I've sat here for ten minutes thinking about what it was .... it'll come to me and I'll post it ....

    Just the fact that it should cut down your syntax errors and unexpected behaviour while building and manipulating strings is enough to use it right ?
     
    krakjoe, Jun 21, 2007 IP
  8. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Unless I misunderstand what you are trying to do, shouldn't there be something in the URL to indicate it contains arguments. For instance:
    
    echo '<a href="' . $go . '?sort=' . $rows->name . '&order=ASC">UP</a>';
    
    Code (markup):
     
    clancey, Jun 21, 2007 IP