Skip to main content
Known Participant
November 25, 2008
Question

Checkbox Help Needed

  • November 25, 2008
  • 2 replies
  • 830 views
I am having trouble with the query and output. Here is the scenario. I have a 3 tables (locations, categories, location_categories.) There are 20 locations, and 5 categories which are checkboxes. The categories are (lube, paint, brakes, tire and repair). The display form selects all locations and categories. Multiple category selections can be made for each location. The action page takes the submitted information and adds it to the location_categories table. I need to be able to query and output each location with all five categories and show the checked categories for each. The location_categories table has these columns (location_category_id, location_id, category_id).

How do I write the query and properly display it, When Adding or Editing the form.





    This topic has been closed for replies.

    2 replies

    Inspiring
    November 25, 2008
    I'd do it with 3 queries. The first two would select the locations and categories, and the third would select the checked items.
    Known Participant
    November 28, 2008
    How would I do and insert and update using the 3 query method.

    Here are my three select queries.

    <cfquery name="getlocations">
    SELECT locationid, locname
    FROM locations
    </cfquery>

    <cfquery name="getcategories">
    SELECT categoryid, catname
    FROM locations
    </cfquery>

    <cfquery name="loccatselected">
    SELECT
    categoryid
    FROM location_categories
    WHERE locationid=#URL.locationid#
    </cfquery>
    <cfset variables.selectedCategories=ValueList(loccatselected.categoryid)>


    Inspiring
    November 25, 2008
    Joe Science wrote:
    > I need to be able to query and output each location with all five categories


    This is one of the few legitimate uses for a CROSS JOIN. Cross joins produce a deliberate cartesian product. ie The number of records in the first table multiplied by the number of records in the second table. That is often undesirable, as it can produce an extremely large amount of information. However, in your case a CROSS JOIN should produce the desired results: return all combinations of categories and locations.

    SELECT l.location, c.category
    FROM location l CROSS JOIN category c
    ORDER BY l.location, c.category

    > and show the checked categories for each

    You can use an OUTER JOIN with the location_categories table to determine which items were checked. If the location_categories id is NULL, then the category was not checked. Use a CASE statement to return 1 or 0 indicating the checked state. Then cfoutput the query as usual.

    SELECT
    l.location, c.category,
    CASE WHEN lc.locationID IS NULL THEN 0 ELSE 1 END AS WasChecked
    FROM location l CROSS JOIN category c
    LEFT JOIN location_categories lc
    ON l.locationID = lc.locationID AND c.categoryID = lc.categoryID
    ORDER BY l.location, c.category

    Bear in mind CROSS JOINs are not suitable for large tables. A cross join on two tables containing 1000 records each would produce a result of 1 million records. But in your case the tables are small. So the cross join would produce only 100 records. That said, use cross joins with care.

    ie
    locations (20 rows) x categories (5 rows) = result (100 rows)
    Known Participant
    November 25, 2008
    Thanks! I'm using a cfloop to output the query but no checkboxes are checked. I know i'm missing something. how should the output be written to accomodate your query?
    Inspiring
    November 25, 2008
    > but no checkboxes are checked. I know i'm missing something.
    > how should the output be written to accomodate your query?

    You need to use the "WasChecked" value to set the checkbox state. Since the WasChecked value will be either 1 or 0, CF can treat as boolean (ie 1- true, 0-false) . Use whatever syntax is appropriate for your form.

    <cfoutput query="...>
    ...
    <input type="checkbox" .... <cfif wasChecked>checked</cfif>>
    </cfoutput>