samuurai
03-04-2008, 12:41 AM
I'm trying to make a contact list with group memberships. I'm a fairly novice programmer, so please can someone take a quick look and see if you can spot the error(s)!?
Here's my code:
<?php
$sql = "SELECT `group` FROM groups";
//echo ">" . $sql . "<br>";
$result2 = mysql_query($sql);
$sql = "SELECT `group_id` FROM groups";
//echo ">" . $sql . "<br>";
$result = mysql_query($sql);
$group_id = mysql_fetch_array($result);
while($groups = mysql_fetch_array($result2)) {
?><input type="checkbox" name="group[]" value="<? echo $groups['group']; ?>" <?php
$sql = "SELECT `group_id` from groups WHERE `group`=" . '"' . $groups['group'] . '"';
//echo ">" . $sql . "<br>";
$result = mysql_query($sql);
$tmp = mysql_fetch_array($result);
$group_id = $tmp['group_id'];
$sql = "SELECT `group_id` FROM groupmembership WHERE `contact_id`=" . $_GET['id'];
//echo ">" . $sql . "<br>";
$result = mysql_query($sql);
$groupmem_ids = mysql_fetch_array($result);
if(in_array($group_id, $groupmem_ids)) {
echo "checked";
}
echo ">" . $groups['group'] . "<br>";
}
?>
For some reason it produces this:
[ ] Gaynors List
[x]Regulars
[ ]New Customer
the only one ticked is Regulars, but in the groupmembership table, the contact is associated with all three of these group_id's.
Also, is there a better way to do this? I kind've thin this would be a fairly heavy load on DB servers.
Thanks a lot guys!
Beren
Here's my code:
<?php
$sql = "SELECT `group` FROM groups";
//echo ">" . $sql . "<br>";
$result2 = mysql_query($sql);
$sql = "SELECT `group_id` FROM groups";
//echo ">" . $sql . "<br>";
$result = mysql_query($sql);
$group_id = mysql_fetch_array($result);
while($groups = mysql_fetch_array($result2)) {
?><input type="checkbox" name="group[]" value="<? echo $groups['group']; ?>" <?php
$sql = "SELECT `group_id` from groups WHERE `group`=" . '"' . $groups['group'] . '"';
//echo ">" . $sql . "<br>";
$result = mysql_query($sql);
$tmp = mysql_fetch_array($result);
$group_id = $tmp['group_id'];
$sql = "SELECT `group_id` FROM groupmembership WHERE `contact_id`=" . $_GET['id'];
//echo ">" . $sql . "<br>";
$result = mysql_query($sql);
$groupmem_ids = mysql_fetch_array($result);
if(in_array($group_id, $groupmem_ids)) {
echo "checked";
}
echo ">" . $groups['group'] . "<br>";
}
?>
For some reason it produces this:
[ ] Gaynors List
[x]Regulars
[ ]New Customer
the only one ticked is Regulars, but in the groupmembership table, the contact is associated with all three of these group_id's.
Also, is there a better way to do this? I kind've thin this would be a fairly heavy load on DB servers.
Thanks a lot guys!
Beren