www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Making URL easier to read...

  1. #1
    Join Date
    Dec 2011
    Posts
    163

    resolved [RESOLVED] Making URL easier to read...

    Hello, I am trying to convert a URL string into something that would be easier for me to read.

    My PHP with currently just output a link directly from the DB, such as index.php?subj=6#121213

    Subject 6 is "Events" and the ID is "121213"

    I would like to convert the link from
    Code:
    index.php?subj=6#121213
    to
    Code:
    Events #121213
    But I also have links like cat=2 and pg=1

    So here's what I need

    A function that...

    1. Will be able to tell is link is for a category (cat), subject (subj), or page (pg)
    2. Grab real name of the type from DB (subj=6 -> Events, cat=1 -> Home), but I already have a function that does this part
    3. Get the 6 digit (always will be 6 digits) ID number from link to display.

    I have looked through substr, strpos, and preg_match, but I cannot seem to figure out the best way to pull off [if($link contains "?subj="){connect to subject table}]

    Thanks for help.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    This is usually done via the web server, in Apache using mod_rewrite.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    163
    Quote Originally Posted by NogDog View Post
    This is usually done via the web server, in Apache using mod_rewrite.
    I'm not looking for a web server function, PHP can do this, I just currently cannot figure out how...

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    I believe (but have not tried) that you could use:
    Code:
    http://www.example.com/index.php/Event#123456
    ...and grab "Event" from $_SERVER['PATH_INFO']. You might need to trim() leading/trailing slashes, and if you want multiple parts, explode() on the slash to break them out. Not sure if you need to urldecode() it or not.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Dec 2011
    Posts
    163
    Sorry I must not have explained this well enough. The link is a reference, nothing click-able. I simply want to convert the link to something more easily readable.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Oh...I thought you were looking for a way to use clean URLs.

    Here's one way to do what I think you're doing (probably should add some defensive code in case any of the elements are missing):
    PHP Code:
    <?php

    $url 
    'index.php?subj=6#121213';
    $subjects = array(
        
    => 'Something',
        
    => 'Event'
    );
    $urlParts parse_url($url);
    parse_str($urlParts['query'], $get);
    echo 
    $subjects[$get['subj']].' '.$urlParts['fragment'].PHP_EOL;
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Dec 2011
    Posts
    163
    Since I don't understand parse_str() all that well, I used explode() instead...

    And with a little modification

    PHP Code:
    function announcement_link($link){
        global 
    $connect;
        
    // GET PARSED PARTS
        
    $link_parts parse_url($link);
        
    // MAKE SURE PATH IS RELAVANT TO WEBSITE
        
    if($link_parts['path'] === "index.php"){
            
    // GET SUBJ, CAT, AND PG FROM URL AND URL ID
            
    $type explode("=",$link_parts['query']);
            
    $type[0]; // cat, subj, or pg
            
    $type[1]; // db id
            
    $link_parts['fragment']; // url id
            // FIND WHICH TYPE IS IN $LINK AND GET ID AND MENU_NAME
            
    if($type[0] === "cat"){
                
    $sql "SELECT id, menu_name FROM categories WHERE id = ".$type[1]." LIMIT 1";
                
    $result mysqli_query($connect$sql);
                
    $row mysqli_fetch_assoc($result);
                
    mysqli_free_result($result);
            }elseif(
    $type[0] === "subj"){
                
    $sql "SELECT id, menu_name FROM subjects WHERE id = ".$type[1]." LIMIT 1";
                
    $result mysqli_query($connect$sql);
                
    $row mysqli_fetch_assoc($result);
                
    mysqli_free_result($result);
            }elseif(
    $type[0] === "pg"){
                
    $sql "SELECT id, menu_name FROM pages WHERE id = ".$type[1]." LIMIT 1";
                
    $result mysqli_query($connect$sql);
                
    $row mysqli_fetch_assoc($result);
                
    mysqli_free_result($result);
            }
            
    // DISPLAY FRIENDLY (ex. Events #122513)
            
    $output $row['menu_name']." #".$link_parts['fragment'];
            
        } else {
            
    $output $link;
        }
        return 
    $output;


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles