millsy007 Posted March 5, 2009 Share Posted March 5, 2009 I have a system that is booking people in, sometimes the user will want to make a multiple booking, so I have added a drop down list with the number of passengers to add My program then reads this in as a variable $passengers = $_GET['passengers']; I then want to loop through for each of the passengers: while ($passengers != 0) { // At the start //Put the rest of my program withing this $passengers= ($passengers-1) //At the end } Is this a sensible way to do things, I wanted to check before I tried as I know loops can go horribly wrong! Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/ Share on other sites More sharing options...
Mchl Posted March 5, 2009 Share Posted March 5, 2009 If I understand you correctly: ( if user chooses for example 5 passengers, you want to display 5 consecutive pages for passengers' details) this is not how you would do that Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777475 Share on other sites More sharing options...
cooldude832 Posted March 5, 2009 Share Posted March 5, 2009 This screams some sort of OOP setup or at the very least a function to do this. I think if you are looking to enter 5 duplicant rows into MySQL then you should do it in a single query and only need to run the processor checking once Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777488 Share on other sites More sharing options...
millsy007 Posted March 5, 2009 Author Share Posted March 5, 2009 The program currently works one booking at a time, the user selects the date and route, enters the passenger name and clicks 'book' however sometimes someone will want to make a booking for say 5 people so instead of clicking 'book' 5 times I want them to enter the number from a list, and then the program basically repeat the insert code 5 times. Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777603 Share on other sites More sharing options...
Mchl Posted March 5, 2009 Share Posted March 5, 2009 So you want to display a single form for 5 clients. Yes, you can do that using while loop Although for might be better. Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777624 Share on other sites More sharing options...
millsy007 Posted March 5, 2009 Author Share Posted March 5, 2009 Hi yes, its one form, but the input would be done 5 times. So in my page a makebooking.php is called in which the booking queries are ran, this is what I was thinking of enclosing within my while loop as per the original post, what would the advantage of a for loop be? Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777636 Share on other sites More sharing options...
Mchl Posted March 5, 2009 Share Posted March 5, 2009 It would look more intuitively IMHO. for($i=1;$i<=$passengers;$i++) { //your code } Other than that it works just like while() Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777638 Share on other sites More sharing options...
millsy007 Posted March 5, 2009 Author Share Posted March 5, 2009 So if I passed in the variable $passengers and this had a value of 5 would that code go through 1 at a time until all 5 had been inserted? Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777668 Share on other sites More sharing options...
Mchl Posted March 5, 2009 Share Posted March 5, 2009 Basically yes. Read the manual for for for detailed information on how it works. Yes... I purposefully put triple 'for' in above sentence. Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-777696 Share on other sites More sharing options...
millsy007 Posted March 6, 2009 Author Share Posted March 6, 2009 I get: Cannot redeclare insert() (previously declared in /home/makebooking.php:92) What I want to do is literally run all my code for the amount of passengers, but it looks like this may cause problems? Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-778142 Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi Can you post up the source code? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148112-setting-program-within-a-while-loop/#findComment-778149 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.