Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

problem with checkboxes


Guest

Recommended Posts

Posted

Hello Everyone!

 

I have 2 tables in a database....

 

Table 1

id | car manufacturers

 

1 | ford

2 | vauxhall

3 | etc

 

 

Table 2

 

id | name | car manufacturers

 

01 | Billy | vauxhall,ford,etc

02 | John | ford

03 | Mark | NULL

 

 

 

Then i am displaying all of the car manufacturers (table 1) with checkboxes next to the names

 

 

<?

$result = mysql_query("SELECT * FROM table1") or die("Query failed");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

foreach ($line as $col_value) {
$x++;
if ($x == 1) { $id = $col_value; }
if ($x == 2) { $makes = $col_value; }
}

echo '<input type="checkbox" name="manufacturer[]" value="'.$id.'"> '.$makes.'';

$x = 0;
}
?>

 

This code works fine and displays like this...

 

[ ] Vauxhall

[ ] Ford

[ ] etc

 

 

Now the problem im having is...... These checkboxes are displayed on a profile page, and for the user they are displaying for, i want the checkboxes to be pre checked if that user has 'that' manufacturer in their profile.

 

So if "john" has only 'Ford' in their profile, then when all checkboxes are displayed, only the 'Ford' check box is selected.

 

 

[ ] Vauxhall

[ x ] Ford

[ ] etc

 

 

 

I have been experimenting and this is all i could come up with so far....

 

The car manufacturers are stored in the users database (table 2) using an Array. So by using this code i am able to extract the car manufacturers

 

foreach(explode("','", $makes) as $v) { echo $v; }

 

Then by using this code i am able to make a checkbox checked....

 

if($makes=="VAUXHALL") { $checked = "checked"; } else { $checked = ""; }

 

and the code inside of the checkbox would be.........

 

<input type="checkbox" '.$checked.' name="manufacturer[]" value="'.$makes.'">

 

 

 

 

So the problem i have is combining all of the code above into a loop.

i ended up with the correct boxes being checked, but it was duplicating all of the same boxes multiple times for some reason. If any one is able to help me i would be very grateful!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...