Populate picklist from directory and return file name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Brashars

    Populate picklist from directory and return file name

    Hi all,

    I have been searching for a week and am unable to find and example to
    "Populate picklist from directory and return file name".

    I have a php script that reads a log file and plots a graph. Right now, the
    log name is hard coded. The logs are archived each day in the form of say
    ddmmyy.log and I would like to put a drop down list in the script that would
    allow my script to run off the "log" picked or default to the current "hard
    coded" name.

    I thought this would be a really common thing but I guess not.

    I would be open to a javascript or asp script as long as I could "feed"

    the file name to my php script.

    Can anyone point me to a script like this??

    Thanks a million!




  • Sandman

    #2
    Re: Populate picklist from directory and return file name

    In article <rInVe.14457$ct 5.12705@fed1rea d04>,
    "Mike Brashars" <mike@apayrolls ervice.com> wrote:
    [color=blue]
    > Hi all,
    >
    > I have been searching for a week and am unable to find and example to
    > "Populate picklist from directory and return file name".
    >
    > I have a php script that reads a log file and plots a graph. Right now, the
    > log name is hard coded. The logs are archived each day in the form of say
    > ddmmyy.log and I would like to put a drop down list in the script that would
    > allow my script to run off the "log" picked or default to the current "hard
    > coded" name.
    >
    > I thought this would be a really common thing but I guess not.
    >
    > I would be open to a javascript or asp script as long as I could "feed"
    >
    > the file name to my php script.
    >
    > Can anyone point me to a script like this??
    >
    > Thanks a million![/color]

    <?
    # Directory
    $dir = "/path/to/a/dir/";

    print "<select name='file'>";
    while (false !== ($file = readdir($dir))) {
    if (in_array($file , array(".", ".."))) continue;
    print "<option value='$file'>$ file</option>";
    }
    print "</select>";

    ?>



    --
    Sandman[.net]

    Comment

    • sunbum

      #3
      Re: Populate picklist from directory and return file name

      Thanks Sandman!

      It seems it doesn't quite like the print statements ......

      Parse error: parse error, unexpected T_STRING in line 5, also in 7, it
      doesn't like the "." in the array statement .... unexpected "."

      Using php 4.3, IIS5

      Thank you!

      Comment

      • Sandman

        #4
        Re: Populate picklist from directory and return file name

        In article <1126620330.013 385.212480@g43g 2000cwa.googleg roups.com>,
        "sunbum" <mbrashars@gmai l.com> wrote:
        [color=blue]
        > Thanks Sandman!
        >
        > It seems it doesn't quite like the print statements ......
        >
        > Parse error: parse error, unexpected T_STRING in line 5, also in 7, it
        > doesn't like the "." in the array statement .... unexpected "."
        >
        > Using php 4.3, IIS5
        >
        > Thank you![/color]


        I'm terribly sorry, one line was missing. This:

        $dir = opendir($dir);

        Should go before the while loop.

        --
        Sandman[.net]

        Comment

        • sunbum

          #5
          Re: Populate picklist from directory and return file name

          Here is what I have now ... (without the line numbers)

          1)<?
          2) # Directory
          3) $dir = "\inetpub\wwwro ot\templogs\";
          4)
          5) print "<select name='file'>";
          6) $dir = opendir($dir);
          7) while (false !== ($file = readdir($dir))) {
          8) if (in_array($file , array(".", ".."))) continue;
          9) print "<option value='$file'>$ file</option>";
          10) }
          11) print "</select>";
          12)
          13)
          14)?>

          Still gives "Parse error: parse error, unexpected T_STRING in (script)
          on line 5

          :(

          Mike

          Comment

          • sunbum

            #6
            Re: Populate picklist from directory and return file name

            Found it!

            oh dummy me, it was the "\" in my path needed to be $dir =
            "/inetpub/wwwroot/templogs/";

            now it works perfectly!!! Thank you!!

            One last thing to bug you..... what holds the final selection of the
            file? is it $file[select] ?

            and what would signal that I am done selecting and want to do something
            with the value?

            Thanks so very much again!!

            Mike

            Comment

            • Steve

              #7
              Re: Populate picklist from directory and return file name

              [color=blue]
              > One last thing to bug you..... what holds the final selection of the
              > file? is it $file[select] ?[/color]
              [color=blue]
              > and what would signal that I am done selecting and want to do something
              > with the value?[/color]

              Make it into a form, with a submit button. See any HTML forms tutorial
              for details.

              ---
              Steve

              Comment

              • Sandman

                #8
                Re: Populate picklist from directory and return file name

                In article <1126621984.221 410.309530@g44g 2000cwa.googleg roups.com>,
                "sunbum" <mbrashars@gmai l.com> wrote:
                [color=blue]
                > Here is what I have now ... (without the line numbers)
                >
                > 1)<?
                > 2) # Directory
                > 3) $dir = "\inetpub\wwwro ot\templogs\";
                > 4)
                > 5) print "<select name='file'>";
                > 6) $dir = opendir($dir);
                > 7) while (false !== ($file = readdir($dir))) {
                > 8) if (in_array($file , array(".", ".."))) continue;
                > 9) print "<option value='$file'>$ file</option>";
                > 10) }
                > 11) print "</select>";
                > 12)
                > 13)
                > 14)?>
                >
                > Still gives "Parse error: parse error, unexpected T_STRING in (script)
                > on line 5[/color]

                Strange. It works like a charm for me. Maybe you got some strange invisible
                character caught on that line?

                --
                Sandman[.net]

                Comment

                • sunbum

                  #9
                  Re: Populate picklist from directory and return file name

                  No, it worked great!

                  I think our messages just crossed.

                  It was the back slashes in my file path. Your code was perfect :)

                  Do you have any pointers on this....
                  [color=blue]
                  > One last thing to bug you..... what holds the final selection of the
                  > file? is it $file[select] ?
                  > and what would signal that I am done selecting and want to do something
                  > with the value?[/color]

                  Thanks for all the patients and great help Sandman! you're the best

                  Mike

                  Comment

                  • Sandman

                    #10
                    Re: Populate picklist from directory and return file name

                    In article <1126636683.912 701.98450@g47g2 000cwa.googlegr oups.com>,
                    "sunbum" <mbrashars@gmai l.com> wrote:
                    [color=blue]
                    > No, it worked great!
                    >
                    > I think our messages just crossed.
                    >
                    > It was the back slashes in my file path. Your code was perfect :)
                    >
                    > Do you have any pointers on this....
                    >[color=green]
                    > > One last thing to bug you..... what holds the final selection of the
                    > > file? is it $file[select] ?
                    > > and what would signal that I am done selecting and want to do something
                    > > with the value?[/color][/color]

                    You are outputting a form, that will look something like this:

                    <select name='file'>
                    <option value='bonjour. txt'>bonjour.tx t</option>
                    <option value='hello.tx t'>hello.txt</option>
                    </select>

                    When you submit that form, it will be put in $_GET["file"] on the receiving PHP
                    script.


                    --
                    Sandman[.net]

                    Comment

                    • sunbum

                      #11
                      Re: Populate picklist from directory and return file name

                      Thanks Sandman,

                      Could you glance at this and see if I'm going in the right direction?

                      <?php
                      <html>
                      <head><title>Co oler Log Graph</title></head>
                      <body>


                      <form action="coolerl inec.php" method="post">;
                      # Directory
                      $dir = "/inetpub/wwwroot/templogs/";

                      print "<select name='file'>";
                      $dir = opendir($dir);
                      while (false !== ($file = readdir($dir))) {
                      if (in_array($file , array(".", ".."))) continue;
                      print "<option value='$file'>$ file</option>";
                      }
                      print "</select>";


                      <input type="submit" value="Submit"> ;
                      </form>

                      </body>
                      </html>
                      ?>

                      Thanks again!

                      Anyone else is more than welcome to chime in too! The main function is
                      from "Sandman" and works great, I am now just trying to incorporate it
                      into a form. Obviously I am greener than grass ;)

                      Mike

                      Comment

                      • sunbum

                        #12
                        Re: Populate picklist from directory and return file name

                        Sorry for replying to my own post.

                        Here is the latest version and I think it's closer.

                        Can someone help me clean up the syntax?

                        Thanks!
                        ------- Code begin (this is NOT in the script ------------------

                        <?php
                        if (isset($_POST['file']))
                        {
                        // process the data, see if it's valid.
                        // if it's valid and you're satisfied, do whatever you want with
                        the data
                        // if not, start outputting the form

                        $file = "/inetpub/wwwroot/templogs/"+$_POST['file'];
                        // Display the Graph
                        displaygraph();
                        }

                        else {
                        // display the form
                        displayform();
                        }

                        <?php
                        function displaygraph() {
                        echo "So far so good, this would draw graph with $file";

                        }


                        ?>

                        <?php
                        function displayform() {


                        <form name="cooler" action="<?php $server['PHP_SELF']?>"
                        method=\"post\" >";
                        $dir = "/inetpub/wwwroot/templogs/";

                        echo "<select name='file'>";
                        $dir = opendir($dir);
                        while (false !== ($file = readdir($dir))) {
                        if (in_array($file , array(".", ".."))) continue;
                        print "<option value='$file'>$ file</option>";
                        }
                        </select>;


                        <input type="submit" value="Submit"> ;
                        </form>

                        }


                        ?>


                        ?>

                        ------- Code end (this is NOT in the script ------------------

                        Mike

                        Comment

                        • Sandman

                          #13
                          Re: Populate picklist from directory and return file name

                          In article <1126718878.324 648.246160@g49g 2000cwa.googleg roups.com>,
                          "sunbum" <mbrashars@gmai l.com> wrote:
                          [color=blue]
                          > Sorry for replying to my own post.
                          >
                          > Here is the latest version and I think it's closer.
                          >
                          > Can someone help me clean up the syntax?
                          >
                          > Thanks!
                          > ------- Code begin (this is NOT in the script ------------------
                          >
                          > <?php
                          > if (isset($_POST['file']))
                          > {
                          > // process the data, see if it's valid.
                          > // if it's valid and you're satisfied, do whatever you want with
                          > the data
                          > // if not, start outputting the form
                          >
                          > $file = "/inetpub/wwwroot/templogs/"+$_POST['file'];
                          > // Display the Graph
                          > displaygraph();
                          > }
                          >
                          > else {
                          > // display the form
                          > displayform();
                          > }
                          >
                          > <?php
                          > function displaygraph() {
                          > echo "So far so good, this would draw graph with $file";
                          >
                          > }
                          >
                          >
                          > ?>
                          >
                          > <?php
                          > function displayform() {
                          >
                          >
                          > <form name="cooler" action="<?php $server['PHP_SELF']?>"
                          > method=\"post\" >";
                          > $dir = "/inetpub/wwwroot/templogs/";
                          >
                          > echo "<select name='file'>";
                          > $dir = opendir($dir);
                          > while (false !== ($file = readdir($dir))) {
                          > if (in_array($file , array(".", ".."))) continue;
                          > print "<option value='$file'>$ file</option>";
                          > }
                          > </select>;
                          >
                          >
                          > <input type="submit" value="Submit"> ;
                          > </form>
                          >
                          > }
                          >
                          >
                          > ?>
                          >
                          >
                          > ?>
                          >
                          > ------- Code end (this is NOT in the script ------------------
                          >
                          > Mike[/color]


                          The above is a good start. But you can't just start typing "<form name..."
                          inside a function block. You need to use

                          print "<form name='cooler' ....";


                          I always do my web app logic like this:


                          <?
                          while (1){
                          if ($_POST["file"]){
                          # Do fancy stufff
                          break;
                          }

                          # output form
                          $dir = "/inetpub/wwwroot/templogs/";
                          # ...

                          break;
                          }

                          --
                          Sandman[.net]

                          Comment

                          Working...