Jump to content

prepopulating checkboxes


Labrat

Recommended Posts

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>';

 

}

}

}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']);

 

Link to comment
Share on other sites

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.....

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.