We help IT Professionals succeed at work.

dynamic form

I need to create a form that gets its columns from a database (php/mysql).  
It will be in the following format:
		<form method="post" name="newActivities" action="addActivities.php">
	        
	        <div class="form-group">
	                <div class="row">
	                    <div class="col-md-4">
	                      <label for="activityDate">Date of Work</label>
	                      <input type="text" class="form-control" name="activityDate">
	                    </div>
	                </div>
            </div>
		    <table id="table" class = "udsGrid">
		          <thead>
	          			<tr>
	          				<th></th>
	          				<th colspan="3">600 POS Handling</th>
	          				<th colspan="3">601 Marketing Handling</th>
	          				<th colspan="3">602 E-Commerce Handling</th>
	          			</tr>
	              		<tr>
							<th>Staff</th>

							<th>Units</th>
							<th>Reg Hrs</th>
							<th>OT Hrs</th>

							<th>Units</th>
							<th>Reg Hrs</th>
							<th>OT Hrs</th>

							<th>Units</th>
							<th>Reg Hrs</th>
							<th>OT Hrs</th>														
	              		</tr>
		          </thead>
		          <tr>
						<td>Jones, Tom</td>
						<td><input type="text" name="Units1_Activity600"></td>
						<td><input type="text" name="RegHrs1_Activity600"></td>
						<td><input type="text" name="OTHrs1_Activity600"></td>
						<td><input type="text" name="Units1_Activity601"></td>
						<td><input type="text" name="RegHrs1_Activity601"></td>
						<td><input type="text" name="OTHrs1_Activity601"></td>
						<td><input type="text" name="Units1_Activity602"></td>
						<td><input type="text" name="RegHrs1_Activity602"></td>
						<td><input type="text" name="OTHrs1_Activity602"></td>
		          </tr>
		          <tr>
						<td>Doe, John</td>
						<td><input type="text" name="Units2_Activity600"></td>
						<td><input type="text" name="RegHrs2_Activity600"></td>
						<td><input type="text" name="OTHrs2_Activity600"></td>
						<td><input type="text" name="Units2_Activity601"></td>
						<td><input type="text" name="RegHrs2_Activity601"></td>
						<td><input type="text" name="OTHrs2_Activity601"></td>
						<td><input type="text" name="Units2_Activity602"></td>
						<td><input type="text" name="RegHrs2_Activity602"></td>
						<td><input type="text" name="OTHrs2_Activity602"></td>
		          </tr>		                                  
		    </table>
	    </form>

Open in new window


I'm guessing that as I loop through the recordset to get all the columns (600 POS, 601 Marketing, etc) and looping through a staff table to get a row for each staff member. I can use jquery to add the needed <th> and <tr> elements.  But here is where my difficulty is.  Each activity gets added as a seperate record:
StaffActivity.StaffID, StaffActivity.ActivityDate, StaffActivity.ActivityID (this is the 600, 601, etc), StaffActivity.Units, StaffActivity, RegHours, and StaffActivity.OTHours.

What is the best way to save each record to individual records.  I was thinking the names of the input controls could be used to store the ActivityID and StaffID????  Should the input controls be arrays? Should I add some type of identifier in the staff <td> cell?

Hope this all makes sense.
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
No points for this, please, because it's not an answer.  But it may provide some useful "getting started" information.  If you're new to PHP and server-side web application development, this article may help you get started.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The general design pattern for what you're doing may be found in the concept of "SQL table maintenance."
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12335-PHP-and-MySQLi-Table-Maintenance.html

Author

Commented:
Thanks Ray, you have helped me plenty of times and these links are not new to me and I refer back to them from time to time.

However I didn't see anything in these articles that helped with this task at hand as part of my question is asking for suggestions on the best way to set up the html table/form given the way the user wants the data displayed and how it is to be stored.  What would be the easiest way to build the tables to allow for easy looping to write back to my database table.

Author

Commented:
Ok.  Let me post what I came up with to build the table.

		          <tbody>
          		<?php 
          			foreach ($staffArray as $key => $value) {
          				echo "<tr>";
          				echo "<td class='headcol'>".$value."<input type='hidden' name='uuk[]' value='".$key."'></td>";
						$rstActivities = mysql_query($sql);          					
  						while($row = mysql_fetch_assoc($rstActivities))
      					{
							echo "<td><input class='qty' activityID='".$row['ActivityID']."' type='text' name='Units[]' size='4'></td>";
							echo "<td><input class='regHrs' activityID='".$row['ActivityID']."' type='text' name='RegHours[]' size='4'></td>";
							echo "<td><input class = 'otHrs' type='text' activityID='".$row['ActivityID']."' name='OTHours[]' size='4'></td>";						
						}      					

					echo "</tr>";
					}
				?>
	            </tbody>

Open in new window


So each row of the table will have one staffID and multiple activity records. The activities table structure is:  StaffActivity.StaffID (this will be the uuk[] value), StaffActivity.ActivityDate, StaffActivity.ActivityID, StaffActivity.Units, StaffActivity, RegHours, and StaffActivity.OTHours

I figured I could add an element in each <td> called ActivityID to help, but not sure how any of this will work.

So again, the question is, what does php code look like to append the many records?

Author

Commented:
I found a post that may be leading me the correct way, change the names of my fields:

          		<?php 
          			foreach ($staffArray as $key => $value) {
          				echo "<tr>";
          				echo "<td class='headcol'>".$value."</td>";
						mysql_data_seek($rstActivities, 0) ;      					
  						while($row = mysql_fetch_assoc($rstActivities))
      					{
							echo "<td>";
								echo "<input type='hidden' name='entry[uuk]' value='".$key."'>";
								echo "<input type='hidden' name='entry[ActivityID]' value='".$row['ActivityID']."'>";
								echo "<input class='qty' type='text' name='entry[Units]' size='4'>";
							echo "</td>";
							echo "<td><input class='regHrs' type='text' name='entry[RegHours]' size='4'></td>";
							echo "<td><input class = 'otHrs' type='text' name='entry[OTHours]' size='4'></td>";						
						}      					

					echo "</tr>";
					}
				?>

Open in new window

Distinguished Expert 2019

Commented:
a picture is worth a thousand words..you have to structure your mysql query until you get the dataset returned the  way you want it... then in server side code you run the query and populate the dataset  then you can then format the table
I was able to get an answer to the partial code in my last post.

I opened another question and got a solution:
  http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28632534.html

Author

Commented:
I was able to get my question answered by google and posting another question