vmicchia Posted April 5, 2011 Share Posted April 5, 2011 Ok so I have a few check box fields that are dynamically populated from a database. What I need to do is check the box if it is a selected value(this is an edit form for a product) so here's what I have but it is not working. <?php include("../../../cart/includes/openDbConn.php"); $sql = "SELECT Name FROM FabricType"; $result = mysql_query($sql); $gettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";'; $dogettype = mysql_query($gettype); $vtype = mysql_fetch_assoc($dogettype); $count = count($vtype); $i = 0; while($row = mysql_fetch_array($result)){ if( $i % 3 == 0 ) { echo '</tr><tr>'; } while($vtype = mysql_fetch_assoc($dogettype)){ if($vtyp['Type'] == $row['Name']){ echo '<td><label><input type="checkbox" name="typeGroup1[]" checked="checked" value="'.trim($row["Name"]).'">'.trim(ucfirst($row["Name"])).'</label</td>'; }else{ echo '<td><label><input type="checkbox" name="typeGroup1[]" value="'.trim($row["Name"]).'">'.trim(ucfirst($row["Name"])).'</label</td>'; } } $i++; } ?> When I do this it echoes nothing out. Link to comment https://forums.phpfreaks.com/topic/232775-form-filling-issue/ Share on other sites More sharing options...
dawsba Posted April 6, 2011 Share Posted April 6, 2011 slightly tidier, but not tested <?php include("../../../cart/includes/openDbConn.php"); $sql = "SELECT Name FROM FabricType"; $dogettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";'; $result = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error()); while($results = mysql_fetch_array($result)) { if( $i % 3 == 0 ) { echo '</tr><tr>'; } $result2 = mysql_db_query($dbname,$dogettype,$link) or die("Invalid Query<br>". mysql_error()); while($results2 = mysql_fetch_array($result2)) { if($results2['Type'] == $results['Name']) { $ischecked = 'checked="checked"'; $value = 1; } else { $ischecked = ''; $value = 0; } $i++; $tn = trim(ucfirst($results["Name"])); echo '<td> <label> <input type="checkbox" name="typeGroup1[]" $ischecked value="$value"> '.$tn.' </label> </td>'; } } ?> Link to comment https://forums.phpfreaks.com/topic/232775-form-filling-issue/#findComment-1197538 Share on other sites More sharing options...
vmicchia Posted April 6, 2011 Author Share Posted April 6, 2011 OK I tried that. It echoes out the check boxes correctly. But it does not check any boxes. In the case I am looking it it would only be one check box but it is possible for more to be checked. Link to comment https://forums.phpfreaks.com/topic/232775-form-filling-issue/#findComment-1197690 Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 to have it checked just echo 'checked', not checked=checked? Link to comment https://forums.phpfreaks.com/topic/232775-form-filling-issue/#findComment-1197691 Share on other sites More sharing options...
vmicchia Posted April 6, 2011 Author Share Posted April 6, 2011 Ok got it fixed I had to trim the results for comparison for some reason. Also can a mod mark as solved the button seems to be gone. Thanks. Also, thank you dawsba for your help. Link to comment https://forums.phpfreaks.com/topic/232775-form-filling-issue/#findComment-1197692 Share on other sites More sharing options...
vmicchia Posted April 6, 2011 Author Share Posted April 6, 2011 Ok Maybe not solved. When $results2['Type']) has more than one value it will only check the last value in the array. The code I have now looks like this: include("../../../cart/includes/openDbConn.php"); $sql = "SELECT Name FROM FabricType"; $dogettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";'; $result = mysql_query($sql); while($results = mysql_fetch_array($result)) { if( $i % 3 == 0 ) { echo '</tr><tr>'; } $result2 = mysql_query($dogettype); while($results2 = mysql_fetch_array($result2)) { // echo $results2['Type']; if(trim($results2['Type']) == trim($results['Name'])) { $ischecked = 'checked="checked"'; $value = 1; } else { $ischecked = ''; $value = 0; } } $i++; $tn = trim(ucfirst($results["Name"])); echo '<td> <label> <input type="checkbox" name="typeGroup1[]" '.$ischecked.' value="'.$results['Name'].'"> '.$tn.' </label> </td>'; } Link to comment https://forums.phpfreaks.com/topic/232775-form-filling-issue/#findComment-1197695 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.