bacarudaguy Posted August 18, 2009 Share Posted August 18, 2009 I've been running around in circles trying to figure out arrays and checkboxes. I've searched on here and can't seem to find the answer to this one, and I have a book in front of me that doesn't quite cover it either... Basic breakdown - editing a Tournament Director (TD) (this is for a poker website) that is already in the database and assigning them to tournament venues on certain nights (venue names and days at the venues are pulled from the database). My problem is using the checkboxes as a TD can be assigned to more then one venue during the week... I've got some error handling going on for _POST and the inputs all setup, but just can't pass it to the DB properly... // Check for TD venue assignments. if (empty($_POST['td_venue[]'])) { $errors[] = 'You forgot to enter the TD\'s venue assignments.'; } else { $td_venue = implode (', ', escape_data($_POST['td_venue[]'])); //$td_venue is correct var to be passed to db insert query } echo "<p>TD Venue Assignment: \n <table border=\"0\">"; //Pull venue name and day to build checkboxes $venueq = "SELECT venue_name AS vname, venue_day AS vday FROM venues ORDER BY venue_name ASC"; $venuer = @mysql_query ($venueq); while ($row = mysql_fetch_array($venuer, MYSQL_ASSOC)) { echo '<tr> <td> <input type="checkbox" name="td_venue[]" value="' . $row['vname'] . '" /> ' . $row['vname'] . ' on ' . $row['vday'] . ' </td> </tr>'; } echo '</table></p>'; Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/ Share on other sites More sharing options...
HoTDaWg Posted August 18, 2009 Share Posted August 18, 2009 im not quite sure what you're asking but i hope this helps <?php //to insert the data into the database. foreach ($_POST['td_venue'] => $key as $value){ //escape the string here, however you see fit. $query = "INSERT INTO tablename VALUES ('$value') "; $result = mysql_query($query); if ($result) { echo $value . ' was added.'; } } ?> change this accordingly i hope it gives you an idea as to what the code should look like. Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/#findComment-900663 Share on other sites More sharing options...
bacarudaguy Posted August 18, 2009 Author Share Posted August 18, 2009 Hotdawg, thanks for the reply... here's a visual of what the backend needs to accomplish. Say you add a Tournament Director, and they're assigned to Graffiti Bar on Monday and PJ's on Wednesday. The values of the checkbox's are the venue name and the entire submission needs to be entered into the DB. I'm guessing the venue names would need to be combined into an array, then, in my edit TD page and view TD page, I can pull the array, blow it up and run IF statements to do the rest of what I need to do. I'm just stuck on getting it into the DB successfully... [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/#findComment-900672 Share on other sites More sharing options...
HoTDaWg Posted August 18, 2009 Share Posted August 18, 2009 okay so $_POST is an ARRAY by naming each of your textboxes as td_venue[] you are creating an ARRAY within the $_POST array if that makes sense, i know it can be confusing. so by referring to $_POST['td_venue'] you are referring to all of the returned checkboxes from the form. so by using the foreach loop you can do certain things to all of the CHECKED checkboxes of the user. Foreach allows you to distinguish what to do with each of the checkboxes. i hope this helps HoTDaWg Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/#findComment-900682 Share on other sites More sharing options...
Daniel0 Posted August 18, 2009 Share Posted August 18, 2009 Perhaps check out this tutorial: http://www.phpfreaks.com/tutorial/working-with-checkboxes-and-a-database Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/#findComment-900726 Share on other sites More sharing options...
bacarudaguy Posted August 19, 2009 Author Share Posted August 19, 2009 Thanks much! Was hoping to find something like this... wasn't aware you had tutorials! I'll search in those next time as well! Hopefully this can solve my issues... Perhaps check out this tutorial: http://www.phpfreaks.com/tutorial/working-with-checkboxes-and-a-database Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/#findComment-902156 Share on other sites More sharing options...
bacarudaguy Posted August 20, 2009 Author Share Posted August 20, 2009 Been plugging away at trying to get this figured out and quickly realized it's a little more complicated then the tutorial posted... Here's the basic run down... I have 3 different pages (add, edit, view) for the tournament directors that all involve using checkboxes. I've figured out how to get the data into the DB in a string, and out of the DB breaking the string into <li>'s... I thought I had figured out how to compare bits of the string from the DB and check the checkbox if it matched, but I guess I'm missing something... Here's the bits of code from the 3 different pages involving the checkboxes.... I'm sure it's something easy I'm looking over... but I'm just not sure. All self taught and tested on PHP so far, so I don't think I'm doing terrible! Adding a TD - this part is working as it should, it posts the entire array with the separator into the DB //Pulled from main IF as part of error checking // Convert TD Venue array for DB $tdven = $_POST['td_venue']; $tdv = implode (';', $tdven); //Pull venue name and day to build checkboxes $venueq = "SELECT venue_name AS vname, venue_day AS vday FROM venues ORDER BY venue_name ASC"; $venuer = @mysql_query ($venueq); while ($row = mysql_fetch_array($venuer, MYSQL_ASSOC)) { echo '<tr> <td> <input type="checkbox" name="td_venue[]" value="' . $row['vname'] . '-' . $row['vday'] . '" /> ' . $row['vname'] . '-' . $row['vday'] . ' </td> </tr>'; } Editing a TD - this is pulling the TD info, but I'm pretty sure I have a problem in my comparison with the string from the DB and trying to "check" the checkboxes that it matches... if (mysql_num_rows($result) == 1) { // if query found the TD // Get the user's information. while (list($tdname, $tdsname, $tdphone, $tdemail, $tdvenue) = mysql_fetch_row ($result)) { // Create the form. echo '<h2>Edit TD</h2> <form action="edit_td.php" method="post"> <p>TD Name: <input type="text" name="tdn" size="20" maxlength="30" value="' . $tdname . '" /></p> <p>TD Screen Name: <input type="text" name="tdsn" size="20" maxlength="30" value="' . $tdsname . '" /></p> <p>TD Phone: <input type="text" name="tdp" size="15" maxlength="15" value="' . $tdphone . '" /></p> <p>TD Email: <input type="text" name="tde" size="25" maxlength="30" value="' . $tdemail . '" /></p>'; echo "<p>TD Venue Assignments: \n"; echo '<table border="0">'; //Pull venue name and day to build checkboxes while (list($vname, $vday) = mysql_fetch_row($venuer)) { if ($tdvenue == $vname . '-' . $vday) { echo '<tr> <td> <input type="checkbox" name="td_venue[]" value="' . $tdvenue . '" checked="checked" /> ' . $tdvenue . ' </td> </tr>'; } else { echo '<tr> <td> <input type="checkbox" name="td_venue[]" value="' . $vname . '-' . $vday . '" /> ' . $vname . '-' . $vday . ' </td> </tr>'; } //end of if } //end of while echo '</table></p>'; } //end of while echo '<p><input type="submit" name="submit" value="Submit" /> <input type="button" name="cancel" value="Cancel" onClick="location.href=(\'view_td.php\');" /></p> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="id" value="' . $id . '" /> </form>'; } else { // Not a valid user ID. echo '<h1>Page Error</h1> <p>This page has been accessed in error.</p><p><br /><br /></p>'; } //end of main if View the TD's - this part is working great. Pulls and explodes the entire string in the db that gets submitted! // Fetch and print all the records and build edit and delete links. while (list($td_id, $name, $sname, $phone, $email, $tdven) = mysql_fetch_row($result)) { echo '<tr> <td>[ <a href="edit_td.php?id=' . $td_id . '">E</a> ] [ <a href="delete_td.php?id=' . $td_id . '">D</a> ]</td><td align="center">' . $td_id . '</td><td align="left">' . $name . '</td><td>' . $sname . '</td><td>' . $phone . '</td><td>' . $email . '</td><td>'; if (strlen($tdven) > 0) { //Convert each string to list format echo '<ul>'; $tdven = explode (';', $tdven); foreach ($tdven AS $tdvenue) { echo '<li>' . $tdvenue . '</li>'; } echo '</ul> </td> </tr>'; } else { echo ' </td> </tr>'; } //end of if } //end of while Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/#findComment-902588 Share on other sites More sharing options...
bacarudaguy Posted August 23, 2009 Author Share Posted August 23, 2009 Bump... any takers? Quote Link to comment https://forums.phpfreaks.com/topic/170778-checkboxes-arrays-and-databases-oh-my/#findComment-904590 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.