# php: can't pass variable from while loop through browser



## twmprys (Jun 4, 2009)

I'm trying to select the name of a page from a php while loop (listing the contents of a directory) and pass it through the browser. Here's the code:


```
$dir = opendir('pages/');

while($read = readdir( $dir )) {
if (substr($read,0,1)=='2') {
echo '• [URL=reviewpages.php?id=$read]';
echo $read . '[/URL]
';
}
}
closedir($dir);
```
All the above code does is literally send the characters '$read' through the browser - it doesn't seem to pick up the individual name of the page. Has some kind person out there go any ideas?


----------



## SNewman (Feb 26, 2006)

If you look at Example 1 in the PHP documentation for readdir [here], you'll see that the way you are looping through the directory is incorrect. Try replacing:


```
while($read = readdir( $dir )) {
```
with:


```
while (false !== ($read = readdir($dir))) {
```
It should work.


----------



## twmprys (Jun 4, 2009)

Thanks for your reply. But the script still seems to be passing just the word $read to the browser, not the filename which should be inside it.

It must be something to do with the while loop function. Even though it is possible to list the names of files in a directory by using a variable, the variable doesn't appear to be able to carry individual filenames. I've tried echoing the variable on a new page, and it's empty. Anyone got any other ideas?....


----------



## SNewman (Feb 26, 2006)

I've created the following function which should accomplish what I believe your goal is - listing all files in the directory beginning with the number 2.


```
function listDir($d)
{
	$handle = opendir($d);
	$numFiles = 0;
	
	while (false !== ($file = readdir($handle)))
	{
		if($file != "." && $file != "..")
		{
			if(substr($file,0,1) == '2')
			{
				echo "&bull; <a href=\"reviewpages.php?id=" . $file . "\" target=\"_self\">" . $file . "</a><br />";
				$numFiles++;
			}
		}
	}
	
	if($numFiles == 0)
		echo "No files found.";
	
	closedir($handle);
}
```
Simply call the function and specify the target directory like such:


```
listDir("pages/");
```


----------



## twmprys (Jun 4, 2009)

Thanks so much for the code. But the existing code lists the files no problem. What I can't do is pass the name of each item to the browser. The variable ($read) appears to be empty.


----------



## MMJ (Oct 15, 2006)

twmprys said:


> I've tried echoing the variable on a new page, and it's empty. Anyone got any other ideas?....


That won't work. Variables - unless attached to a session/cookie - are not static and change each time the code is ran.


----------



## twmprys (Jun 4, 2009)

So how do I do it? Maybe I haven't explained my problem clearly enough - to restate the situation, I've got a list of files sitting in a directory (generated by a while loop) and I want to be able to pass any of the names I select to another page - doesn't have to go through the browser, I suppose.


----------



## MMJ (Oct 15, 2006)

twmprys said:


> So how do I do it? Maybe I haven't explained my problem clearly enough - to restate the situation, I've got a list of files sitting in a directory (generated by a while loop) and I want to be able to pass any of the names I select to another page - doesn't have to go through the browser, I suppose.


Get the file names from that page?

Store the names in a database or session/cookie?


----------



## twmprys (Jun 4, 2009)

The way I eventually got round the problem was to ECHO a FORM around each iteration of the loop which then POSTS the file name to the next page. Still, I'm not too pleased that the links have to be in the form of SUBMIT buttons - would prefer them to be conventional text.


----------

