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 acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard netgear network printer problem ram registry repair router slow software sound toshiba trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless xbox
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
php: can't pass variable from while loop through browser

Reply  
Thread Tools
twmprys's Avatar
Computer Specs
Member with 106 posts.
 
Join Date: Jun 2009
Experience: Beginner
03-Nov-2009, 08: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 82 posts.
 
Join Date: Feb 2006
Location: New Jersey
Experience: Intermediate
03-Nov-2009, 10: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 106 posts.
 
Join Date: Jun 2009
Experience: Beginner
03-Nov-2009, 11: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 at 12:13 PM..
SNewman's Avatar
Member with 82 posts.
 
Join Date: Feb 2006
Location: New Jersey
Experience: Intermediate
03-Nov-2009, 01: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 106 posts.
 
Join Date: Jun 2009
Experience: Beginner
03-Nov-2009, 08: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
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
03-Nov-2009, 09: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 106 posts.
 
Join Date: Jun 2009
Experience: Beginner
04-Nov-2009, 05: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
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
04-Nov-2009, 02: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 106 posts.
 
Join Date: Jun 2009
Experience: Beginner
05-Nov-2009, 05: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 at 05:36 AM..
Reply

Tags
php, variable, while loop

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:43 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.