anyone care to take a shot at a PHP question?

Last post 04-28-2009 4:20 PM by SynergyNT. 3 replies.

Sort Posts:

  • anyone care to take a shot at a PHP question?

    04-28-2009, 2:25 PM
    • Participant
      1,109 point Participant
    • threeo
    • Member since 03-30-2005, 10:26 PM
    • Posts 392

    a little off topic but.......

    in PHP i am building a link, actually two links; one to do a "sort high to low" and another to do a "sort low to high"

    a user could have selected multiple filter settings which are contained in the querystring. what i'm trying to do is create my two links and if the user selects "hi to low" i add "ORDER=htl" to the querystring and if they select "low to hi" i add "ORDER=lth" - easy enough. the problem i'm having is....let's say they select "hi to low" and then decide to go back, so they select "low to hi" - i end up with BOTH in the querystring. what i decided to do was a simple str_replace to wipe out any pre-existing lo-to-hi or -hi-to-lo sorts in the querystring. but my code is never removing the pre-existing item(s).

    here it is:

    <?php
    // - begin edit to add price sort hi to low / low to
    $strDomain = "<a href = 'http://www.mysite.com?";
    $strQueryString = $_SERVER['QUERY_STRING'];
    $strNewURL = str_replace("&pORDER=htl", "", $strQueryString); <------seems to never happen
    $strNewURL = str_replace("&pORDER=lth", "", $strQueryString); <------seems to never happen
    $strURL = $strDomain.$strNewURL;
    $dosorturl = $strURL . "&pORDER=htl'>H->L</a>";
    $dosorturl2 = $strURL . "&pORDER=lth'>L->H</a>";
    echo "      sort: " . $dosorturl . "|" . $dosorturl2;
    ?>
     

    any idea why this doesn't remove any pre-existing instances of "&pORDER=htl" or "&pORDER=lth"  ??? 

  • Re: anyone care to take a shot at a PHP question?

    04-28-2009, 2:57 PM
    • Participant
      901 point Participant
    • SynergyNT
    • Member since 04-17-2009, 1:27 PM
    • Kansas City
    • Posts 205

    Why are you assigning the value of strNewURL twice in a row?  It seems that in the first instance, you would replace "&pORDER=htl" from strQueryString with an empty string, but then in the second instance, you are replacing "&pORDER=lth" from the strQueryString with an empty string, so if strQueryString contained just "&pORDER=htl", then by way of your second instance, that would remain.  If you are wanting to account for the possibility of both being present, why not do it like so?

    $strNewURL = str_replace("&pORDER=htl", "", $strQueryString);
    $strNewURL2 = str_replace("&pORDER=lth", "", $strNewURL);

    Then you would need to change your strURL line as follows.

    $strURL = $strDomain.$strNewURL2;

    Please remember to Mark as Answer if this answered your question.
  • Re: anyone care to take a shot at a PHP question?

    04-28-2009, 3:34 PM
    • Participant
      1,109 point Participant
    • threeo
    • Member since 03-30-2005, 10:26 PM
    • Posts 392

     i tried it the way you suggested and it works. thanks.

    i have to admit....i don't quite understand why though. to me it seems like both ways should do the same thing but.....i ain't gonna waste no more time with it. you fixed the problem so i'm not going to investigate the logic. long as it works.

    thanks again 

  • Re: anyone care to take a shot at a PHP question?

    04-28-2009, 4:20 PM
    • Participant
      901 point Participant
    • SynergyNT
    • Member since 04-17-2009, 1:27 PM
    • Kansas City
    • Posts 205

    I won't go into too much depth since it doesn't seem like you are really concerned with the why, but you were assigning different values to the same variable in succession, so the second time, you basically overwrote what you had assigned the first time.

    Please mark the previous post as the answer since it resolved your issue.

    Please remember to Mark as Answer if this answered your question.
Page 1 of 1 (4 items)