Forum Moderators: coopster

Message Too Old, No Replies

got all files in directory - now the next step

if ( this == "any of the files" ) { do something; } - how?

         

al1911

2:39 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



i have gotten all the files in a directory and turned them into a list by doing this

function select_menu(){
echo '<select name="files">';
$count = "0";
$getDir = opendir("./thingy"); # Enter here the directory to read the contents of
while($filename = readdir($getDir)) {
if ($filename[0]!= "." && $filename[0]!= ".." ) {
echo "<option value=\"$filename\">$filename</option>";
$count++;
}
}
echo '</select>';
closedir($getDir);
}

select_menu();

now what i want to do is this...

$userinput = $_POST['data_from_form'];
if ( $userinput = "any of the documents listed by the function" ) {
do something; }
else { do something else }

obviously the above will not work
i don't know what to replace the Any Of The Documents with
i don't even think i'm using the right statements

please tell me if i'm not being clear about what i want to do
any help appreciated

jamie

5:08 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

you'd probably be best to change the select_menu() function slightly so that it returns an array instead of a fully formatted html select drop down.

something like

function select_menu()
{
$arr_files = array(); // initialise the array
$count = "0";
$getDir = opendir("./thingy");
while($filename = readdir($getDir))
{
if ($filename[0]!= "." && $filename[0]!= ".." )
{
array_push($arr_files, $filename); // add each filename to the array
$count++;
}
}
closedir($getDir);
return $arr_files; //
}

then when you call select_menu() assign it straight a variable:

$array_of_files = select_menu();

and you have a ready made array of all your filenames. you can either loop through it spitting out html forms or comparing if/else values. your previous function only made an html select menu, which limits your options for further 'fun' with the filenames.

understanding arrays and looping through them is one of THE most useful php commands. google for "php array tutorials" there's loads out there.

good luck

al1911

5:33 am on Jan 23, 2005 (gmt 0)

10+ Year Member



yeah, sorry, i posted up old code. i knew i had to convert the entried to an array somehow but just didn't know how - now i do :) thanks.

when i call the array to see if a user's post equals ANY of the entries in the array, is it as simple as going...

if ( $userinput == $array ) { do this; }

?

i will still search google for a tutorial on arrays
thanks for the help again

jamie

8:37 am on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



al,

foreach() would be the one to use

foreach ($arr_files as $k)
{
// now put code to test against $k
}

foreach allows you to loop through the array, doing something with each of the values.

($arr_files as $k) means you are telling foreach() to put the first value of $arr_files into the variable $k (you can choose $x, $y, $z - it's up to you).

the foreach() runs as many time as you have items in your array, and each time it assigns the next value in the array to $k. that way you loop through testing each value.

so in your case it would be something like

foreach ($arr_files as $k)
{
if ($userinput == $k)
{
// do something
}
}

or to spit out an html form
foreach ($arr_files as $k)
{
echo '<option value="' . $k . '">' . $k . '</option>';
}

(you'd have to add an opening and closing select tags for it to work).

good luck

al1911

10:10 am on Jan 23, 2005 (gmt 0)

10+ Year Member



thanks, that answers my question :)
thanks again

al1911

2:17 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



ok, i tried what you said and it isn't working for me
i've gone over the code for the last half hour and i;m about ready to give up
all this code is doing is returning an empty page when there is more script beneath it in the document that should be running and making echos
it's like the code is making the script exit prematurly
the folder being read contains files whose file name are dummy ip addressed
my ip is not one of the filenames
but even if it was, i should get the echo messages

here's the code

$adminRoot = "admin/";
$activityFile = $adminRoot."activity.txt";

$ipAddress = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$banStateFile = $adminRoot."banState.txt";
$banState = file_get_contents($banStateFile);

if ($banState == "1") {

$blockBanned = array();
$count = "0";
$getDir = opendir($adminRoot."banned");
while($filename = readdir($getDir)) {
if ($filename[0]!= "." && $filename[0]!= "..") {
array_push($blockBanned, $filename);
$count++; }
}
closedir($getDir);
return $blockBanned;

foreach ($blockBanned as $k) {
if ($ipAddress == $k) {
$activityContents = file_get_contents($activityFile);
$newActivityContents = $doDate." - ".$ipAddress." - Banned user loaded page<BR>\r".$activityContents;
$openActivity = fopen($activityFile,"w");
fwrite($openActivity,$newActivityContents);
fclose($openActivity);

include("includes/header.php");
echo "<BR><BR>You cannot participate in this chat room<BR>because the owner has banned your ip.";
include("includes/footer.php");
exit(); }
}
}

al1911

2:24 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



sorry, i just realised, i wacked a return(); in there which will end the entire script in the document
my question now is, can i just remove the line beginning with the return?

jamie

6:48 pm on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

there is no function definition in the above code, nor a call to an existing one, so the return line is obsolete.

this line:
if ($filename[0]!= "." && $filename[0]!= "..")

also has no meaning because the [0] doesn't refer to anything - that's what the $count value is for.

it would really help to read up on arrays and looping through them, then you'll be able to solve it easily yourself.

good luck