Jump to content

Help with displaying checkboxes and auto checking them


fulleffect

Recommended Posts

Hello Everyone!

I have 2 tables in a database....

[color=blue][u][b]Table 1[/b][/u]
id  |  car manufacturers

1  |  ford
2  |  vauxhall
3  |  etc[/color]


[color=red][u][b]Table 2[/b][/u]

id  |  name  |  car manufacturers

01  |  Billy  |  vauxhall,ford,etc
02  |  John  |  ford
03  |  Mark  |  NULL[/color]



Then i am displaying all of the car manufacturers with checkboxes next to the names


[code]<?

$result = mysql_query("SELECT * FROM makes") 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;
}
?>[/code]

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

[code]foreach(explode("','", $makes) as $v) { echo $v; }[/code]

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

[code]if($makes=="VAUXHALL") { $checked = "checked"; } else { $checked = ""; }[/code]

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

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




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!!!
[code]
<?php

$user = $_POST['user_name'];

$query = "SELECT gen, ind, cg, dcpv, dcpm, admin FROM osi_menurights WHERE user_name = '$user'";
$result = mysql_query($query);
$rowcount = 1;
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $var => $val) {
echo "<input TYPE=CHECKBOX NAME=\"$var\"";
if ($val == '1') {
echo " CHECKED";
}
echo "/> $var<br/>\n";
}
?>
[/code]

I am not strong enough with php to remodify someone else's code, but the above code is how I handled looping checkboxes and checking those that were flagged as checked in the database. Just in case it helps.
Ok, hopefully this is not to confusing, and if it is, go ahead and message me.  Now, not positive this is what will work, but its something you can try with what I do in my code alot.  Php is great with how variables work, so you can really have some flexibility.
1.  Place your code in a while loop
2.  Create a switch statement that switches which number loop you are on.
3.  Set each case to make $var = to the word that is the name of your variable, WITHOUT the $ (the value should be checked or blank)
4.  Use your checkbox code for one line where you add a new checkbox with name $var and value $$var.


If not that clear, it should look something like this...
[code]
<table>
<?
    $conn = mysql_connect("localhost", "username", "password");
    mysql_select_db("table", $conn);

    while ($i != 10)
    {
        echo "<td>";
        switch ($i)
        {
            case 0:
                $var = 'Ford';
                break;
            case 1:
                $var = 'Toyata';
                break;
            .
            .
            .
        }

        $request = "SELECT $var FROM bluecard WHERE COND";
$eval = mysql_query($request, $conn) or die(mysql_error());

        <input type="checkbox" '.$$var' name="manufacturer[]" value="'.$var.'">
 
      echo "</td>";
      $i++;
    }
?>
</table>
[/code]

Now, thats not perfect I know, but it works well.  So you understand, let me explain the $$
we are going to say this
$Ford = 'Checked'
$Var = 'Ford'

than $Var == 'Ford' && $$Var == 'Checked'

Hope this all makes sense and helps you

Archived

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

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