steveracicot
Programmer
I am hoping someone can help me out. I have 2 database tables. 1 table stores the category names ( the number can change, since the administrator can add and delete categories). The second table lists all of the categories a link is opted into.
So, table 1( link_category) may have 7 categories and a link may belong to only 3 of those categories.
The modification form, needs to display 7 categories (from table 1) but needs to check if there is a matching category_id in table 2. If there is a match between category_id in table 1 and in table 2.( it means that the link is in this category, and it should display a "checked" check box with the category name. If it doesnt, an empty check box with category name will be displayed. The result will be a list of checked or unchecked check boxes with no more than the number of categories.
I have however been unsuccessful in achieing this.
In the code below, I have been able to display all 7 categories with the correct opted-in category checked, as long as the last category is checked. If the link is only in category 1, it only lists the first category and does not display the other 6 categories, which defeats the purpose of a link being modified and being added to different additional categories.
I have also attempted to use to nested while loops, but that just gave me a bunch of funky stuff.
Does anybody have a way to solve my problem. ??? if you need further explainatin let me know.
Any help would be greatly appreciated.
-----------------------
table 1
name: link_category
link_category_id
link_category
table 2
name: link_optin
link_id
link_category_id
------------------
So, table 1( link_category) may have 7 categories and a link may belong to only 3 of those categories.
The modification form, needs to display 7 categories (from table 1) but needs to check if there is a matching category_id in table 2. If there is a match between category_id in table 1 and in table 2.( it means that the link is in this category, and it should display a "checked" check box with the category name. If it doesnt, an empty check box with category name will be displayed. The result will be a list of checked or unchecked check boxes with no more than the number of categories.
I have however been unsuccessful in achieing this.
In the code below, I have been able to display all 7 categories with the correct opted-in category checked, as long as the last category is checked. If the link is only in category 1, it only lists the first category and does not display the other 6 categories, which defeats the purpose of a link being modified and being added to different additional categories.
I have also attempted to use to nested while loops, but that just gave me a bunch of funky stuff.
Does anybody have a way to solve my problem. ??? if you need further explainatin let me know.
Any help would be greatly appreciated.
-----------------------
table 1
name: link_category
link_category_id
link_category
table 2
name: link_optin
link_id
link_category_id
------------------
Code:
// array 1 - select all link categories ---
$query = "select link_category_id, link_category from
link_category";
$result = $db->run_query($query);
// array 2 - grab all entries from this table that match
link_id --
$query = "select link_id, link_category_id from link_optin where
link_id = $link";
$s = $db->run_query($query);
// create my outer loop
while (list($link_id, $link_category_id) = mysql_fetch_array($s))
{
$row1 = $link_category_id;
while (list($link_category_id, $link_category ) = mysql_fetch_array($result))
{
$row2 = $link_category_id;
if ($row1 == $row2)
{
print "<input type='checkbox' name='". LINKCAT . "' value=$row2 checked>$link_category<br>";
break;
}
else
{
print "<input type='checkbox' name='". LINKCAT . "'
value=$row2>$link_category<br>";
}
} // end of inner while
}