
12-15-2003, 12:30 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Aug 2003
Posts: 57
|
|
else in while loop
This is driving me nuts, can anybody please explain why this wont work,
PHP Code:
<?
$string = $_GET['string'];
$path = "/home/me/public_html/dir/";
$fp = @opendir($path);
if ($string && $string != "") {
while (($file = readdir($fp)) !== FALSE) {
if (preg_match('/'.$string.'/', $file)) {
$filename = str_replace('.html','',$file);
echo "<a href=\"$file\">$filename</a><br>";
}
else { echo "no matches for $string"; exit; }
}
closedir($fp);
}
else {
echo "no string";
}
?>
I've tried all different combinations of this, the above one returns "no matches for string" even when there is a match, if i remove that else condition it'll return the matches correctly, but then obviously when there is no match it wont tell you.
I know this isn't an ideal way to search a directory, but i'm just trying to work out why it doesn't work as above. 
|

12-15-2003, 12:48 PM
|
|
Web Hosting Master
|
|
Join Date: Jul 2003
Location: East TN
Posts: 568
|
|
Quote:
Originally posted by code-2k3
This is driving me nuts, can anybody please explain why this wont work,
PHP Code:
<?
$string = $_GET['string'];
$path = "/home/me/public_html/dir/";
$fp = @opendir($path);
if ($string && $string != "") {
while (($file = readdir($fp)) !== FALSE) {
if (preg_match('/'.$string.'/', $file)) {
$filename = str_replace('.html','',$file);
echo "<a href=\"$file\">$filename</a><br>";
}
else { echo "no matches for $string"; exit; }
}
closedir($fp);
}
else {
echo "no string";
}
?>
I've tried all different combinations of this, the above one returns "no matches for string" even when there is a match, if i remove that else condition it'll return the matches correctly, but then obviously when there is no match it wont tell you.
I know this isn't an ideal way to search a directory, but i'm just trying to work out why it doesn't work as above.
|
Code:
else { echo "no matches for $string"; exit; }
Check your use of double semi-colons and that exit stmt....
__________________
Take care,
Lori
-- Please use tags for code. PM me for information if not sure how to do so. | Refer to PHP Manual and MySQL Manual before posting.
|

12-15-2003, 01:00 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Aug 2003
Posts: 57
|
|
I think that just looks weird the way i typed it out, but
PHP Code:
else {
echo "no matches for $string";
exit;
}
makes no difference, the reason for the exit is otherwise it'll return "no matches for $string" over and over for however many files there are that don't match the string.
I know it would be better to do the loop then echo the results after, but i've tried several ways and didn't have any luck.
|

12-15-2003, 01:14 PM
|
|
Aspiring Evangelist
|
|
Join Date: Jul 2002
Posts: 433
|
|
Quote:
Originally posted by code-2k3
This is driving me nuts, can anybody please explain why this wont work,
PHP Code:
<?
$string = $_GET['string'];
$path = "/home/me/public_html/dir/";
$fp = @opendir($path);
if ($string && $string != "") {
while (($file = readdir($fp)) !== FALSE) {
if (preg_match('/'.$string.'/', $file)) {
$filename = str_replace('.html','',$file);
echo "<a href=\"$file\">$filename</a><br>";
}
else { echo "no matches for $string"; exit; }
}
closedir($fp);
}
else {
echo "no string";
}
?>
I've tried all different combinations of this, the above one returns "no matches for string" even when there is a match, if i remove that else condition it'll return the matches correctly, but then obviously when there is no match it wont tell you.
I know this isn't an ideal way to search a directory, but i'm just trying to work out why it doesn't work as above.
|
as blackbelt080 states, remove the exit statement.
You are exiting the application not the while statement.
The way this is written, on the first no match, the script will halt execution
|

12-15-2003, 01:44 PM
|
|
Community Guide
|
|
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
|
|
You need "continue" instead of "exit"
__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise
|

12-15-2003, 01:58 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Aug 2003
Posts: 57
|
|
bah, thanks, that's what i was thinking, that something was causing it to halt after the first no match, but because i was using exit to solve one problem, i didn't think it was causing the other.
So back to square one, how to get it to return "no match for $string" just once, if there's no matches. as it is removing the exit would cause it to return something like
filename
no match for $string
filename
filename
no match for $string
no match for $string
etc etc, through all the files.
|

12-15-2003, 02:18 PM
|
|
Web Hosting Master
|
|
Join Date: Jul 2003
Location: East TN
Posts: 568
|
|
Quote:
Originally posted by code-2k3
bah, thanks, that's what i was thinking, that something was causing it to halt after the first no match, but because i was using exit to solve one problem, i didn't think it was causing the other.
So back to square one, how to get it to return "no match for $string" just once, if there's no matches. as it is removing the exit would cause it to return something like
filename
no match for $string
filename
filename
no match for $string
no match for $string
etc etc, through all the files.
|
See fyrestrtr's post... 
__________________
Take care,
Lori
-- Please use tags for code. PM me for information if not sure how to do so. | Refer to PHP Manual and MySQL Manual before posting.
|

12-15-2003, 02:39 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Aug 2003
Posts: 57
|
|
Sorry didn't mention, that doesn't solve the problem, it continues looping through all files and prints the result for each of them.
|

12-15-2003, 02:57 PM
|
|
Web Hosting Master
|
|
Join Date: Jul 2003
Location: East TN
Posts: 568
|
|
Quote:
Originally posted by code-2k3
Sorry didn't mention, that doesn't solve the problem, it continues looping through all files and prints the result for each of them.
|
Did you take the 'exit;' out?
__________________
Take care,
Lori
-- Please use tags for code. PM me for information if not sure how to do so. | Refer to PHP Manual and MySQL Manual before posting.
|

12-15-2003, 02:59 PM
|
|
Web Hosting Master
|
|
Join Date: Jul 2003
Location: East TN
Posts: 568
|
|
Hmm. Try putting a 'return;' statement before the ?>....
__________________
Take care,
Lori
-- Please use tags for code. PM me for information if not sure how to do so. | Refer to PHP Manual and MySQL Manual before posting.
|

12-15-2003, 03:07 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Aug 2003
Posts: 57
|
|
same result lol, yes i removed the exit, and the return is the same with or without continue;
It'll run all the way through the directory and give a result for each file, rather than ...
If there's matches - echo them, else if not - echo no matches
Would it be better to mabye put the files in an array then loop once through the array to return "no match" or pull out the matches? does that even make sense? i confuse myself sometimes 
|

12-15-2003, 03:13 PM
|
|
Web Hosting Master
|
|
Join Date: Jul 2003
Location: East TN
Posts: 568
|
|
Quote:
Originally posted by code-2k3
same result lol, yes i removed the exit, and the return is the same with or without continue;
It'll run all the way through the directory and give a result for each file, rather than ...
If there's matches - echo them, else if not - echo no matches
Would it be better to mabye put the files in an array then loop once through the array to return "no match" or pull out the matches? does that even make sense? i confuse myself sometimes
|
Check out this useful link: here
__________________
Take care,
Lori
-- Please use tags for code. PM me for information if not sure how to do so. | Refer to PHP Manual and MySQL Manual before posting.
|

12-15-2003, 03:55 PM
|
|
Web Hosting Guru
|
|
Join Date: Aug 2003
Posts: 266
|
|
Create a flag, and once a match is found then print the statement and exit the loop. If the flag is never initiated then no statement was found so:
Set $flag == 0
If the string doesn't match, then move onto the next check
Else (string does match) $flag == 1
If the flag == 1 at the end of the loop then a match was found.
|

12-15-2003, 04:33 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Aug 2003
Posts: 57
|
|
arrgh, so simple, never thought of it, thankyou
The following works perfectly
PHP Code:
<?
$string = $_GET['string'];
$path = "/home/me/public_html/dir/";
$fp = @opendir($path);
if ($string && $string != "") {
while (($file = readdir($fp)) !== FALSE) {
if (preg_match('/'.$string.'/', $file)) {
$match = "1";
$filename = str_replace('.html','',$file);
echo "<a href=\"$file\">$filename</a><br>";
}
}
closedir($fp);
}
else {
echo "no string";
$match = "1";
}
if (!$match) {
echo "no results for $string";
}
?>
not ideal still i know, but i can tidy it up now it's stopped doing crazy things 
|

12-16-2003, 01:13 AM
|
|
Web Hosting Guru
|
|
Join Date: Aug 2003
Posts: 266
|
|
No problem, glad I could help 
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|