Announcement

Collapse
No announcement yet.

Using variable declared in URL?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #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


    • #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


      • #4
        Take a look at this:

        http://www.phpbuilder.com/manual/ref.dir.php

        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


        • #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


          • #6
            oops!

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

            Comment


            • #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


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

                Comment


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

                  Comment


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

                    .. but it helps to know C

                    Comment


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

                      Comment


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

                        Comment


                        • #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


                          • #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


                            • #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...
                              X