Using variable declared in URL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thetakerfan
    Senior Member
    • Jun 2000
    • 1095

    Using variable declared in URL?

    How would I go about have a script do a couple different things based on a variable in the URL without specifying each variable within the script?
    ex)
    lyrics.php?fed=fedname would display a dir listing of everything in the "fedname" directory (preferably split into tables, but I didn't even look for that yet, so I'll try that later), and
    lyrics.php?fed=fedname&name=wrestlername would display the file "wrestlername" in the "fedname" directory.
    I don't know if this is possible at all, but I'd figure I'd ask, I looked through the php docs but didn't see anything
  • JohnM
    Senior Member
    • May 2000
    • 622

    #2
    When something is passed via the querystring (?this=that), PHP automatically converts it to variables.

    For example, if you type "test.php?name=John", PHP would create a variable called $name with the value of John.

    Comment

    • thetakerfan
      Senior Member
      • Jun 2000
      • 1095

      #3
      I did not know that, thanks...but how would I be able to print a dir listing of "John", if the variable is "$name"?

      Comment

      • JohnM
        Senior Member
        • May 2000
        • 622

        #4
        Take a look at this:



        here's basically what'd you'd want to do:

        Code:
        $dir = opendir("/path/to/dir/$name");
        echo "<h2>Directory listing for $name</h2>\n";
        echo "Files:<br>\n";
        while ($file = readdir($dir)) {
             if ($file != "." && $file != ".." {
                  echo "<a href=\"/$name/$file\">$file</a><br>\n";
             }
        }
        closedir($dir);
        or you could do this:

        Code:
        $dir = dir("/path/to/dir/$name");
        echo "<h2>Directory listing for $name</h2>\n";
        echo "Files:<br>\n";
        while ($file = $dir->read()) {
             if ($file != "." && $file != ".." {
                  echo "<a href=\"/$name/$file\">$file</a><br>\n";
             }
        }
        $dir->close();
        [Edited by JohnM on 10-21-2000 at 03:53 PM]

        Comment

        • thetakerfan
          Senior Member
          • Jun 2000
          • 1095

          #5
          thanks for all your help so far John, I have it working except for one problem, the "." and ".." in the dir listing, I can't seem to get them to be removed, I get an error in that line of the file
          other than that, its working great

          Comment

          • JohnM
            Senior Member
            • May 2000
            • 622

            #6
            oops!

            change this:
            Code:
            if ($file != "." && $file != ".." {
            to
            Code:
            if ($file != "." && $file != "..") {

            Comment

            • chrispadfield
              Senior Member
              • Aug 2000
              • 5366

              #7
              yesterday i started learning php. I started on a nice intro book "PHP Fast and Easy Web Development" before i get onto my Wrox press book. Guess what the exact page i am on is about.. you guessed it .. listing a directory
              Christopher Padfield
              Web Based Helpdesk
              DeskPRO v3.0.3 Released - Download Demo Now!

              Comment

              • JohnM
                Senior Member
                • May 2000
                • 622

                #8
                I learned PHP w/o a book.. tho I am thinking about getting Professional PHP Programming...

                Comment

                • thetakerfan
                  Senior Member
                  • Jun 2000
                  • 1095

                  #9
                  I can not believe I didn't notice that

                  Comment

                  • Freddie Bingham
                    Former vBulletin Developer
                    • May 2000
                    • 14057
                    • 1.1.x

                    #10
                    I didn't use no steenken book either (backwards redneck double-negative used on purpose)

                    .. but it helps to know C

                    Comment

                    • chrispadfield
                      Senior Member
                      • Aug 2000
                      • 5366

                      #11
                      you guys are so great lol
                      Christopher Padfield
                      Web Based Helpdesk
                      DeskPRO v3.0.3 Released - Download Demo Now!

                      Comment

                      • thetakerfan
                        Senior Member
                        • Jun 2000
                        • 1095

                        #12
                        I'm learning vB in school, atleast thats what I signed up for anyway, damned public school system

                        Comment

                        • chrispadfield
                          Senior Member
                          • Aug 2000
                          • 5366

                          #13
                          you are lucky. We never got taught to use a computer at all let alone programming (and i am only 19 so it was not long ago!).

                          I always think it is hard to be taught to use a computer and especially to be taught to program somthing. Seems something you just have to learn yourself with the aid of books (or online manuals for the experts here )
                          Christopher Padfield
                          Web Based Helpdesk
                          DeskPRO v3.0.3 Released - Download Demo Now!

                          Comment

                          • JohnM
                            Senior Member
                            • May 2000
                            • 622

                            #14
                            I'm 13 and I learned PHP w/o any books... took me about 2 hours to read the manual on the basic stuff, and a couple days to get used to it...

                            Comment

                            • thetakerfan
                              Senior Member
                              • Jun 2000
                              • 1095

                              #15
                              well, the programming class is a joke, so it doesn't really matter. I only took it because I desperately needed an easy class in my schedule.

                              Comment

                              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                              Working...