guyfromfl Posted September 18, 2008 Share Posted September 18, 2008 I am trying to make an order form that starts with 1 row of fields. example: txtPartNum, qty...whatever else you need... so the first row would say txtPartNum1, qty1 then if you add another add a row txtPartNum2, qty2... and so on until you have added enough rows for all the parts in the order. my problem is once you've POSTed the data, the number at the end (qty2) isn't dynamic anymore. i want to make a for loop that loops through all the rows the order needs and pass this data on to insert into a SQL database? something like this: <?php $count = $_POST['count'] // the number of rows for this order // loop through the total number of items for ($item=0; $item<$count; $item++) { $sql = "INSERT INTO orders (customer, partNum, qty) VALUES ($_POST['custNum<need a variable here>', $_POST['txtPartNum<var>', $_POST['qty<var>'])"; ?> btw: thats a basic idea i know there was no validation, formatting and syntax perfection... Im trying to get around limiting the orders to say 10 items, or creating a new order for each item. I hope that made sense to somebody. Thanks mark Quote Link to comment https://forums.phpfreaks.com/topic/124747-solved-dynamic-fieldsvariables-with-phphtml/ Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 You can post into array form one input uses var[] for its name another uses var[] for its name yet another uses var[] for its name the first input is accessible by var[0], the second var[1], etc. combine that with a simple foreach loop such as foreach($_POST['var'] as $input) {} or, alternatively for($i = 0; $i < count($_POST['var']); ++$i) { echo $_POST['var'][$i]; // or whatever } Quote Link to comment https://forums.phpfreaks.com/topic/124747-solved-dynamic-fieldsvariables-with-phphtml/#findComment-644350 Share on other sites More sharing options...
guyfromfl Posted September 18, 2008 Author Share Posted September 18, 2008 thanks yea i was "making up" code in the mean time and figured out you can do this: <?php for ($item=1; $item<$count; $item++) { $part[$item] = $_POST['partNum' . $item]; // and so on... } Quote Link to comment https://forums.phpfreaks.com/topic/124747-solved-dynamic-fieldsvariables-with-phphtml/#findComment-644359 Share on other sites More sharing options...
CroNiX Posted September 18, 2008 Share Posted September 18, 2008 arrays start with 0 though, so it should be $item=0 in your for loop or you will miss the first entry. Quote Link to comment https://forums.phpfreaks.com/topic/124747-solved-dynamic-fieldsvariables-with-phphtml/#findComment-645147 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 Use a foreach() loop. Quote Link to comment https://forums.phpfreaks.com/topic/124747-solved-dynamic-fieldsvariables-with-phphtml/#findComment-645151 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.