jcbarr Posted August 1, 2006 Share Posted August 1, 2006 I am creating a page that will allow a user to input a lot of data into a table. The number of values that will be passed for each entry is 4. For example each entry will have the following fields;POSNAMELEAGUESTATThe user will be able to add as many entries as they see fit. So this could result in a bunch of variables. So my question is this. When the form is submitted what would be the best way to gather all of the variables so that I can insert them into the database? I'm a little stuck as to what to do. Should I name them pos1, pos2... name1...name2...? Or is there a better way to do this?I don't want to have a hundred lines like this in my code;[code]$pos1 = $_POST['pos1'];[/code]I hope that I have explained this well enough. If you need more info I will be more than glad to give it to you.Anyone have any ideas on how to do this? I have passed variables from a form before, but that was when I knew exactly what variables were being passed and exactly how many there would be, so I'm not a complete newb...Thanks in advance to anyone willing to take the time to help. Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 1, 2006 Share Posted August 1, 2006 To program in a proper php fashion you will have to $_POST['every_varable'] unless you have set your php.ini to globels on what will cause php problams.you are coding the correct way always post each varable. Quote Link to comment Share on other sites More sharing options...
jcbarr Posted August 1, 2006 Author Share Posted August 1, 2006 The problem with that is that I don't know how many there will be coming from the form.I was wondering if there was some way to grab everything being sent with the POST command and dump them in to arrays or something like that and then do a loop where it dumps every value of the array in to a table field? I'm not sure if that can be done, and I'm not too good with arrays, but I was thinking it might be able to be done.The problem with the form is that the user will select how many entries they make. Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 try this[code]<input type="text" name="pos[]"><input type="text" name="name[]"><input type="text" name="league[]"><input type="text" name="stat[]"><input type="text" name="pos[]"><input type="text" name="name[]"><input type="text" name="league[]"><input type="text" name="stat[]">[/code]and you'll get postdata like this when the form is submitted[code]<?php$_POST['pos'][0]$_POST['name'][0]...$_POST['pos'][1]$_POST['name'][1]...?>[/code]So then you can use a loop[code]foreach($_POST['pos'] as $k=>v){ echo $v;}[/code]hope this helps. Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 1, 2006 Share Posted August 1, 2006 dont quit understand sorry as for how many entrys the user makes has no diffrence to having the posts within the page.you might want to lookup sessions ok.good luck. Quote Link to comment Share on other sites More sharing options...
jcbarr Posted August 1, 2006 Author Share Posted August 1, 2006 [quote author=ryanlwh link=topic=102628.msg407599#msg407599 date=1154469098][code]<?php$_POST['pos'][0]$_POST['name'][0]...$_POST['pos'][1]$_POST['name'][1]...?>[/code]So then you can use a loop[code]foreach($_POST['pos'] as $k=>v){ echo $v;}[/code]hope this helps.[/quote]Okay I set the form names like you did, but do I still have to grab all of them with the POST lines? Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 1, 2006 Share Posted August 1, 2006 yes yes yes you got to post to get the varables to post ok.Think off it like a letter, if you dont put a stamp on it you dont get the letter, but if the letter got a stamp you get that letter.hope this helps. Quote Link to comment Share on other sites More sharing options...
jcbarr Posted August 1, 2006 Author Share Posted August 1, 2006 Yeah so I guess I'm not going to be able to do what I wanted. I thought there might be some way to do a catch all. Like some fucntion that would catch all the pos[] variables being passed in one shot rather than having to put multiple lines to catch each one. Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 didn't you see this??[code]<?phpforeach($_POST['pos'] as $k=>v){ echo $v;}?>[/code]if you want to catch "everything" easily[code]<?phpforeach($_POST as $k=>$v){ if($k=='pos') { foreach($_POST[$k] as $v) { //do stuff with each pos } }}?>[/code]or in your original way[code]foreach($_POST as $k=>$v){ if (strpos($k,'pos')!== false) //do stuff for pos if (strpos($k,'name') !== false) //do stuff for name}[/code] Quote Link to comment Share on other sites More sharing options...
jcbarr Posted August 1, 2006 Author Share Posted August 1, 2006 Yeah I did and when I add that to my code I get this error;Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /public_html/dynamic/all_star/allstar.php on line 10This is the only code so far in the script;[code]<?php$create = $_GET['CREATE'];$edit = $_GET['EDIT'];foreach($_POST['pos'] as $k=>v){ echo $v;}?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 1, 2006 Share Posted August 1, 2006 ryanlwh if you use the foreach to get the array and post the information would you have to set the name according to the foreach to insert the information to the database.cheers.so you got to get the correct name for the insert but also use the $_POST statement i dont see any other way.<input type="text" name="pos[]"><input type="text" name="name[]"><input type="text" name="league[]"><input type="text" name="stat[]"><?phpforeach($_POST['pos'] as $k=>$v) {$name=$v[1];$league=$v[2];stat=$v[3];}if($_POST['submit']){insert code}?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 1, 2006 Share Posted August 1, 2006 [code]<?php$create = $_GET['CREATE'];$edit = $_GET['EDIT'];foreach($_POST['pos'] as $k=>$v){ echo $v;}?>[/code] Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 [quote author=redarrow link=topic=102628.msg407640#msg407640 date=1154471620]ryanlwh if you use the foreach to get the array and post the information would you have to set the name according to the foreach to insert the information to the database.cheers.so you got to get the correct name for the insert but also use the $_POST statement i dont see any other way.<input type="text" name="pos[]"><input type="text" name="name[]"><input type="text" name="league[]"><input type="text" name="stat[]"><?phpforeach($_POST['pos'] as $k=>$v) {$name=$v[1];$league=$v[2];stat=$v[3];}if($_POST['submit']){insert code}?>[/quote]:) this will give you error :) $v is not an array Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 1, 2006 Share Posted August 1, 2006 <?phpforeach($_POST['pos'] as $v) {$name=$v[1];$league=$v[2];stat=$v[3];}if($_POST['submit']){insert code}?>thats better cheers. Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 have you tried it yourself? Quote Link to comment Share on other sites More sharing options...
jcbarr Posted August 1, 2006 Author Share Posted August 1, 2006 Wonderful!! Thanks for all the help!! 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.