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.

Creating a variable based on folder?

Discussion in 'PHP' started by medialab, Aug 24, 2014.

  1. #1
    Hey Everyone,

    Hoping someone can help me out, I have searched everywhere and found a few bits and pieces but can't seem to put it together.

    I am trying to create 2 variables based on a folder structure - city and state really.

    Like this: http://www.website.com/state/city/

    Then on the page I want to create 2 variables:

    $state = folder1
    $city = folder2

    Is there an easy way to get this info? The folders won't really exist so I will need some sort of htaccess wildcard for them, but I am mostly concerned with the PHP part. Anyone have any ideas?

    Thanks very much!!
     
    Solved! View solution.
    medialab, Aug 24, 2014 IP
  2. #2
    the php part is depends on how the htaccess handle the query.
    if the htaccess is like this :
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^/(.*)/(.*)$ index.php?state=$1&city=$2 [L]
    Code (markup):
    so the php part will be like this :
    $state  = $_GET['state'];
    $city = $_GET['city'];
    PHP:
     
    hangbowl, Aug 24, 2014 IP
  3. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #3
    Hey Hangbowl,

    I was wondering if I mostly needed the htaccess, I just found a post that looks like I might be able to do most of it in the .htaccess,

    Really all of the files will be in root, I just want the 2 folders for SEO reasons,

    if I created a link like above (www.website.com/state/city/index.php?state=$1&city=$2) would it just grab the index file from root and the I use the GET (as you stated above) to display the city and state on page?

    Thanks again for your help!
     
    medialab, Aug 24, 2014 IP
  4. hangbowl

    hangbowl Well-Known Member

    Messages:
    228
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    143
    Digital Goods:
    2
    #4
    if the url is like this :

    http://www.website.com/west-state/current-city/

    so it means in your php file the value of
    state = west-state
    city = current-city
     
    hangbowl, Aug 24, 2014 IP
  5. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #5
    I was playing with your code, it doesn't seem to be working, I just get a 404 error?

    I really just want to make a link (fake) www.website.com/state/city/ and the use the PHP to get the 2 folders. It will just use index.php in the root.
     
    medialab, Aug 24, 2014 IP
  6. hangbowl

    hangbowl Well-Known Member

    Messages:
    228
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    143
    Digital Goods:
    2
    #6
    404 error means the htaccess is not correct.
    try this
    RewriteRule ^/(.*)/(.*)$ /index.php?state=$1&city=$2 [L]
    Code (markup):
     
    hangbowl, Aug 24, 2014 IP
  7. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #7
    Still 404, I am searching stack exchange and stuff for answers as well, I think your on the right track though!
     
    medialab, Aug 24, 2014 IP
  8. hangbowl

    hangbowl Well-Known Member

    Messages:
    228
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    143
    Digital Goods:
    2
    #8
    Yes, the logic is like that.

    try this
    RewriteRule ^/(.*)/(.*)/?$ /index.php?state=$1&city=$2 [L]
    Code (markup):
     
    hangbowl, Aug 24, 2014 IP
  9. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #9
    Hey Hangbowl,

    Got it working with this: RewriteRule ^(.*)/(.*)/?$ /index.php?state=$1&city=$2 [L]

    Only issue is state has a trailing / lol

    Any easy fix for that? It displays state/ and city as the variables lol
     
    medialab, Aug 24, 2014 IP
  10. hangbowl

    hangbowl Well-Known Member

    Messages:
    228
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    143
    Digital Goods:
    2
    #10
    I hope this will work, make another htaccess above that line.

    RewriteRule ^(.*)?$ /index.php?state=$1 [L]
    Code (markup):
     
    hangbowl, Aug 24, 2014 IP
  11. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #11
    That just broke it lol and I realized it is not adding the / to state, it is combining all of it so the state variable creates: state/city and the city variable does nothing
     
    medialab, Aug 24, 2014 IP
  12. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #12
    Got it!! RewriteRule ^(.*)/(.*)/ /index.php?state=$1&city=$2 [L]

    Creates a city and state variable!! Thanks so much for your help you saved me days of creating stupid folders!!
     
    medialab, Aug 24, 2014 IP
  13. hangbowl

    hangbowl Well-Known Member

    Messages:
    228
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    143
    Digital Goods:
    2
    #13
    AWESOME!!
    ^_^
     
    hangbowl, Aug 24, 2014 IP
  14. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #14
    Hey @hangbowl thanks again for your help yesterday! I was wondering if you might have a little more insight to share??

    I have the city state working, but I have another folder level for an article, so www.website.com/state/city/article-title/

    in the .htaccess I created this (the first 3 lines are your stuff):

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)/(.*)/ index.php?state=$1&city=$2
    RewriteRule ^(.*)/(.*)/post-title/ post.php?state=$1&city=$2

    the 4th line however works, but it shows the page index.php as the state, city works great - am I missing something? I assume the logic in line 3 is overriding line 4. if I get rid of line 3 4 works lol

    Thanks again for your help!!
     
    medialab, Aug 26, 2014 IP
  15. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #15
    Typed to soon! Got it working - thanks again and sorry for bothering ya!
     
    medialab, Aug 26, 2014 IP