Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am hoping someone can help me o

Status
Not open for further replies.

steveracicot

Programmer
Jun 23, 2001
10
US
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



------------------

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 &quot;<input type='checkbox' name='&quot;. LINKCAT . &quot;' value=$row2 checked>$link_category<br>&quot;;


break;

}

else

{

print &quot;<input type='checkbox' name='&quot;. LINKCAT . &quot;'
value=$row2>$link_category<br>&quot;;

}

} // end of inner while

}











 

i managed to do this a while ago.

This is what i did.

i have got a function that i use several times throughout my application. This function takes various arguments as you can see:

function writeCheckBox($FormFieldName, $dbObject, $JoinField, $UpdateIdent, $DisplayValue, $LinkedCount, $LinkedDB, $breakVal) {
$CurrentRow =0;
echo (&quot;<TD valign=top class=\&quot;plainText\&quot;>&quot;);
while ($CheckBoxRow = mysql_fetch_array($dbObject)) {
$DisplayForm = StripSlashes($CheckBoxRow[$DisplayValue]);
print (&quot;<input type=\&quot;checkbox\&quot; name=\&quot;$FormFieldName$CheckBoxRow[$JoinField]\&quot; value=\&quot;1\&quot;&quot;);
if (IsSet($_GET[$UpdateIdent])) {
for ($i=0; $i<=($LinkedCount-1);$i++) {
$DBVal = mysql_result($LinkedDB, $i, $JoinField);
if (($CheckBoxRow[$JoinField]) == ($DBVal)) {
echo &quot;checked&quot;;
}
}
}
print(&quot;>$DisplayForm<BR>&quot;);
$CurrentRow++;
if ($breakVal != 0){
if (bcmod($CurrentRow,$breakVal)==0){
print (&quot;</TD><TD valign=top>&quot;);
}
}
}
if ($breakVal == 0) {
echo (&quot;</TD>&quot;);
}
}

$FormFieldName: the name to assign to the form field
$dbObject: the recordset to loop over(in your case $result)
$JoinField: the fieldname that joins the two tables together(link_category_id in your case)
$UpdateIdent: something i use to show if i am inserting data or updating it

$DisplayValue: the fieldname in the db to get the checkbox info for(linik_category)

$LinkedCount: number of records in your recordset
$LinkedDB: the recordset that contains your linked values
$breakVal: a variable i have for listing out the results in columns

to call the function use this:

<?writeCheckBox(&quot;what_name_you_want_to_give_it&quot;, $result, &quot;link_category_id&quot;, $DoSelect, &quot;link_category&quot;, $ResultCount, $s, &quot;10&quot;);?>

you will need to define the variable $DoSelect for the checking of the checkboxes to happen

hope this helps!

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top