Announcement

Collapse
No announcement yet.

checkboxes question and sc_field_display macro

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • checkboxes question and sc_field_display macro

    Hi there

    What you guys use as db field when usinf checkboxes? I tried many things in mysql and best found is the tinyblob as it stores the field as numbers inside mini file in db which doesnt conflict with showing the checkboxes and lookup and was the best, most accurate chose to filter/search when having checkboxes..... do you use other type as mysql field type with checkvoxes?

    Okay, today i had a challenge to mahe a field Of type checkvox has 5 choices displays another field if one specefic (or more) of the choices is selected... i created the ajax event, on click and used sc_field_display macro on/off it worked ok.... but only one choice has to be selected in order to appear the additional field... if the choice is combined with any other choice from the other 4 choics, then the additional field will not display! Any idea how to control this? In other words, i have checkbox with a, b, c, d,e and 'other' i want to select a, b and other, or 'other' alone so it will idsplay the field "otherData" for user to fill other text.... but now i have to select Only 'other' choice in order for the field to display.... any hint? Is there other better way? Or i have to use different data type for this field??

  • #2
    the problem is that the field gets all the selected values in a string like '1,2,3,4,5' if you have 5 options and each one has a number. so you have to evaluate all the combinations for example:

    PHP Code:
    $var = array( {myfield} ); // this will create an array with all the selected values.

    // then you have to use a command like in_array();
    // this will return true if the first checkbox has been selected

    if ( in_array($var) )
    {
        
    sc_field_display'field1''on' );
    }
    else
    {
        
    sc_field_display'field1''off' );
    }

    //repeat that for all the options. 
    Hope this helps.

    Regards

    Comment


    • #3
      kafecadm, thanks a lot dude, you seem got my point, solution seem so correct but i may be doing something wrong here

      i have the "other" option of the checkbox list is number 4

      i put this code in event "onLoad"and also created new ajax event onClick and added the code to it

      $var = array( {list} );
      if (in_array(4, $var) ){sc_field_display( {list_other}, 'on' );}
      else { sc_field_display( {list_other}, 'off' );}


      but seems doesn't work, when clicking on "other" checkbox nothing happens, the field stay hidden, and when on ajax, the loading message shows but also no field does show or being hidden

      what could be wrong? and what you mean by repeat for all options?

      Comment


      • #4
        well what I mean is this.

        if you have 5 values, you have to evaluate all 5 of them so to complement my last post

        PHP Code:
        $var = array( {myfield} ); // this will create an array with all the selected values.

        // then you have to use a command like in_array();
        // this will return true if the first checkbox has been selected

        if ( in_array($var) )
        {
            
        sc_field_display'field1''on' );
        }
        else
        {
            
        sc_field_display'field1''off' );
        }
        if ( 
        in_array($var) )
        {
            
        sc_field_display'field2''on' );
        }
        else
        {
            
        sc_field_display'field2''off' );
        }
        if ( 
        in_array($var) )
        {
            
        sc_field_display'field3''on' );
        }
        else
        {
            
        sc_field_display'field3''off' );
        }
        if ( 
        in_array($var) )
        {
            
        sc_field_display'field4''on' );
        }
        else
        {
            
        sc_field_display'field4''off' );
        }
        if ( 
        in_array(5$var) )
        {
            
        sc_field_display'field5''on' );
        }
        else
        {
            
        sc_field_display'field5''off' );

        remember that your checkbox field is considered as a single one regarding the number of options so you have to evaluate all values in the same code.}

        hope this is what you need.

        Regards

        Comment


        • #5
          okay friend, after many thanks, and applying blindly your advise (didn't work)

          seems we are arguing logic here now, perhaps i'm not explaining well what i want to do or something

          first field is {list} checkboxes 5options, 0,1,2,3, and 4 (4 is other) - internal mysql type is tinyblob, thats why I asked if there is other better type for checkboxes

          second field which should appear if "other" is selected above is: {list_other}

          because, someone could select "other" AND several else options, then 4 could not be alone, hence, the {list_other} field will not show unless "other" selected alone

          good, now in_array will check the already selected stuff from those checkboxes and if 4 "other" is included into the selection, then displays the other field {list_other}

          why should i add in_array(3, $var)? option(3) is something else, I don't want {list_other} to show show if 3 is selected! I just wanna the {list_other} show if option (4) or if it is combined with 0,1,2 or 3 in field {list} then the {list_other} should show, otherwise, no need to show {list_other} if the (4=other) is not checked

          Comment


          • #6
            Lol my bad, I thought you had the need to make a field hide or appear for each one of your options. Now, first I would like to clarify if you really need to use a checkbox, does your user is allowed to select several options at the same time, lets say option 3 plus option 1, if not, then you should use a radio button.

            Secondly i would say that the database type you should use in case only one option can be selected is TINYINT (thats the one i use anyways) otherwise you should use VARCHAR,

            Finally the code should be as simple as

            PHP Code:
            // code to be place in the onClick event of the field list
            $var = array( {list} );
            if ( 
            in_array($var )
            {
                
            sc_field_display'list_other''on' );
            }
            else
            {
                
            sc_field_display'list_other''off' );

            Comment


            • #7
              Yes dear, I want the user to check more than one option, and last one of the options (no4) is "other" which I want display the extra field if this no. 4 is selected but regardless to if user selected also 1,2 or 3 as well

              I've done your suggestion but didn't work, also changed the type from tinyblob to varchar and data is being saved to db as 1;2;3 but the conditional display the field doesn't work! if selected "other" alone, the extra field shows, but if selected other with any other option it will not show

              Comment


              • #8
                Now i get the difference, then this becomes even easier. try this in your onclick event

                PHP Code:
                if ( strpos({list} , '4') )
                {
                    
                sc_field_display('other_list',on);
                }
                else
                {
                    
                sc_field_display('other_list',off);

                in my environment configuration, check-boxes values are separated by coma, which make the in_array work, but since the field is a string lets use STRPOS to solve it, this function will return the position in wish the number 4 is found in the string, the IF will take any value different from 0 as TRUE so if you select 4 at any time this should work.

                There only one consideration, if you try to use this with lets say... 14 values =P, then the condition will be triggered when you select the options 4 and/or the option 14 >.<

                But since this is not the case I hope this works for you. I tried it my self just to be sure.

                Regards

                Comment


                • #9
                  Great! we are almost there

                  Tried it and it work if I select one or more options + other (id=4)

                  but i'm not sure why if i select other alone (id==4) without any of the rest 3 options it doesn't show the {others} field! strange yeah!?

                  also tried to add additional condition only if list==4 or list=='4' but doesn't trigger the extra field unless is selected with some extra options...

                  Comment


                  • #10
                    okay here we go!!! solved this way

                    Code:
                    if ({list} == 4){sc_field_display({other},on);}
                    elseif ( strpos({other} , '4') ) {sc_field_display({other_list},on);}
                    else{sc_field_display({other_list},off);}
                    you are now officially one of the heros

                    appreciated dude

                    Comment


                    • #11
                      Glad i could help.

                      Success my friend.

                      Comment

                      Working...
                      X