JasonGreen Posted October 7, 2009 Share Posted October 7, 2009 Seriously, I'm not that bright. My whole life random stragners have taken pity on me. You only have to meet me once to see I am incapable of even dressing myself. I pass off as hip, but the truth is I just forgot to wear a belt. Here is my endless loop. Probably best not to run, it keeps freezing my PC $breed = rtrim(strtok($pet_spa, " \n")); $appointment = rtrim(strtok(" \n")); $day = rtrim(strtok(" \n")); $time = rtrim(strtok(" \n")); $charge = rtrim(strtok(" \n")); printf ("%9s\n", "Breed App. Day Time Charge"); while($breed){ echo $breed, " "; echo $appointment, " "; echo $day, " "; echo $time, " "; echo "\$" . number_format ($charge,2); } I have to use While. I know there are other ways of doing this. But can't I use While? Quote Link to comment Share on other sites More sharing options...
eugeniu Posted October 7, 2009 Share Posted October 7, 2009 The code above states that while $breed is set (exists) do (echo) all of this stuff. Nowhere in the code is $breed unset so yeah, it'll go on forever. What are you trying to do with this script? Quote Link to comment Share on other sites More sharing options...
pernest Posted October 7, 2009 Share Posted October 7, 2009 The variable that you are testing in your while loop is not being changed in that loop, so if you get into that loop and $breed is not false, then it never will be false, and your while loop will continue for ever. I think you need to be calling strtok within your while loop, maybe somthing like this at the end of the loop $breed = rtrim(strtok(" \n")); then your loop will exit when all the lines or spaces in $petspa have been used up. Also you might like to change while($breed) to while($breed !== false) as $breed could come back as zero, not as null, depending on what is in $petspa Seriously, I'm not that bright. My whole life random stragners have taken pity on me. You only have to meet me once to see I am incapable of even dressing myself. I pass off as hip, but the truth is I just forgot to wear a belt. Here is my endless loop. Probably best not to run, it keeps freezing my PC $breed = rtrim(strtok($pet_spa, " \n")); $appointment = rtrim(strtok(" \n")); $day = rtrim(strtok(" \n")); $time = rtrim(strtok(" \n")); $charge = rtrim(strtok(" \n")); printf ("%9s\n", "Breed App. Day Time Charge"); while($breed){ echo $breed, " "; echo $appointment, " "; echo $day, " "; echo $time, " "; echo "\$" . number_format ($charge,2); } I have to use While. I know there are other ways of doing this. But can't I use While? Quote Link to comment Share on other sites More sharing options...
btherl Posted October 7, 2009 Share Posted October 7, 2009 Based on the code you've posted, you don't need to use while. Just do the echos without while. If you have multiple lines to loop through, the while needs to go outside, somewhere in the code that you haven't posted. Quote Link to comment Share on other sites More sharing options...
JasonGreen Posted October 7, 2009 Author Share Posted October 7, 2009 I'm using include 'petspainfo.php' and then spitting out the info, have to use While and still need assigned variables for more data manipulation. while(echo) only gives an error including $breed = rtrim(strtok(" ")); repeats the same info (I took out \n because it was placing each item on a separate line Quote Link to comment Share on other sites More sharing options...
JasonGreen Posted October 8, 2009 Author Share Posted October 8, 2009 Can't I just tell the loop to stop when it's retrieved every line of data from the include file? break does nothing Quote Link to comment Share on other sites More sharing options...
btherl Posted October 8, 2009 Share Posted October 8, 2009 I need to see the rest of your code to help you. Quote Link to comment Share on other sites More sharing options...
JasonGreen Posted October 8, 2009 Author Share Posted October 8, 2009 there is not much else to it, but here it is ($petspa comes from the information within petspainfo.php) <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title>Spoiled Pets</title> <h5>Pet Spa Super Spa</h5> </head> <body> <pre> <?php // File Name: PetSpaRus // Author: me // Purpose:etc include 'petspainfo.php'; $breed = rtrim(strtok($pet_spa, " \n")); $appointment = rtrim(strtok(" \n")); $day = rtrim(strtok(" \n")); $time = rtrim(strtok(" \n")); $charge = rtrim(strtok(" \n")); printf ("%9s\n", "Breed App. Day Time Charge"); while($breed){ echo $breed, " "; echo $appointment, " "; echo $day, " "; echo $time, " "; echo "\$" . number_format ($charge,2); } ?> </pre> </body> </html> Quote Link to comment Share on other sites More sharing options...
JasonGreen Posted October 8, 2009 Author Share Posted October 8, 2009 I don't think I can use if or any other variation of it since I'm getting the data from another file (include 'info.php') . Is ther anything I can do to just stop the loop? I need it structured this way to keep the variables for other manipulations Quote Link to comment Share on other sites More sharing options...
btherl Posted October 8, 2009 Share Posted October 8, 2009 Thanks for posting the code. Does $pet_spa contain all of your data? And you want to keep calling strtok() to get more items until there's no more data? In that case you could do this. nclude 'petspainfo.php'; $breed = rtrim(strtok($pet_spa, " \n")); $appointment = rtrim(strtok(" \n")); $day = rtrim(strtok(" \n")); $time = rtrim(strtok(" \n")); $charge = rtrim(strtok(" \n")); printf ("%9s\n", "Breed App. Day Time Charge"); while($breed){ echo $breed, " "; echo $appointment, " "; echo $day, " "; echo $time, " "; echo "\$" . number_format ($charge,2); $breed = rtrim(strtok(" \n")); $appointment = rtrim(strtok(" \n")); $day = rtrim(strtok(" \n")); $time = rtrim(strtok(" \n")); $charge = rtrim(strtok(" \n")); } You might have some issues here - it depends on what the contents of $pet_spa actually is. I'm making a few assumptions about that. You can use "break" from inside the while loop and it ought to work. It won't work outside the while loop though, as there's nothing to break out of. Quote Link to comment Share on other sites More sharing options...
JasonGreen Posted October 8, 2009 Author Share Posted October 8, 2009 btherl, you are a savior, a true guru and a gentleman, Thank you so much. I will drink to you and yours tonight. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.