PHP_Idiot Posted April 10, 2009 Share Posted April 10, 2009 I have a FOR Loop that sequentially names form fields based on a user input in the PlayerNo field. The below code snippets should give you the idea. for ( $counter = 1; $counter <= $PlayerNo; $counter += 1) <input name=\"Position$counter\" <input name=\"Points$counter\" <input name=\"MembershipNo$counter\" This works perfectly, but the problem is when I try to get the $_POST data for each field How can I get all the data and have the following also sequentially numbered? if ($_POST['MembershipNo1'] == ' '){ $MembershipNo = '<span style="color:red;">MembershipNo omitted.</span>'; }else{ $MembershipNo = $_POST['MembershipNo1']; } I have tried a few things but I just keep getting errors! Anyone got any ideas? Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/ Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 and why are u comparing against a space? if (empty(trim($_POST['MembershipNo1']))){ but these are dynamic fields, why not have them in a loop instead as yer form does? for ( $counter = 1; isset($_POST[($mc='Membership'.$counter)]); $counter += 1) if (empty(trim($_POST[$mc]))){ we want to check for an empty response, trim just removes any whitespaces Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806599 Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 <?php for ($i = 1; $i <= $PlayerNo; $i++) { if ($_POST["MembershipNo$i"] == ' ') { ${"MembershipNo$i"} = "<span style=\"color:red;\">MembershipNo$i omitted.</span>"; } else { ${"MembershipNo$i"} = $_POST["MembershipNo$i"]; } } ?> Will create variables $MembershipNo1, $MembershipNo2 etc. If that's not what you want, just tell me. Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806602 Share on other sites More sharing options...
PHP_Idiot Posted April 10, 2009 Author Share Posted April 10, 2009 Thank you both for your suggestions. Laffin, When I tried your code I got a fata error on the If line, I probably used it in the wrong way! Sorry i couldn't get it to work! thebadlad your code runs without error, and you correct that I am trying to create a variable for each field name as I need to load the contents of these variables into my database. However, I couldn't echo out the contents of any of the variables! Bascially on the form that is created (MembershipNo1, MembershipNo2 etc) I want to create variables of the same name that stores the contents of those fields. I suspect the code you have given does this, but I don't seem to be able to 'see' the contents to check it is storing what I need. I confess to being a total noob this is my first ever dip into php so please be patient with me, I appreciate the help enourmously! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806615 Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 Be sure that $PlayerNo is defined in the processing script too. Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806657 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 Just a side note, the easier way of doing this is using an array for $PlayerNo this can be achieved by using brackets in your form fields for ( $counter = 1; $counter <= $PlayerNo; $counter += 1) echo " <input name=\"Position[$counter]\"> <input name=\"Points[$counter]\"> <input name=\"MembershipNo[$counter]\"> "; } Now since the variables are in an array you can use foreach or count Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806687 Share on other sites More sharing options...
PHP_Idiot Posted April 10, 2009 Author Share Posted April 10, 2009 Woohoo.........you guys rock!! Thebadbad - correctly assumed I hadn't defined $PlayerNo (it should actually have been $Players) laffin now got your original code running and the below: for ($i = 1; $i <= $Players; $i++) { if ($_POST["MembershipNo$i"] == '') { ${"MembershipNo$i"} = "<span style=\"color:red;\">MembershipNo$i omitted.</span>"; } else { ${"MembershipNo$i"} = $_POST["MembershipNo$i"]; } echo $i; echo "<br>"; echo ${"MembershipNo$i"}; echo "<br>"; } gives me the output I was looking for However... I really like the idea of using the array you posted your last comment, but couldn't get it working! I presume this would do away with having to repeat the above code for each field I wanted to place into variables! Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806706 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 Correct take a simple form like for($i=1;$i<10;$i++) { $val=$i*10; echo "<input type=\"hidden\" name=\"box[{$i}]" value=\"{$val}\""; } echo "<input type=\"hidden\" name=\"box[{$i}]" value=\"\""; very simple form, for readability. okay in processing u can now treat $box as an array <?php $boxes=$_POST['box']); $count=count($boxes); echo "Found {$boxes} boxes<BR />"; $removed=0; foreach($boxes as $idx=>$box) { if(empty($box)) { unset($boxes[$idx]); $removed++; } } echo "Removed {$removed} empty boxes<br />"; Just to give u an idea Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806724 Share on other sites More sharing options...
PHP_Idiot Posted April 10, 2009 Author Share Posted April 10, 2009 One day I'll make it a mission to understand all this!! But for now I'm happy just to have it working!! I really appreciate you taking the time to explain it, and I kinda get it, but I need to play with it some more to fully understand. Can I ask you one more question though please... if ($_POST["Venue$i"] == '') { ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>"; } else { ${"Venue$i"} = ereg_replace("[A-Za-z)(.-]","",$_POST["Venue$i"]; } This code doesn't work, if I remove the -- ereg_replace("[A-Za-z)(.-]","", -- bit it does. Where am I going wrong? The reason I'm doing this is that the VenueName stores the ID as well, this is the format: Venue Name(12) I want to strip off the text and brackets and spaces -'s and anything else and just be left with the number. This is the VenueID in myDB Table and is the primary key. Any ideas? (Promise this is the last question..probably...for now at least ) Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806764 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 I never used eregi functions, I stick with pregmatch functions preg_match('@.*?\((\d*)\)@',$_POST["Venue{$i}"],$match); ${"Venue$i"}=$match[1]; Shud work Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806778 Share on other sites More sharing options...
PHP_Idiot Posted April 10, 2009 Author Share Posted April 10, 2009 Oh ok, never heard of preg_match before! I tried to use it like this: if ($_POST["Venue$i"] == '') { ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>"; } else { ${"Venue$i"} = preg_match('@.*?\((\d*)\)@',$_POST["Venue{$i}"],$match); ${"Venue$i"}=$match[1]; } but got no results. Then went and read up on it on the manual website, but I can't see how this will work in place of ereg-replace! Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806810 Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 Try this then: if ($_POST["Venue$i"] == '') { ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>"; } else { preg_match('@.*?\((\d*)\)@', $_POST["Venue$i"], $match); ${"Venue$i"}=$match[1]; } Or you can use preg_replace() there's very similar to ereg_replace(): if ($_POST["Venue$i"] == '') { ${"Venue$i"} = "<span style=\"color:red;\">Venue$i omitted.</span>"; } else { ${"Venue$i"} = preg_replace('~[^\d]~', '', $_POST["Venue$i"]); } The simple RegEx [^\d] matches any character NOT a digit, and replaces those matches with nothing (second parameter ''). So if the venue names don't contain any digits, that should leave you with the ID. Basically the preg_ functions are recommended over the ereg_ functions because they are faster. Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806889 Share on other sites More sharing options...
PHP_Idiot Posted April 10, 2009 Author Share Posted April 10, 2009 Thanks so much for explaining in more detail, I tried it with the preg_replace (just because it makes more sense to my simple brain!) But when I ran the following at the end to check the output, it still had the Venue name as well as the ID! foreach($_POST as $field => $value) { echo "$field, $value<br>"; } Unfortunately my server seems to have gone to bed, as I have no connection at the moment so can't test anything else out, I think I'll give up for tonight and try again in the morning. Thanks so much for all the help I really appreciate it, I'll drop a note in tomorrow if I manage to get it going Quote Link to comment https://forums.phpfreaks.com/topic/153514-how-can-i-get-_post-to-squentially-number-from-counter/#findComment-806905 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.