Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Web Design & Development
Tag Cloud
access audio black screen blue screen boot bsod connection crash desktop driver drivers dvd email error excel firefox hard drive hardware hijackthis internet keyboard laptop malware monitor motherboard network networking outlook problem processor recovery router safe mode screen slow sound spyware tdlwsp.dll trojan upgrade vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
php: can't pass variable from while loop through browser

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

 
Thread Tools
twmprys's Avatar
Computer Specs
Member with 69 posts.
 
Join Date: Jun 2009
Experience: Beginner
03-Nov-2009, 07:46 AM #1
php: can't pass variable from while loop through browser
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:

PHP Code:
$dir opendir('pages/');

while(
$read readdir$dir )) {
if (
substr($read,0,1)=='2') {
echo 
'&bull; <a href="reviewpages.php?id=$read" target="_self">';
echo 
$read '</a><br />';
}
}
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's Avatar
Member with 57 posts.
 
Join Date: Feb 2006
Location: New Jersey
Experience: Intermediate
03-Nov-2009, 09:54 AM #2
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:

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

Code:
while (false !== ($read = readdir($dir))) {
It should work.
twmprys's Avatar
Computer Specs
Member with 69 posts.
 
Join Date: Jun 2009
Experience: Beginner
03-Nov-2009, 10:46 AM #3
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?....

Last edited by twmprys : 03-Nov-2009 11:13 AM.
SNewman's Avatar
Member with 57 posts.
 
Join Date: Feb 2006
Location: New Jersey
Experience: Intermediate
03-Nov-2009, 12:12 PM #4
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.

Code:
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:

Code:
listDir("pages/");
twmprys's Avatar
Computer Specs
Member with 69 posts.
 
Join Date: Jun 2009
Experience: Beginner
03-Nov-2009, 07:10 PM #5
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's Avatar
MMJ MMJ is offline
Distinguished Member with 3,519 posts.
 
Join Date: Oct 2006
03-Nov-2009, 08:53 PM #6
Quote:
Originally Posted by twmprys View Post
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's Avatar
Computer Specs
Member with 69 posts.
 
Join Date: Jun 2009
Experience: Beginner
04-Nov-2009, 04:34 AM #7
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's Avatar
MMJ MMJ is offline
Distinguished Member with 3,519 posts.
 
Join Date: Oct 2006
04-Nov-2009, 01:06 PM #8
Quote:
Originally Posted by twmprys View Post
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's Avatar
Computer Specs
Member with 69 posts.
 
Join Date: Jun 2009
Experience: Beginner
05-Nov-2009, 04:07 AM #9
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.

Last edited by twmprys : 05-Nov-2009 04:36 AM.
Reply Bookmark and Share

Tags
php, variable, while loop

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 03:05 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.