Hi! I've developed something like this recently, i'll explain to you how
i proceeded, maybe that'll help you to solve your problem :-)

    Here goes:
I have the following tables:
1. users (primary key: user_id)
2. categories (primary key: category_id)

Now, the users may be in different categories, i.e. there is a many-to-many
relationship between users and categories, thus i create the table
user_category which contains the following fields:
1.user_id
2.category_id

Now, for the part where i display the users for modification, i first query
my table users, to display all my users

$all = "select * from users";
$result = mysql_query($all) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
    // i have a link on each user, e.g.
    echo '<a
href="modify.php?id='.$row['user_id'].'>'.$row['user_name'].'</a>';
}


Now on modify.php, i run the following queries

1. // get all details for that particular user (maybe you'll be needing to
display other user details on this page)
$query1 = "select * from users where user_id = '$id'";
$result = mysql_query($query) or die(mysql_error());


2. // query table user_category to get all the category_id that the user
selected
$query2 = "select category_id from user_category where user_id = '$id'";
$result2 = mysql_query($query2) or die(mysql_error());

while ($row = mysql_fetch_array($result2))
{
    $sel_cat_id[] = $row['category_id'];
}


3 . // select all categories and place them in an array (i'll display a list
of checkboxes for each category name and loop through each one to check only
those whose category_id correspond to what the user selected initially)

$category = "select * from category";
$result_category = mysql_query($category) or die(mysql_error());

while ($row = mysql_fetch_array($result_category))
{
    $cat_id[] = $row['category_id'];
    $cat_name[] = $row['category_name'];
}


Now, i display a list of the checkboxes (name of category followed by
checkbox)

for ($i = 0; $i < count($cat_id); $i++)
{
    echo $cat_name[$i]; // display the name of the category
    echo '<input type="checkbox" name="category[]"';

    // now to validate whose checkboxes category_id value corresponds to
that which the user selected

    for ($j = 0; $j < count($sel_cat_id); $j++)
    {
        if (sel_cat_id[$j] == cat_id[$i])  // i forgot if it's this way or
the other way round
        {
            echo "checked";
        }

    // closing your input tag
    echo ">";
    }

}


This should do the trick!
You can try it and modify it to suit your needs. :-)
Now you can check new values and post your form to process the modification.

If that still doesn't work, please let me know, and i'll give you the
solution on Monday cos my stuffs are on my pc at work :-)

Regards,

Yogesh Mahadnac,
Web Developer,
Sohonet Information Technology Ltd,
49, Maurel Lane,
Charles Lees Street,
Curepipe,
Mauritius.
Tel : +230 6704066
Fax: +230 6700511


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to