George Botley Posted August 26, 2009 Share Posted August 26, 2009 Hello Guys, I am having a problem... Heres the HTML code: <input type="text" name="scorer" /><br /> <input type="text" name="scorer" /><br /> <input type="text" name="scorer" /><br /> <input type="text" name="scorer" /><br /> Heres the PHP code: <?php $scorer = $_POST["scorer"]; while(!empty($scorer)) { echo "$scorer"; } ?> Now I made this script to take values from fields with the same name so a javascript code can add more fields in the event of more than 4 being needed. So why, if the last field entry equals Bob for example then why does the script keep echoing Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob I need help with this desperately, as the site is due to go live soon. Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/ Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 because, since you assigned the value of scorer to the variable $scorer, thus making it not empty, your while loop will always run true. you created an endles loop. Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907045 Share on other sites More sharing options...
George Botley Posted August 26, 2009 Author Share Posted August 26, 2009 Then why doesn't this work either? while(!empty($_POST["scorer"])) { echo $_POST["scorer"]; } I am good enough at PHP to understand many errors, but this is the first time I have tried to do anything like this.... So how would I fix this issue? Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907053 Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 because that is essentially the same exact thing as the first code... your basically doing hey set this variable to this value while this variable has a value do echo that value since you never do anything but echo the value, the while will always be true and thus it becomes an infinite loop Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907056 Share on other sites More sharing options...
George Botley Posted August 26, 2009 Author Share Posted August 26, 2009 because that is essentially the same exact thing as the first code... your basically doing hey set this variable to this value while this variable has a value do echo that value since you never do anything but echo the value, the while will always be true and thus it becomes an infinite loop So what would you recommend? Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907059 Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 not doing that! but what exactly are you trying to accomplish with this loop. Right now it doesn't seem to be accomplishing anything useful for anyone. What should your loop be doing? Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907062 Share on other sites More sharing options...
George Botley Posted August 26, 2009 Author Share Posted August 26, 2009 I want it to take all information from each field and insert each item individually into a database called scorers. The whole script will be a match report if you like, for a football club. So the user can add additional scorers if needed and no matter how many form fields, the script will use the data regardless. Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907069 Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 well, the way your doing it won't really work. I'm not entirely sure, but having a bunch of fields with the same name will result in the last field being the value from $_POST[whatever] (or maybe the first). However, if you change it from <input type="text" name="scorer" /><br /> to <input type="text" name="scorer[]" /><br /> then the scorer post var will be its own array (note the [] at the end of the name) then you simply iterate through the array $scorers = $_POST['scorer']; foreach($scorers as $scorer){ //database insert code here } Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907074 Share on other sites More sharing options...
George Botley Posted August 26, 2009 Author Share Posted August 26, 2009 Thank you, just what I was looking fo Quote Link to comment https://forums.phpfreaks.com/topic/172016-solved-whileemptyscorer-echo-scorer-need-a-solution/#findComment-907081 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.