using a combo box to display dir

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Last edited by volka on Thu Oct 12, 2006 9:13 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The second parameter of a for loop is the "while" check. Normally, a comparison is done there. You have an assignment.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a second opendir() isn' t needed, but the setting of $dirname should be done where it was originally.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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']/")
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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 :)
Post Reply