471,624 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

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!


Sep 12 '05 #1
12 2147
In article <rInVe.14457$ct5.12705@fed1read04>,
"Mike Brashars" <mi**@apayrollservice.com> wrote:
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!


<?
# 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]
Sep 13 '05 #2
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!

Sep 13 '05 #3
In article <11**********************@g43g2000cwa.googlegroups .com>,
"sunbum" <mb*******@gmail.com> wrote:
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!

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

$dir = opendir($dir);

Should go before the while loop.

--
Sandman[.net]
Sep 13 '05 #4
Here is what I have now ... (without the line numbers)

1)<?
2) # Directory
3) $dir = "\inetpub\wwwroot\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

Sep 13 '05 #5
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

Sep 13 '05 #6
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?


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

---
Steve

Sep 13 '05 #7
In article <11**********************@g44g2000cwa.googlegroups .com>,
"sunbum" <mb*******@gmail.com> wrote:
Here is what I have now ... (without the line numbers)

1)<?
2) # Directory
3) $dir = "\inetpub\wwwroot\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


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

--
Sandman[.net]
Sep 13 '05 #8
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....
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 for all the patients and great help Sandman! you're the best

Mike

Sep 13 '05 #9
In article <11*********************@g47g2000cwa.googlegroups. com>,
"sunbum" <mb*******@gmail.com> wrote:
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....
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?


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

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

When you submit that form, it will be put in $_GET["file"] on the receiving PHP
script.
--
Sandman[.net]
Sep 14 '05 #10
Thanks Sandman,

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

<?php
<html>
<head><title>Cooler Log Graph</title></head>
<body>
<form action="coolerlinec.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

Sep 14 '05 #11
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

Sep 14 '05 #12
In article <11**********************@g49g2000cwa.googlegroups .com>,
"sunbum" <mb*******@gmail.com> wrote:
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

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]
Sep 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Johnston | last post: by
2 posts views Thread by 73blazer | last post: by
1 post views Thread by Andrew | last post: by
reply views Thread by Andrus | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.