hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : else in while loop
Reply

Forum Jump

else in while loop

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-15-2003, 12:30 PM
code-2k3 code-2k3 is offline
Junior Guru Wannabe
 
Join Date: Aug 2003
Posts: 57
Unhappy

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.



Sponsored Links
  #2  
Old 12-15-2003, 12:48 PM
blackbelt080 blackbelt080 is offline
Web Hosting Master
 
Join Date: Jul 2003
Location: East TN
Posts: 568
Re: else in while loop

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.

  #3  
Old 12-15-2003, 01:00 PM
code-2k3 code-2k3 is offline
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.

Sponsored Links
  #4  
Old 12-15-2003, 01:14 PM
Tux-e-do Tux-e-do is offline
Aspiring Evangelist
 
Join Date: Jul 2002
Posts: 433
Re: else in while loop

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

  #5  
Old 12-15-2003, 01:44 PM
Burhan Burhan is offline
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,099
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

  #6  
Old 12-15-2003, 01:58 PM
code-2k3 code-2k3 is offline
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.

  #7  
Old 12-15-2003, 02:18 PM
blackbelt080 blackbelt080 is offline
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.

  #8  
Old 12-15-2003, 02:39 PM
code-2k3 code-2k3 is offline
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.

  #9  
Old 12-15-2003, 02:57 PM
blackbelt080 blackbelt080 is offline
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.

  #10  
Old 12-15-2003, 02:59 PM
blackbelt080 blackbelt080 is offline
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.

  #11  
Old 12-15-2003, 03:07 PM
code-2k3 code-2k3 is offline
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  
Old 12-15-2003, 03:13 PM
blackbelt080 blackbelt080 is offline
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.

  #13  
Old 12-15-2003, 03:55 PM
Jeanco Jeanco is offline
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.

  #14  
Old 12-15-2003, 04:33 PM
code-2k3 code-2k3 is offline
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

  #15  
Old 12-16-2003, 01:13 AM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266
No problem, glad I could help

Reply

Related posts from TheWhir.com
Title Type Date Posted
cPanel Addresses User Concerns of Transfer and Backup Restore System Security Web Hosting News 2013-05-24 10:13:44
Cloud Platform Heroku Offers Free Ruby on Rails Training for Developers Web Hosting News 2013-04-12 14:41:30
DataBank Holdings Opens Data Center in North Dallas Suburb Web Hosting News 2013-03-21 11:18:01
TELUS Communications Opens Quebec Data Center to Offer Hosting, Cloud Services Web Hosting News 2012-12-07 15:02:11
Lightower Fiber Networks Adds 100 Route Miles to New Jersey Network Web Hosting News 2012-05-07 14:34:37


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?