bulrush Posted April 30, 2010 Share Posted April 30, 2010 I am somewhat new to PHP. I am going through a PHP tutorial book "Head First: PHP and MySql". So I know the PHP basics. But none of their examples cover what I need to do in a real world application. I would like to make a form with many columns per row, with multiple rows. Ideally each row would be an entry in an array, so the user can enter 10 rows at once, and save them all at once. My form with one row looks like this: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table> <tr><td>Description <td>Number <td>Price <tr><td><input type="text" name="txtDesc" value="<?php if (!empty($desc)) echo $desc; ?>" size="45"> <td><input type="text" name="txtModel" value="<?php if (!empty($model)) echo $model; ?>" size="20"> <td><input type="text" name="txtPrice" value="<?php if (!empty($price)) echo $price; ?>" size="6"> </table> <p> <p>Commands: <input type="submit" name="submit" value="save"> </form> Now, how can I turn this into 10 rows, like an array? Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/ Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 So you want something similar to a Data Grid? Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051013 Share on other sites More sharing options...
bulrush Posted April 30, 2010 Author Share Posted April 30, 2010 I think so. In Visual Basic I think it's called a Data Grid, or simply a Grid control. I'm not sure what the PHP term is. But yes, I want to display n rows on the New Record Entry screen, where each row would become a new record in a table. Later, I will make a screen to edit existing records. This screen is for mass entry of new records. Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051023 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 Data Grid isn't bind to any one specific language. But there's definitely a way to do that. One way would be to give them separate names like: txtDec1, txtDec2, etc. You can initially set up a table with 10 rows, all editable fields. Then have a button to add more if need be. Use JavaScript to save how many rows are added + the original 10. Then upon submit, have JavaScript fill a hidden field with that count and have PHP do a loop through them all. Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051029 Share on other sites More sharing options...
litebearer Posted April 30, 2010 Share Posted April 30, 2010 as an alternative. The data could be entered into a spread sheet and then imported into the database Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051032 Share on other sites More sharing options...
litebearer Posted April 30, 2010 Share Posted April 30, 2010 also might look at PhpMyEdit http://www.phpmyedit.org/ Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051036 Share on other sites More sharing options...
bulrush Posted April 30, 2010 Author Share Posted April 30, 2010 Ok. So if I name the form fields txtDesc1, txtDesc2, txtDesc3, txtModel1, txtModel2, txtModel3, etc for rows 1, 2, and 3. How would I construct the field names for my INSERT statement inside a loop? for ($i=1; $i<=10; $i++) { $query = "INSERT INTO mytable (desc, model) VALUES (??, ??)"; //... do query } //for i Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051056 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 o.O huh? You get the values and put them into the query. I'm not understanding your question at all. You know that the fields are txtDesc#. The # part will be specified by $i. So you just fetch the values of those fields and put them into the query. Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051059 Share on other sites More sharing options...
bulrush Posted April 30, 2010 Author Share Posted April 30, 2010 But txtDesc1 is not a variable at this point, is it? It's the name of a form field (text box). Isn't that different than a PHP variable? A form is in HTML, a variable is in PHP. I have not saved the value of the form field in a PHP variable yet. Perhaps this will work: for ($i=1; $i<=10; $i++) { $descvar='txtDesc'.$i; $modelvar='txtModel'.$i; $query = "INSERT INTO mytable (desc, model) VALUES ('$$descvar', '$$modelvar')"; //... do query } //for i Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051063 Share on other sites More sharing options...
bulrush Posted April 30, 2010 Author Share Posted April 30, 2010 Maybe I should clarify. My php page is called "entry.php". When the user goes to this form the first time, it shows a form with 10 rows. If they click the Save button (named "submit") then the form reloads itself and saves all 10 rows to the table. So, when the form is submitted, and the form fields are named txtDesc1, txtDesc2, txtModel1, txtModel2, the values are saved in $_POST['txtDesc1'], $_POST['txtDesc2'], etc. They are not stored in variables $txtDesc1 from my understanding, unless I manually copy the values from $_POST['txtDesc1'] to $txtDesc1. Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051069 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 I didn't say they have to be variables. If you have the loop, you can easily go through $_POST['txtDesc'+$i] to get the value so I have no idea why this is so complicated for you to understand. Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051075 Share on other sites More sharing options...
bulrush Posted April 30, 2010 Author Share Posted April 30, 2010 It's tricky because I'm new to PHP. I just started going through this book last week, and I'm not sure how to construct a PHP variable inside the double quotes of my query statement. Do you think this would work? for ($i=1; $i<=10; $i++) { $descvar=$_POST['txtDesc'.$i]; $modelvar=$_POST['txtModel'.$i]; $query = "INSERT INTO mytable (desc, model) VALUES ('$$descvar', '$$modelvar')"; //... do query } //for i Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051083 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 You have an extra $ in your variables. Also, you can insert multiple entries like this: INSERT INTO mytable (desc, model) VALUES ('desc1', 'model1'), ('desc2', 'model2'); Add a comma and another set so you run 1 SQL instead of 10. Link to comment https://forums.phpfreaks.com/topic/200275-form-rows-as-an-array-of-text-boxes-for-data-entry/#findComment-1051087 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.