Labrat Posted May 11, 2012 Share Posted May 11, 2012 I am trying to prepopulate some checkboxes from two tables, the first one holds the already chosen items, the second holds ALL existing items available. Now, the first tables items consist of values such as 1,2,3,7,22 which is also the ID numbers of the second table, so I need it to show ALL available options and have a check in the ones already chosen so they can be UNchecked if need be. Any help would be greatly appreciated. Code is as follows: function my_features_checkboxesLand($features){ $feature2 = explode(",", $features); $feature3 = implode(',', $feature2); echo ' <div class="clear"></div> <div class="title">Please select the Additional features.</div> '; $result = mysql_query("SELECT * FROM features WHERE f_id IN ( $feature3 ) || f_status='1' AND land_option='1'") or die("Sql error : " . mysql_error()); $f_id = $row['f_id']; $f_name=$row['f_name']; echo $feature3. '<br><br>'; while($row = mysql_fetch_assoc ($result)){ $feature3 = array($row['f_id']); foreach($feature3 as $var) echo '<div class="cbwrapper"><label><input type="checkbox" '; if($feature3){echo $feature3. ' checked ';}elseif($feature3 !=$row['f_id']){echo ''; }; echo 'name="features[]" value="'.$row["f_id"].'">'.$row["f_name"].', '.$row['f_id'].'</label></div>'; $i++; if(($i%4) == 0){ echo '<div class="clearboth"></div>'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/ Share on other sites More sharing options...
StaticFX Posted May 11, 2012 Share Posted May 11, 2012 its kinda hard to follow what you are doing without seeing data... how is the list stored in the DB? do you have the features in one field? CSV? etc like this line $feature3 = array($row['f_id']); whats in f_id? then here foreach($feature3 as $var) you arent even using $VAR? something more like this $chkd = ''; //reset it $chkd = (($var==$row['f_id'])?'CHECKED':''); echo '<div class="cbwrapper"><label><input type="checkbox" name="features[]" value="'.$row["f_id"].'" ' . $chkd . '>'.$row["f_name"].', '.$row['f_id'].'</label></div>'; sorry.. its hard without data. Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1344808 Share on other sites More sharing options...
Labrat Posted May 11, 2012 Author Share Posted May 11, 2012 how is the list stored in the DB? do you have the features in one field? CSV? etc the features are stored in a single column in table 1 separated by a comma $feature3 = array($row['f_id']); whats in f_id? f_id is the id number in table 2 that corresponds to the feature number, such as if there is a feature in table1 that has a value of 2 then the f_id in table 2 would be 2 foreach($feature3 as $var) i am grabbing the $var from the array set in $feature3 = array($row['f_id']); Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1344814 Share on other sites More sharing options...
StaticFX Posted May 11, 2012 Share Posted May 11, 2012 how is the list stored in the DB? do you have the features in one field? CSV? etc the features are stored in a single column in table 1 separated by a comma $feature3 = array($row['f_id']); whats in f_id? f_id is the id number in table 2 that corresponds to the feature number, such as if there is a feature in table1 that has a value of 2 then the f_id in table 2 would be 2 foreach($feature3 as $var) i am grabbing the $var from the array set in $feature3 = array($row['f_id']); 1) im not sure why you are pulling row['fid'] as an array... ? its just one field (string? number?) and not an array 2) if #1 is true.. there will be nothing to loop through in feature 3)right... but do you see in your code, you are looping through the $feature3 setting the value into $var.. but you arent using var. does you query return multiple rows? please post a sample of each field that you are using..... Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1344857 Share on other sites More sharing options...
Labrat Posted May 14, 2012 Author Share Posted May 14, 2012 1) im not sure why you are pulling row['fid'] as an array... ? its just one field (string? number?) and not an array The reason for me pulling the f_id as an array is I thought I needed to do it that way in order to separate the features that have already been used. I was basically trying everything I could think of in order to distinguish which ones had been chosen from the ones that are available the query (or the way i was wanting it to do) is to pull ALL available features from the features table, then inside of the loop have checkboxes for each feature with the ones already chosen checked as default the feature3 (features previously chosen) are stored in the same column of a different table, ie. 1,2,3,4,5. the numbers there correspond to the f_id numbers in a different table which already has the numbers separated with names of each feature, ie, f_id #1 = Well/Septic tank ect. this is a classified ads script I am writting, yes I know there are plenty of others out there already written but you can`t learn how things work if you keep using someone elses scripts. Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1345363 Share on other sites More sharing options...
mrMarcus Posted May 14, 2012 Share Posted May 14, 2012 To turn the comma-delimited string into an array for use, use $arr = explode(',', $row['fid']); However, you should not be storing them that way to begin with as they are not scalable and will always require separation before use. It's better to store them in a separate table and JOIN that table to your initial query using an indexable key identifier between the tables. Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1345366 Share on other sites More sharing options...
Labrat Posted May 14, 2012 Author Share Posted May 14, 2012 mrMarcus, thank you for the reply, at the very top of my script i am already using explode for feature3, f_id is not comma-delimited Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1345376 Share on other sites More sharing options...
Labrat Posted May 17, 2012 Author Share Posted May 17, 2012 anyone got any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1346324 Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 $feature2 = explode(",", $features); $feature3 = implode(',', $feature2); This hurts my brain. $feature3 is actually just $features. No need to convert to an array only to immediately convert back to string. You really need to lose the practice of incrementing your variable names, ie. $features, $features2, $features3, $features4, etc...... Once you start making some larger scale systems, you will drive yourself insane. And please wrap your code in [ code ][ / code ] tags. Also, indenting your code will help with clarity as I cannot look at code that is left-aligned and often, just to help someone out, have to put it into an editor and straighten it out (which = time). i am already using explode for feature3 I don't see an explode() on $feature3 anywhere in your script. To be perfectly honest, your code has many issues with it that will not allow it to run. $f_id = $row['f_id']; $f_name=$row['f_name']; $row has not yet been defined in your code, and therefore, $f_id and $f_name will have no value. Having $feature3 = array($row['f_id']); within a while() loop makes no sense whatsoever. Each iteration clears the $feature3 array() with another single value ($row['f_id']). <?php $feature3 = array(); while($row = mysql_fetch_assoc ($result)) { $feature3[] = $row['f_id']; } foreach($feature3 as $var) { echo '<div class="cbwrapper"><label><input type="checkbox" '; if($feature3){echo $feature3. ' checked ';}elseif($feature3 !=$row['f_id']){echo ''; }; echo 'name="features[]" value="'.$row["f_id"].'">'.$row["f_name"].', '.$row['f_id'].'</label></div>'; $i++; if(($i%4) == 0) { echo '<div class="clearboth"></div>'; } } } Something like that ^ makes more sense, but you're still not using $var and are clearly trying to echo an array (echo $feature3): if($feature3){echo $feature3. ' checked ';}elseif($feature3 !=$row['f_id']){echo ''; }; Honestly, I really can't help you as I would have to completely rewrite your entire code, and to be honest, I'm still not 100% what it is you're trying to accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1346326 Share on other sites More sharing options...
Labrat Posted May 17, 2012 Author Share Posted May 17, 2012 thanks for the help, will try and rewrite everything Quote Link to comment https://forums.phpfreaks.com/topic/262400-prepopulating-checkboxes/#findComment-1346335 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.