methodman Posted September 8, 2009 Share Posted September 8, 2009 Hey everybody! I've been struggling with this issue for a bit now and it's beginning to get more elusive the longer i work on it!! lol. SO, i am trying to post "checked" checkboxes to my database and retrieve this dynamic list to generate the form. NOTE that the checkboxes are dynamically generated by the DB query. Table names are: affiliations - contains a list of different affiliations to choose from aff_list - contains the selected / checked affiliations for each ID <td><label for="affiliations">Affiliations</label></td> <td> <?php $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database'); $affiliationsQuery = "SELECT * FROM affiliations"; $affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); $affiliation = array(); $affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'"; $data2 = mysqli_query($dbc, $affQuery); $aff_list = array(); while($row2 = mysqli_fetch_array($data2)){ array_push($aff_list, $row2); } echo '<fieldset><table><tr>'; while($row10 = mysqli_fetch_array($affiliationsData)){ array_push($affiliation, $row10); } foreach($affiliation as $aff){ if($row2['affiliation_id'] == 2){ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>'; } else{ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' . $aff['affiliation'] . '</td>'; } } echo $row2['affiliation_id'] . '<br />'; print_r($aff_list); // ******************************************************************************************************* echo '</tr></table></fieldset>'; mysqli_close($dbc); ?> OPERATIONALS: i am able to dynamically generate the checkboxes for the form with no problems I am able to update the database with the selected information with no problems I can retrieve the data contained in the "aff_list" table and use array_push() to get this data into an array ISSUES: i cannot retrieve the values from this array i cannot use this information to compare the values to determine if this checkbox has been submitted.. IDEAS: i am thinking i can use a conditional like: if($aff['affiliation_id'] == $aff_list['affiliation_id']){ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>'; } else{ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' . $aff['affiliation'] . '</td>'; } ----------- i have tried many variations of this - but i'm not making any progress and i really can't waste anymore time on this issue. ANY help or even a point in the right direction will be of great assistance. Thanks in advance guys!! Quote Link to comment Share on other sites More sharing options...
methodman Posted September 8, 2009 Author Share Posted September 8, 2009 I just had an idea what about using javascript (ajax) to popluate the checkboxes with the $aff_list array data??? SO: pull the aff_list table data with PHP then use javascript to read the incoming data and assign "checked" value to each box?? sounds like a good plan to me i'm going to attempt that Again, any help / feeback is appreciated! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 8, 2009 Share Posted September 8, 2009 what if javascript is off??? just before your check for the value of affiliate echo out its value foreach($affiliation as $aff){ echo $row2['affiliation_id']; if($row2['affiliation_id'] == 2){ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>'; } else{ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' . $aff['affiliation'] . '</td>'; } } obviously stick it inside some tags so it appears in the expected place... Quote Link to comment Share on other sites More sharing options...
methodman Posted September 8, 2009 Author Share Posted September 8, 2009 what if javascript is off??? just before your check for the value of affiliate echo out its value foreach($affiliation as $aff){ echo $row2['affiliation_id']; if($row2['affiliation_id'] == 2){ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>'; } else{ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' . $aff['affiliation'] . '</td>'; } } obviously stick it inside some tags so it appears in the expected place... That echo statement returns no value (nothing prints). However, the print_r($aff_list) function returns this string : Array ( [0] => Array ( [0] => 2 [affiliation_id] => 2 ) [1] => Array ( [0] => 1 [affiliation_id] => 1 ) ) this page is actually an admin page, so the user will always have javascript enabled.. I would prefer to fix this problem the right way though.. using the PHP that builds it instead of the Javascript "fix" :-\ Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 8, 2009 Share Posted September 8, 2009 OK here is a clue for you... when the affiliate id is anything other than 2 you will NOT get the check box checked... so your if statement check the id of the current affiliate and see if its present in the list of affiliates associated with the current breed. Quote Link to comment Share on other sites More sharing options...
methodman Posted September 9, 2009 Author Share Posted September 9, 2009 OK here is a clue for you... when the affiliate id is anything other than 2 you will NOT get the check box checked... so your if statement check the id of the current affiliate and see if its present in the list of affiliates associated with the current breed. Good call, actually that was a typo. i was in the midst of changing random things just to see one of the checkboxes become selected, lol. the code i am trying to use now is: <td><label for="affiliations">Affiliations</label></td> <td> <?php // these affiliations need to populate the attr_list table NOT the breed table. $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database'); $affiliationsQuery = "SELECT * FROM affiliations"; $affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); $affiliation = array(); $affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'"; $data2 = mysqli_query($dbc, $affQuery); $aff_list = array(); while($row2 = mysqli_fetch_array($data2)){ array_push($aff_list, $row2); } echo '<fieldset><table><tr>'; while($row10 = mysqli_fetch_array($affiliationsData)){ array_push($affiliation, $row10); } foreach($affiliation as $aff){ echo '<p>' . $aff_list['affiliation_id'] . '</p>'; if($aff['affiliation_id'] == $aff_list['affiliation_id']){ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>'; } else{ echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' . $aff['affiliation'] . '</td>'; } } print_r($aff_list); // ******************************************************************************************************* echo '</tr></table></fieldset>'; mysqli_close($dbc); ?> I'm still not having any luck but i have been working on other aspects of this project and giving this a rest for a min - while hoping for a solution on here. I appreciate your efforts! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 9, 2009 Share Posted September 9, 2009 You have an array of affiliate IDs and an array of affiliate IDs associated with a breed. Loop through the affiliate IDs and for each one check if that ID is present in the array of IDs associated with a breed. <?php foreach($affiliation as $aff) { $checked = array_search($aff['affiliation_id'], $aff_list) ? ' checked="checked"' : NULL; echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]"' . $checked . ' />' . $aff['affiliation'] . '</td>'; } ?> Quote Link to comment Share on other sites More sharing options...
methodman Posted September 10, 2009 Author Share Posted September 10, 2009 GREAT IDEA ToonMariner, i've been trying to work this out. since it still doesn't return any checked boxes. The code is sound and it should work..?? this is where i am at right now. <?php // these affiliations need to populate the attr_list table NOT the breed table. $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database'); $affiliationsQuery = "SELECT * FROM affiliations"; $affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); $affiliation = array(); while($row10 = mysqli_fetch_array($affiliationsData)){ array_push($affiliation, $row10); } $affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'"; $data2 = mysqli_query($dbc, $affQuery); $aff_list = array(); while($row2 = mysqli_fetch_array($data2)){ array_push($aff_list, $row2); echo $row2['affiliation_id']; } echo '<fieldset><table><tr>'; foreach($affiliation as $aff){ $checked = (array_search($aff['affiliation_id'], $aff_list) ? ' checked="checked"' : ''); echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]"' . $checked . ' />' . $aff['affiliation'] . '</td>'; } print_r($aff_list); // returns : Array ( [0] => Array ( [0] => 1 [affiliation_id] => 1 ) ) echo '</tr></table></fieldset>'; mysqli_close($dbc); ?> I don't see the issue. The Ternary conditional looks good, and the arrays are populated with data..? What gives? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 10, 2009 Share Posted September 10, 2009 you are not populating your arrays as you think you are... simply pushing an array onto an array creates an element containing an array of the indices you are interested in - making it harder for you to access those elements. use print_r to output the two arrays you build and you should see what I mean. In the mean time try this... <?php // these affiliations need to populate the attr_list table NOT the breed table. $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database'); $affiliationsQuery = "SELECT * FROM affiliations"; $affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); $affiliation = array(); while($row10 = mysqli_fetch_array($affiliationsData)){ foreach($row10 as $key => $val){ $affiliation[$key][] = $val; } } $affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'"; $data2 = mysqli_query($dbc, $affQuery); $aff_list = array(); while($row2 = mysqli_fetch_array($data2)){ $aff_list[] = $row2['affiliation_id']; } echo '<fieldset><table><tr>'; foreach($affiliation['affiliation_id'] as $key => $val){ $checked = (array_search($val, $aff_list) ? ' checked="checked"' : ''); printf('<td><input type="checkbox" value="%1$d" name="affiliations[]"%3$s/>%2$d</td>',$val,$affiliation['affiliation_name'][$key],$checked); } echo '</tr></table></fieldset>'; mysqli_close($dbc); ?> Quote Link to comment 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.