I've been heading down the wrong paths looking for the answers to this problem. In theory, it's straightforward. In practice, however, I'm hitting a brick wall.

I have the code successfully entering a series of checkbox data into a database client_info with an associated userid.

I am trying to have a page show sub-categories and their associated checkboxes from one or more of the 10 selected categories.

For instance, the user selects checkboxes 1, 3, 9, & 10. On the following page, I'd like to have


$query = "SELECT skill_id FROM client_info WHERE userid = '$userid'";

I get kind of lost here. I think I want to have a series of conditional statements such as

if (something=="1"){
echo "Category 1 and subcategory items";
} 
if (something =="2"){
echo "Category 2 and appropriate subcategory items";
}
...
...
...
if (something =="10"){
echo "Category 10 and appropriate subcategory items";
}

My problem is I don't know how to pull out "something" from the query to have it run through each of the appropriate checks to display the information I wish it to. In fact, I don't even know what "something" is supposed to be!

Is there a better solution, and if so, could you please help me? Thanks.

    I don't really understand what it is you're trying to do... what information is the SQL query you posted above supposed to return? It doesn't reflect the example checkbox scenario (those "checked" values don't appear anywhere in the query), so I'm not sure what that query is supposed to be doing.

    Otherwise, I would say all you need is a [man]foreach/man loop. Example, say your HTML for the checkboxes looks like this:

    <input type="checkbox" name="category[]" value="1"> Category 1<br>
    <input type="checkbox" name="category[]" value="2"> Category 2<br>
    <input type="checkbox" name="category[]" value="3"> Category 3<br>
    <!-- etc. -->

    You would then want to do something like:

    if(isset($_POST['category']) && is_array($_POST['category']))
    {
        foreach($_POST['category'] as $cat)
        {
            // output category info here for category number $cat
        }
    }
      bradgrafelman;10997907 wrote:

      I don't really understand what it is you're trying to do... what information is the SQL query you posted above supposed to return?

      The sample query should returns results such as the following:

      userid     skillid
      1              3
      1              4
      1              7
      1              10
      

      I see a problem with my original query - it should have read "select *" not "select skill_id".

        Bilbozilla;10997909 wrote:

        The sample query should returns results such as the following:

        userid     skillid
        1              3
        1              4
        1              7
        1              10
        

        Okay, but how does that relate to your example at all? I still don't see how that data has anything to do with the rest of your post about selected categories and whatnot.

        Bilbozilla;10997909 wrote:

        I see a problem with my original query - it should have read "select " not "select skill_id".


        Most definitely not - you should almost never (and I say 'almost never' only because you should never say never 😉) use 'SELECT
        ' for several reasons. Instead, you should explicitly SELECT the columns from which you actually need data.

          Let me post the steps:

          1. User selects checkboxes on page 1
          2. PHP places checkboxes data into a database and records checkbox # and userid. This occurs behind the scenes with code on page 2.
          3. The new page (page 3) will display 1 or more sets of checkboxes dependent upon which checkboxes were selected on page 1.

          My question pertains to the following:

          If I query the table to get the checkbox data from page 1, how do I evaluate the results from the query so that the code can select which groups of checkboxes to display on page 3?

          And thanks for the "Select *" directive.

            One way would be to retrieve all of the results from the DB and store them in an array; then you'd simply use something like [man]in_array/man to check if a given value exists in the array of checked values.

            Or, you could work in the opposite direction - loop through the results and determine the "groups of checkboxes" for each selection.

              bradgrafelman;10997915 wrote:

              One way would be to retrieve all of the results from the DB and store them in an array; then you'd simply use something like [man]in_array/man to check if a given value exists in the array of checked values.

              Or, you could work in the opposite direction - loop through the results and determine the "groups of checkboxes" for each selection.

              I'm not sure I follow how I'd even begin to code something like this.

                Something like.. what? What specifically don't you know how to do? Execute a SQL query? Fetch the results? Create an array? (Something else)?

                  bradgrafelman;10997918 wrote:

                  Something like.. what? What specifically don't you know how to do? Execute a SQL query? Fetch the results? Create an array? (Something else)?

                  The loop to check for the various matches in the array. Would fetching the results be a simple

                  $quiery=Select userid, skill_id from table…

                  ?

                  And does the query search return an array? Arrays confuse me.

                    Bilbozilla;10997919 wrote:

                    And does the query search return an array? Arrays confuse me.

                    Then it's time to get unconfused, and this is where you do it. Arrays are a very simple concept. It's like a stack of boxes. The keys (numerical or strings) are the labels on the front of the boxes, and the value of each element is what's inside the box.

                    How did you manage to retrieve the categories (main, not sub) from the database? How did you manage to display them in a form? Did you manage to post the users data back to the server? Do you know what $POST / $GET is?

                    If you have solutions or answer "yes" to those question, you pretty much allready have everything you need. The only thing lacking now is using WHERE and ORDER BY in your query.

                    WHERE main_cat_id IN (stuff from form post)
                    -- to get your data order first by main category, then by sub_cat_id
                    ORDER BY main_cat_id ASC, sub_cat_id ASC
                    

                    Except you probably will order by category names instead of ids.

                    When it comes to grouping data under seprate headings for each category, you simply keep track of if you've echoed heading for the current group or not. Then you keep showing subcategories until you get to a sub category with a new main category id. That's where you end the old group and output a new heading. Repeat like before until you hit the next main category id.

                      Write a Reply...