Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving table data to MySQL

Status
Not open for further replies.

rogerte

Programmer
Nov 9, 2001
164
0
0
GB
I have just about finished my first ever PHP project (a Sports Club Management System converted from a Del[phi app) and I am left with one thing to complete. The attendance register. More specifically saving the data back to the MySQL table. Everything else works fine

Basically the attendance table has the following structure
Member_ID
Session_ID
Attend

The latter field is a string in the format "1,0,1,1,1,0,0,1,1,1,0,0,1" where each 0 or 1 signifies attendance at each of the 13 sessions available in the term.

Each session will have no more than 25 attendees.

The data is assembled into a table, within a form, with each table row consisting of 15 <td>s. The first is an indicator (used to show expanded member data), the second the members name, and the remainder the 13 attendance indicators as checkboxes (1= Attended, 0 = not attended)

The code used to build the table is:
PHP:
                            <?php
                            while($g_fetch = $a_query->fetch_array()) {
                                $checked = array();
                                $memid = $g_fetch['mem_id'];
                                $name = $g_fetch['firstname'].' '.$g_fetch['lastname'];
                                $attendences = explode(",",$g_fetch['attend']);
                                for ($x = 0; $x <= 12; $x++) {if ($attendences[$x]!="0") {$checked[$x] = 'checked = "checked"';} else { $checked[$x] = '';}}
                                
                                /* Now display table data rows */
                                echo '<tbody><tr class="parent">';  
                                echo '<td><i class="fa fa-chevron-down"></i></td>';
                                echo '<td name="'.$memid.'">'.$name.'</td>';
                                for ($y = 0; $y <= 12; $y++) { 
                                  if ($y <> $NoWeeks){ $dis = ' disabled'; } else {$dis = '';}
                                  echo '<td align="center"><input type="checkbox"  value = "1" '.$checked[$y].'" "'.$dis.'></td>';
                                  }	
                                echo '</tr>';
                                echo '<tr class="cchild">';
                                echo '<td colspan="5">';   
                                echo "Parent Name: <b>".$g_fetch['parent_name']."</b><br />";
                                echo "Parent Tel: <b>".$g_fetch['parent_tel']."</b><br />";
                                echo "Emergency 1: <b>".$g_fetch['emergency1']."</b><br />";
                                echo "Emergency 2: <b>".$g_fetch['emergency2']."</b><br />";
                                echo '</td>';
                                echo '<td colspan="5" >';   
                                echo '</td>';   
                                echo '<td colspan="5" >';
                                echo '</td>';   
                                echo '</tr></tbody>'; 
                                unset($checked);
                                unset($attendences);
                            }	 
                            ?>
                       </table>

This works ok, but am having trouble saving the data back to the table after clicking on the form Save button (set up as a POST)
I have read that I need to set up the checkboxes as an array and assign a different value to each.
I assume this means for each checkbox setting it to something like name="attend[".$y."]"
Now I am stuck - I assume that for each row I can use the second <td>s name to find the record to update, and that I can assemble the attend[] elements to build the attendance string.
However I cannot seem to get the looping through the rows to get the various elements to work.
Any help gratefully received.
Many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top