ThisisForReal Posted July 19, 2010 Share Posted July 19, 2010 Hello my super smart helpers! To one of you, this is super easy, but it's definitely a pain in my butt because it's very new to me. I am creating an html form in PHP that generates a series of radio buttons to report about who showed up (list of names with 'yes' and 'no') for a dynamic number of participants to a dynamic number of events. No trouble creating the form - works great. An abbreviated (but all necessary info shown) snippet of the code is below. I'm just having trouble taking the $_post data and being able to extract the keys and values into a method that I can comfortably use them. I will use them by running a loop and calling a mysql stored procedure for each participant and their attendance (yes or no). Here's how the form is generated (code has been trimmed but all necessary info is there): <?php //Get the list of all unresolved events $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query = "CALL db.spGetUnconfirmedEvents ('$admin_code')"; $data = mysqli_query($dbc,$query); ?> <div id="mainContent"> <form method="post" action=""> <?php while ($event_list = mysqli_fetch_array($data)) { $eventID = $event_list['eventID']; echo '<tr><td>Participant</td><td colspan="2">Attended?</td></tr>'; //Fill with participants from that event $query_participants = "CALL db.spViewParticipants ('$event')"; $data_participants = mysqli_query($dbc,$query_participants); while ($participants = mysqli_fetch_array($data_participants)) { //every ID is unique for all events (no 2 events will have any common ID's between them) $participantID = $participants['eventParticipantID']; $part_first = $participants['firstName']; $part_last = $participants['lastName']; echo '<td>' . $part_first . ' ' . $part_last . '</td><td><input type="radio" name="' . $participantID . '" value="1" checked>Yes</td>'; echo '<td><input type="radio" name="' . $participantID . '" value="0">No</td></tr>'; } } </table> ?> <input type="image" border="0" value="Resolve" name="submit" src="images/button_alert_okay.png"/></form> As I mentioned before, that part works great. When the administrator hits submit, each unique participant ID has a 1 or a 0 associated with it. It's just using the post data that is giving me trouble: <?php //If the submit button is pressed if ((isset($_POST['submit_x'])) || (isset($_POST['submit']))) { $retrieve = $_POST; If I run a print_r on the $retrieve array, I get something like this: [22] => 1 [37] => 0 ...(output shows every participantID and the respective 1 or 0 for yes/no)...... [submit_x] => 62 [submit_y] => 19 [submit] => Resolve I have a sproc ready to call to mysql that handles the business logic of resolving the attendance lists of these previous events since the admin last logged into the sight. I've tried a half dozen ways to loop through the array and submit each set of keys and values to my stored procedure: /* -- Name : spConfirmParticipant -- Desc : Confirms participant to have attended the event or not -- Inputs : (participantID INT, attendFlag INT 1/0 = attend, not attend ) -- Outputs : (nothing) */ Because my participantIDs are unique and autoincrement, they will eventually become very large. So I need an efficient way to grab them. I've played around with using regular expressions with the array_keys command to try to build a new array but I know there's got to be a really easy method. I'm sorry my question is a little more open ended than usual but - any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/208131-rearranging-keys-and-values-from-a-post-data-of-a-dynamic-html-form/ Share on other sites More sharing options...
Pikachu2000 Posted July 19, 2010 Share Posted July 19, 2010 I would suggest making the radio buttons an array as well. Then you'll have an array within in the POST array that you can loop through on its own. <td><input type="radio" name="button_array[' . $participantID . ']" value="1" checked>Yes</td>'; echo '<td><input type="radio" name="button_array[' . $participantID . ']" value="0">No</td></tr> print_r($_POST) would return: Array( name => Joe pass=> pass button_array => Array( participantID_1 => yes participantID_2 => no ) etc. Quote Link to comment https://forums.phpfreaks.com/topic/208131-rearranging-keys-and-values-from-a-post-data-of-a-dynamic-html-form/#findComment-1087956 Share on other sites More sharing options...
ThisisForReal Posted July 20, 2010 Author Share Posted July 20, 2010 An elegant solution. Thank you so much. For anyone that visits this thread, here's how I handled your info: //If the submit button is pressed if ((isset($_POST['submit_x'])) || (isset($_POST['submit']))) { $retrieve = array(); $retrieve = $_POST['button_array']; foreach($retrieve as $key => $value) { $query_resolve = "CALL db.spConfirmParticipant ('$key','$value')"; } Quote Link to comment https://forums.phpfreaks.com/topic/208131-rearranging-keys-and-values-from-a-post-data-of-a-dynamic-html-form/#findComment-1088507 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.