Page 2 of 3

Posted: Thu Oct 12, 2006 9:12 am
by volka
maybe the example was confusing.
try this one (without value attributes)

Code: Select all

<html>
	<head>
		<title>select test</title>
	</head>
	<body>
		<pre><?php print_r($_POST); ?></pre>
		<form method="post">
			<div>
				<select name="xyz">
					<option>fileA</option>
					<option>fileB</option>
					<option>fileC</option>
					<option>fileC</option>
				</select>
				<input type="submit" name="submit" />
			</div>
		</form>
	</body>
</html>
p.s.: feyd probably meant
for($i=1; $i=$file; $i++)
== comparision
= assignment

but the for/while loop combination was nonsense anyway ;)

Posted: Thu Oct 12, 2006 9:12 am
by feyd
The second parameter of a for loop is the "while" check. Normally, a comparison is done there. You have an assignment.

Posted: Thu Oct 12, 2006 9:34 am
by Obadiah
so...if i did this....shouldent it work similar to what you have

Code: Select all

<?php
session_start();
ini_set('error_reporting', E_ALL); 
ini_set('display_errors', 1); 

$dirname = "resids/{$_SESSION['logname']}";
$dh = opendir($dirname) or die("couldn't open directory");
echo "<pre>print_r($_POST)</pre><form method=\"post\">";
echo "<select name=\"xyz\">";
while(!(($file = readdir($dh)) === false))
{ 
		echo "<option";
		echo ">$file\n";
}
      echo "</select>";
echo "<input type=\"submit\" name=\"submit\" >";      
closedir($dh);

?>
it doesent...it looks as though it will but when you hit submit nothing prints to the screen...should i put a get in somewhere or return the value of something.....i dont get any errors....it just doesnt work

Posted: Thu Oct 12, 2006 9:46 am
by feyd

Code: Select all

<?php

session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

?>
<html>
        <head>
                <title>select test</title>
        </head>
        <body>
                <pre><?php print_r($_POST); ?></pre>
                <form method="post">
                        <div>
                                <select name="xyz">
<?php
$indent = str_repeat("\t", 5);
$dirname = "resids/{$_SESSION['logname']}";
if (!is_dir($dirname))
{
  echo $indent . '<option>Invalid directory specified.</option>';
}
else
{
  $dh = opendir($dirname) or die("couldn't open directory");
  while(($file = readdir($dh)) !== false)
  {
    if ($file != '.' and $file != '..')
    {
      echo $indent . '<option' . ((isset($_POST['xyz']) && $_POST['xyz'] == $file) ? ' selected="selected"' : '') . '>' . $file . '</option>' . PHP_EOL;
    }
  }
}

?>
                                </select>
                                <input type="submit" name="submit" />
                        </div>
                </form>
        </body>
</html>

Posted: Thu Oct 12, 2006 9:46 am
by Obadiah
i take it back...this works but what exactly is happening when i hit the submit button

Code: Select all

<?php
session_start();
ini_set('error_reporting', E_ALL); 
ini_set('display_errors', 1); 

$dirname = "resids/{$_SESSION['logname']}";
$dh = opendir($dirname) or die("couldn't open directory");
echo "<form action=\"post\">";
echo "<pre>print_r($_POST)</pre>";
echo "<select name=\"xyz\">";
while(!(($file = readdir($dh)) === false))
{ 
		echo "<option";
		echo ">$file\n";
}
      echo "</select>";
echo "<input type=\"submit\" name=\"submit\" >";      
echo "</form>";
closedir($dh);

?>
ok...i hit submit and it looks as though its trying to navigate....why?....wasnt it just supposed to print?

P.S....lol....what happened to the other post?

Posted: Thu Oct 12, 2006 9:50 am
by feyd
it's not trying to navigate as the code has no logic to support it right now. The example displays what it receives as submission data.

Posted: Thu Oct 12, 2006 10:17 am
by Obadiah
what does this do?

Code: Select all

PHP_EOL;
i think the purpose of this

Code: Select all

$indent = str_repeat("\t", 5);
and this

Code: Select all

if ($file != '.' and $file != '..') 
    { 
      echo $indent . '<option' . ((isset($_POST['xyz']) && $_POST['xyz'] == $file) ? ' selected="selected"' : '') . '>' . $file . '</option>' . PHP_EOL; 
    }
was to get rid of the annoying .,.. at the beginning, the if statement moreso checks to see what file is selected and post the value of whatevers selected in the xyz array....and how come you didnt get any errors for not putting \'s where it says selected

i know that isset checks to see whether a variable exist...which has me a bit confused....did you put a nested if inside the option tag?

another question....say if i changed dirname to be the value of xyz after this has executed....would it give an error....how could i post the value of xyz to the end of dirname?

Posted: Thu Oct 12, 2006 10:26 am
by feyd
PHP_EOL is a core constant that is defined as the local file system's carriage return, be it \r\n, \n or \r.

The str_repeat() call was to make adding multiple tabs as indentation a bit easier for later modification for whatever reason.

The latter quoted snippet contains several different things. You are correct in guessing the initial if strips out the . and .. directories; inside the echo line there is a ternary, which is used in combination with checking that there is a submission value to check against and adding a preselection if the posted value matches the current record.

As for adding navigation logic, on the most basic level it would simply be concatenating the submission directory to $dirname. There are security bits to consider however, such as making sure this new directory is still under the original so someone isn't able to navigate outside of the tree which you've set up.

Posted: Thu Oct 12, 2006 11:09 am
by Obadiah
ok....ive managed to do something along the lines of what i wanted to do...exept....it puts the info in the next folder in the same <select>....get this

Code: Select all

<?php 

session_start(); 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

?> 
<html> 
<head> 
<title>select test</title> 
</head> 
<body> 
<pre><!--<?php print_r($_POST); ?>--></pre> 
<form method="post"> 
<div> 
<select name="xyz"> 
<?php 
$indent = str_repeat("\t", 5); 
$dirname = "resids/{$_SESSION['logname']}"; 
if (!is_dir($dirname)) 
{ 
  echo $indent . '<option>Invalid directory specified.</option>'; 
} 
else 
{ 
  $dh = opendir($dirname) or die("couldn't open directory"); 
  while(($file = readdir($dh)) !== false) 
  { 
    if ($file != '.' and $file != '..') 
    { 
      echo $indent . '<option' . ((isset($_POST['xyz']) && $_POST['xyz'] == $file) ? ' selected="selected"' : '') . '>' . $file . '</option>' . PHP_EOL; 
      $dirname = "resids/{$_SESSION['logname']}/{$_POST['xyz']}";
      $dh = opendir($dirname) or die("couldn't open directory"); 
    } 
  }
  
  
} 

	
?> 
</select> 
<input type="submit" name="submit" /> 
</div> 
</form> 
</body> 
</html>
i hate it...its like one of those times you screw up and figure out what you did and at the same time not knowing what you did....AHHH!!!.....i think i know what i did and im gonna test something

Posted: Thu Oct 12, 2006 11:23 am
by feyd
a second opendir() isn' t needed, but the setting of $dirname should be done where it was originally.

Posted: Thu Oct 12, 2006 11:26 am
by Obadiah
ok....ive gotten to the next step....but i get this as an error
Warning: readdir(): 2 is not a valid Directory resource in C:\Program Files\xampp\htdocs\Log_In\agent\resid_report.php on line 27
also it doesnt print the entire contents of the folder of the first folder but it shows the contents of the one when you pull it up in the second folder shows the indentation but it doesnt print in the <select>

this is what i have

Code: Select all

<?php 
$indent = str_repeat("\t", 5); 
$dirname = "resids/{$_SESSION['logname']}"; 
if (!is_dir($dirname)) 
{ 
  echo $indent . '<option>Invalid directory specified.</option>'; 
} 
else 
{ 
  $dh = opendir($dirname) or die("couldn't open directory"); 
  while(($file = readdir($dh)) !== false) 
  { 
    if ($file != '.' and $file != '..') 
    { 
      echo $indent . '<option' . ((isset($_POST['xyz']) && $_POST['xyz'] == $file) ? ' selected="selected"' : '') . '>' . $file . '</option>' . PHP_EOL; 
      
      echo "<select name=\"foobar\">";
      $dirname = "resids/{$_SESSION['logname']}/{$_POST['xyz']}";
      $dh2 = opendir($dirname) or die("couldn't open directory");
      	while(!(($file = readdir($dh2)) === false)) 
		{ 
                
                echo "<option"; 
                echo ">$file\n"; 
		} 
      			echo "</select>"; 
        
		closedir($dh); 
 
    } 
  }
  
  
} 
?>
what am i missing?

[edited]
i just got your post feyd after i posted mine....much appologies...ill try that

Posted: Thu Oct 12, 2006 11:54 am
by Obadiah
sweet...worked like a charm...thanx feyd.....one more question though....after you get inside the last folder that actually contains files it says Invalid directory specified.....how do i get it to show the files in that folder and when the user selects that one....to actually open that file i tried this

Code: Select all

<?php 

session_start(); 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

?> 
<html> 
<head> 
<title>select test</title> 
</head> 
<body> 
<pre><?php print_r($_POST); ?></pre> 
<form method="post"> 
<div> 
<select name="xyz"> 
<?php 
$indent = str_repeat("\t", 5); 
$dirname = "resids/{$_SESSION['logname']}/{$_POST['xyz']}"; 
if (!is_dir($dirname)) 
{ 
  echo $indent . '<option>Invalid directory specified.</option>'; 
} 
else 
{ 
  $dh = opendir($dirname) or die("couldn't open directory"); 
  while(($file = readdir($dh)) !== false) 
  { 
    if ($file != '.' and $file != '..') 
    { 
      echo $indent . '<option' . ((isset($_POST['xyz']) && $_POST['xyz'] == $file) ? ' selected="selected"' : '') . '>' . $file . '</option>' . PHP_EOL; 
    } 
  }
if (file_exist(is_dir(xyz)))
{
$filename="['xyz']";
$fp = fopen($filename, "r") or die("Couldn't open $filename");
  
} 

	
?> 
</select> 
<input type="submit" name="submit" /> 
</div> 
</form> 
</body> 
</html>
should i instead have changed this block

Code: Select all

if (!is_dir($dirname)) 
{ 
  echo $indent . '<option>Invalid directory specified.</option>'; 
}
im thinking now the latter and am testing something out

Posted: Thu Oct 12, 2006 12:08 pm
by Obadiah
okie...i switched it up to this

Code: Select all

<?php 
$indent = str_repeat("\t", 5); 
$dirname = "resids/{$_SESSION['logname']}/{$_POST['xyz']}";
$filename= "{$_POST['xyz']}";
if ((!is_dir($dirname))||(!is_file($filename))) 
{ 
  echo $indent . '<option>Invalid directory specified.</option>'; 
}
no errors....xept it says Invalid directory specified....i know why....the if statement is doing either or...it feels as though im on the right track but....

ok...i set the filename for ['xyz'] which is cool....only one thing...it needs to look for folders as well as files

hint please :P

Posted: Thu Oct 12, 2006 12:09 pm
by feyd
I'd supply additional pathing information either in the form as a separate element, or in the options themselves.

Basically, you need to store $dirname (stripped of the common part "resids/$_SESSION['logname']/")

Posted: Thu Oct 12, 2006 12:16 pm
by Obadiah
you mean like if the user selects a folder not only does it concatenate whatever the value of xyz is but it echos a new select with xyz's content in that select?

i tried that...i couldent figure how to echo a new one on submission without calling errors....ill try again now though :)