Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    82
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Displaying an Array problem

    Here's one for you...

    I have a form with 4 checkboxes. When the form is submitted those that are checked are stored in the database in 4 separate fields as "yes" or left null if they were not checked.

    So, I then want to display on a page the categories (checkboxes) that the person has checked. Obviously I want to separate these with a comma or an & sign. However I don't want a comma or & sign to appear if part of the array is empty. So as an example...

    4 categories: General, Immigration, Housing, Employment.

    The user checks Immigration and Employment. These are stored in the database in fields categoryImmigration and categoryEmployment with the record "yes" and the other two (categoryGeneral and categoryHousing) are left blank.

    I now want to display the categories on a page in the format... Immigration Team, Employment Team.

    So I SELECT * FROM the table with these fields in. I store the results as follows...
    Code:
    $categoryGeneral = mysql_result($result,$i,'categoryGeneral');
    	if ($categoryGeneral == "yes") $category[0] = "General";
    $categoryImmigration = mysql_result($result,$i,'categoryImmigration');
    	if ($categoryImmigration == "yes") $category[1] = "Immigration Team";
    $categoryHousing = mysql_result($result,$i,'categoryHousing');
    	if ($categoryHousing == "yes") $category[2] = "Housing Team";
    $categoryEmployment = mysql_result($result,$i,'categoryEmployment');
    	if ($categoryEmployment == "yes") $category[3] = "Employment Team";
    That's fine, and now I have the result stored in the array $category[]

    I then need to display on the page. As you can now see, I can't do it in the following way, because if a user didn't select one of the checkboxes there would be a redundant comma..

    Code:
    echo $category[0].", ".$category[1].", ".$category[2].", ".$category[3];
    In this example, the following would be displayed on the page...

    , Immigration Team, , Employment Team

    Obviously unacceptable. How can I get around this?

    Thanks a million in advance I have a deadline to meet!!

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    You should probably put this into a while statement but you get the idea, it does the job ...

    Replace
    PHP Code:
    echo $category[0].", ".$category[1].", ".$category[2].", ".$category[3]; 
    with ...

    PHP Code:
    if($category[0]){echo ("".$category[0].",");}
    if(
    $category[1]){echo ("".$category[1].",");}
    if(
    $category[2]){echo ("".$category[2].",");}
    if(
    $category[3]){echo ("".$category[3]."");} 

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    That won't really work, sir.

    Try this instead, replace:

    PHP Code:
    $categoryGeneral mysql_result($result,$i,'categoryGeneral');
        if (
    $categoryGeneral == "yes"$category[0] = "General";
    $categoryImmigration mysql_result($result,$i,'categoryImmigration');
        if (
    $categoryImmigration == "yes"$category[1] = "Immigration Team";
    $categoryHousing mysql_result($result,$i,'categoryHousing');
        if (
    $categoryHousing == "yes"$category[2] = "Housing Team";
    $categoryEmployment mysql_result($result,$i,'categoryEmployment');
        if (
    $categoryEmployment == "yes"$category[3] = "Employment Team";

    echo 
    $category[0].", ".$category[1].", ".$category[2].", ".$category[3]; 
    with

    PHP Code:
    $categories mysql_fetch_assoc($result);
    $count 0;
    $comma "";
    foreach (
    $categories as $cat => $name) {
        if (!empty(
    $name)) {
            if (
    $count 0) {
                
    $comma ", ";
            }
            echo 
    $comma.$name;
        }


  • #4
    New Coder
    Join Date
    Feb 2007
    Location
    Indianapolis, IN
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I am running into an *almost* identical problem myself. I have a database setup with two tables, one for the item and one for categories with an auto_increment primary_key id field on both tables. Categories can be added/deleted with the system I am working on. Now, I am using a list form field in the HTML where you can select multiple categories to store an item.

    I am not the best with arrays and how to use them but it seems to be the best way to do this. I want to store this data into the single selected_categories field in the item table.

    This is my first question: Say there are 6 categories for an item to be listed under. The user selects category 3 and 6. This is then stored into the item table under selected_categories. How can i do this with an array?

    Second: When editing an item through the system, I want to pull the categories from the item table, but also display the categories that were NOT selected initially. Example: Select item 1 from database which happens to be in category 3 and 6, but in the HTML output list field it shows all available categories from the categories table but only has categories 3 and 6 selected. How can this be accomplished?

    Thank you!

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    That's fine, and now I have the result stored in the array $category[]
    Simplest answer:

    PHP Code:
    $categories implode(', '$category);
    echo 
    $categories

  • #6
    New Coder
    Join Date
    Feb 2007
    Location
    Indianapolis, IN
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts
    The code above my post is not related to my question. I only posted my question here since it is similar. Should i make it a new topic instead?

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by Brandt View Post
    The code above my post is not related to my question. I only posted my question here since it is similar. Should i make it a new topic instead?
    Yes, make one that pertains to your question specifically.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •