Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Determine if Path Changed

    I am trying to find a way to set a flag if the file path changes.

    For example, if the user is initially here...
    Code:
    www.debbie.com/management/other/
    ...and then surfs to...
    Code:
    www.debbie.com/legal/tax-law/
    ...then I want to set a flag.


    Any ideas how to do this?

    Sincerely,


    Debbie

  • #2
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    This is what I have so far, but it isn't working...
    PHP Code:
    <?php
        $currentPath 
    substr($_SERVER['REQUEST_URI'], 0strpos($_SERVER['REQUEST_URI'], '?'));

        
    $lastPath = (isset($_SESSION['lastPath']) ? $_SESSION['lastPath'] : $currentPath);
        
    //    echo $_SESSION['lastPath'];
    //    exit();
        
        
    if ($currentPath == $lastPath){
            echo 
    "NO CHANGE";
        }else{
            echo 
    "CHANGE";
        }

    //    echo "<p>Section+Subsection Path = '$sectionSubsectionPath'";
    //    $_SESSION['lastPath'] = $sectionSubsectionPath;
        
        
    exit();
    ?>
    Sincerely,


    Debbie

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Print out the two variables and see what you get from each one
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #4
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    One thing I forgot to do was include session_start()

    Here is some updated code that sorta works, but still isn't quite right...
    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();
        
        
    $currentPath substr($_SERVER['REQUEST_URI'], 0strpos($_SERVER['REQUEST_URI'], '?'));

        
    $_SESSION['lastPath'] = (isset($_SESSION['lastPath']) ? $_SESSION['lastPath'] : $currentPath);

        echo 
    "<p>\$currentPath = '" $currentPath "'</p>";
        echo 
    "<p>\$_SESSION['lastPath'] = '" $_SESSION['lastPath'] . "'</p>";
            
        if (
    $currentPath == $_SESSION['lastPath']){
            echo 
    "NO CHANGE";
        }else{
            echo 
    "CHANGE";
        }
        
        exit();
    ?>

    If you go from...
    Code:
    www.debbie.com/finance/economy/
    to...
    Code:
    www.debbie.com/finance/markets/
    then it displays "CHANGE".


    However, if you then go back from
    Code:
    www.debbie.com/finance/markets/
    to...
    Code:
    www.debbie.com/finance/economy/
    then it displays "NO CHANGE" which is wrong.


    How can I fix this?


    Maybe there is a better way to code things than the approach I am taking?

    Sincerely,


    Debbie

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    You need to update $_SERVER['lastPath'], you could set it at the end of the page.

    Also, keep in mind if the session expires or the user clears their data this won't work, so if this is something that's important then you might want to consider a different method, just a heads up (in case you find it to not work sometimes).


    Also, you don't need to call exit(); if it's the end of the script, it's going to exit itself once the code is all finished.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #6
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    You need to update $_SERVER['lastPath'], you could set it at the end of the page.
    I assume you meant $_SESSION['lastPath']


    Quote Originally Posted by Dubz View Post
    Also, keep in mind if the session expires or the user clears their data this won't work, so if this is something that's important then you might want to consider a different method, just a heads up (in case you find it to not work sometimes).
    Well here is the larger issue I am trying to solve...


    When a user - logged in or not - lands on the Subsection page, they see a listing of Article Summaries and at the page top they see "Filter by" and "Sort by" drop-down menus.

    When I user chooses values for Filter and Sort and clicks "Go", then my script stores those parameters in the $_SESSION so that as they paginate through the listing, they can see what the Filter and Sort parameters are.

    Follow me so far?


    Well, all of that works fine, except for this scenario...

    If you are at...
    Code:
    www.debbie.com/finance/economy/?filter=editors-choice&sort=by-title&sortdir=asc&page=1
    And then you navigate to...
    Code:
    www.debbie.com/finance/markets/
    then the drop-down menus should reset back to Filter=None, and Sort=By-Date (Desc) however, the Filter and Sort parameters are still set to the *old* parameters which were stored in the $_SESSION.


    So even though you are looking at new data under the finance/markets subsection, if you look at the drop-down menus, you'd think the old Filter and Sort was being applied which is very deceptive!!


    To try and solve this issue, I was trying to write new code which looks at the <section>/<subsection> in the URL, and if it is different, then it would re-set the Filter and Sort drop-down boxes to their default values.

    Follow me?

    (If you can think of a better way to do this, then I'm all ears...)

    Sincerely,


    Debbie
    Last edited by doubledee; 05-13-2014 at 08:10 PM.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    I'm assuming you're saving this data to an array? Or are you just getting it from $_GET?

    If you're saving it to an array (ex. post data and update by the saved settings) then simply mark the array by the page name.
    For example:
    PHP Code:
    $page 'economy'//However you get the page, just substitute as needed
    $_SESSION['filter'][$page] = $filter_data//Whatever data you need saved

    //Simply call back that filter based on the page the same way you set it
    $filter $_SESSION['filter'][$page]; 
    This way the information is based on the page that you're on. If a user goes back and fourth between the pages, the filter will stay the same for each page.

    As for the $page variable, you'll have to build that yourself depending on how your layout is setup. It should be equal to the second sub-directory of the URI.

    If you wish to reset the filter if they change pages, you could also do it this way:
    PHP Code:
    $page 'economy'//Same as before
    if($page != $_SESSION['filter']['page'])
        
    $_SESSION['filter'] = array('page' => $page'filter' => '');
    //Update the filter in the variable $_SESSION['filter']['filter']; 
    This way, it clears the filter if the page you are on isn't the same, but keep in mind if they navigate away from the page it will clear the filter, even if they hit their browsers back button (although some browsers cache the entire page visited, it would need refreshed to show it reset).


    It all just depends on what you want it to do.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #8
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    This seems to be working as a prototype...
    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();
        
        
    $currentPath substr($_SERVER['REQUEST_URI'], 0strpos($_SERVER['REQUEST_URI'], '?'));

        
    $_SESSION['lastPath'] = (isset($_SESSION['lastPath']) ? $_SESSION['lastPath'] : $currentPath);

        echo 
    "<p>\$currentPath = '" $currentPath "'</p>";
        echo 
    "<p>\$_SESSION['lastPath'] = '" $_SESSION['lastPath'] . "'</p>";

        if (
    $currentPath == $_SESSION['lastPath']){
            echo 
    "NO CHANGE";
        }else{
            echo 
    "CHANGE";
            
    $_SESSION['lastPath'] = $currentPath;
            echo 
    "<p>\$_SESSION['lastPath'] = '" $_SESSION['lastPath'] . "'</p>";
        }
    ?>
    Sincerely,


    Debbie

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    That's good to hear!

    Also, as for the 5th line, you could change that to an if statement rather than setting it to itself. I'm not sure which is more efficient but if I had to guess I'd probably say the if statement is since it would skip setting it if it's the same. You could also combine it with the if statement you already use.

    Example:
    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();
        
        
    $currentPath substr($_SERVER['REQUEST_URI'], 0strpos($_SERVER['REQUEST_URI'], '?'));

        if (isset(
    $_SESSION['lastPath']) && $_SESSION['lastPath'] == $currentPath){
            echo 
    "NO CHANGE";
        }else{
            echo 
    "CHANGE";
            
    $_SESSION['lastPath'] = $currentPath;
            echo 
    "<p>\$_SESSION['lastPath'] = '" $_SESSION['lastPath'] . "'</p>";
        }
    ?>
    You're already using the if statement so it would be more efficient (that I know of) to combine it rather than doing it twice (sort of). Unless you need it separated for some reason, that's just my recommendation.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • Users who have thanked Dubz for this post:

    doubledee (05-14-2014)

  • #10
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    That's good to hear!

    Also, as for the 5th line, you could change that to an if statement rather than setting it to itself. I'm not sure which is more efficient but if I had to guess I'd probably say the if statement is since it would skip setting it if it's the same. You could also combine it with the if statement you already use.

    Example:
    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();
        
        
    $currentPath substr($_SERVER['REQUEST_URI'], 0strpos($_SERVER['REQUEST_URI'], '?'));

        if (isset(
    $_SESSION['lastPath']) && $_SESSION['lastPath'] == $currentPath){
            echo 
    "NO CHANGE";
        }else{
            echo 
    "CHANGE";
            
    $_SESSION['lastPath'] = $currentPath;
            echo 
    "<p>\$_SESSION['lastPath'] = '" $_SESSION['lastPath'] . "'</p>";
        }
    ?>
    You're already using the if statement so it would be more efficient (that I know of) to combine it rather than doing it twice (sort of). Unless you need it separated for some reason, that's just my recommendation.
    Ah, a perfectionist - just like me!

    I'll have to run that through Debbie's rigorous testing, but at first glance, YES, your way is coded "tighter" than my way.

    Thanks!

    Sincerely,


    Debbie

  • #11
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by doubledee View Post
    Ah, a perfectionist - just like me!
    Only difference between us is I put brackets on their own lines rather than group them with if or else statements and such, with the exception to do{ } while();, sometimes I'll put the while with the closing bracket, most of it's my CDO (It's like OCD but all the letters are in order like they should be) haha

    Quote Originally Posted by doubledee View Post
    I'll have to run that through Debbie's rigorous testing, but at first glance, YES, your way is coded "tighter" than my way.
    That sounds painful, but yes, tighter is always better

    Anyways, glad to hear you've gotten it working (hopefully)!
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #12
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    Only difference between us is I put brackets on their own lines rather than group them with if or else statements and such, with the exception to do{ } while();, sometimes I'll put the while with the closing bracket, most of it's my CDO (It's like OCD but all the letters are in order like they should be) haha
    Priceless!!


    Quote Originally Posted by Dubz View Post
    That sounds painful, but yes, tighter is always better
    Everything I do is painful! It's why after 4 years, I still don't have a working website. (But at least my code is bug-free!!)


    Quote Originally Posted by Dubz View Post
    Anyways, glad to hear you've gotten it working (hopefully)!
    Well, due to severe allergies - caused by Climate Change and the ultimate collapse of civilization as we know it - I've been in a "fog" all day.

    Am trying to get through this 1,000 lines of code in my script, but having hard time concentrating.

    In the a.m., will try and use your "tighter" code, and hopefully get this stupid script signed off on?!

    Thanks,


    Debbie

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by doubledee View Post
    It's why after 4 years, I still don't have a working website. (But at least my code is bug-free!!)
    I feel ya, it's annoying to get the little bugs out just to make stuff work and then it feels like it takes forever to do things and by the time you get half way done, half of it is either outdated or you get a new idea of how to do it and you start over, which is my main problem. That and the fact that I have a short attention span and work on multiple projects at once but never seem to finish them haha :P
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #14
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    That's good to hear!

    Also, as for the 5th line, you could change that to an if statement rather than setting it to itself. I'm not sure which is more efficient but if I had to guess I'd probably say the if statement is since it would skip setting it if it's the same. You could also combine it with the if statement you already use.

    Example:
    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();
        
        
    $currentPath substr($_SERVER['REQUEST_URI'], 0strpos($_SERVER['REQUEST_URI'], '?'));

        if (isset(
    $_SESSION['lastPath']) && $_SESSION['lastPath'] == $currentPath){
            echo 
    "NO CHANGE";
        }else{
            echo 
    "CHANGE";
            
    $_SESSION['lastPath'] = $currentPath;
            echo 
    "<p>\$_SESSION['lastPath'] = '" $_SESSION['lastPath'] . "'</p>";
        }
    ?>
    You're already using the if statement so it would be more efficient (that I know of) to combine it rather than doing it twice (sort of). Unless you need it separated for some reason, that's just my recommendation.
    Dubz,

    Taking a quick look at your code, it doesn't do the same thing mine does. (Compare Post #8 to your Post #9)

    For instance, when the script is first run, $_SESSION is blank and yet your code would echo "CHANGE".

    Because I need to get things done, and I know my version works, I'll stick with that.

    But thanks for trying to help!


    Debbie

  • #15
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    I feel ya, it's annoying to get the little bugs out just to make stuff work and then it feels like it takes forever to do things and by the time you get half way done, half of it is either outdated or you get a new idea of how to do it and you start over, which is my main problem. That and the fact that I have a short attention span and work on multiple projects at once but never seem to finish them haha :P
    Well, I'm very satisfied with my website, but it just takes forever to get it to the 95th percentile, but I think it's worth it.

    Not saying my code is infallible, but I almost never had bugs in my code, and when I do, my error-handling catches it gracefully. (As opposed to my last client where 80% of the day was doing bug fixes on the live system. I would *never* allow that kind of sloppiness on my website!)


    Debbie


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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