Hi there forum world.

I have a site where I need help with coding.

All I need to know is how to make this code (below) so that one that says "Once a Week" is checked by default? Of course the user can go into the screen where this code is and change it what they like and it will stay to whatever they have selected, but the default needs to be set to Once a Week. Thanks in advance for your help.

<input type="radio" name="matches_email" value="daily" <?if($member->matches_email=='daily'){?>checked<?}?>> Daily

<input type="radio" name="matches_email" value="3_times_week" <?if($member->matches_email=='3_times_week'){?>checked<?}?>>3 times a week

<input type="radio" name="matches_email" value="once_week" <?if($member->matches_email=='once_week'){?>checked<?}?>>Once a week

<input type="radio" name="matches_email" value="no_mail" <?if($member->matches_email=='no_mail'){?>checked<?}?>> Do not send matches by email

    Give this a try. (next time, use the &#91;php&#93; tags around your code.

    <input type="radio" name="matches_email" value="daily" <? ($member->matches_email=='daily'?"checked":"")?>> Daily
    <input type="radio" name="matches_email" value="3_times_week" <? ($member->matches_email=='3_times_week'?"checked":"")?>>3 times a week
    <input type="radio" name="matches_email" value="once_week" <? ($member->matches_email=='once_week'?"checked":"")?>>Once a week
    <input type="radio" name="matches_email" value="no_mail" <? ($member->matches_email=='no_mail'?"checked":"")?>> Do not send matches by email
    

      Oops forgot about that tag sorry!

      Ok so I see that the checked script has been added, but they all look the same. Is there somethign else I am suppose to do or put within the " " so that Once a Week is checked by default but the others are not?

      or just add the new code to the one that I want checked by default?

        If you order them differently you can solve the problem with no additional logic

        <!-- default has to come first... -->
        <input type="radio" ... checked="checked" />
        <input type="radio" ... <?php if ($some_condifiont) echo 'checked="checked"';?>/>
        

        If you must have them in your present order, you have to make additional checks. I.e. the once a week should be checked if either of these are true

        1. weekly option is true
        2. none of the other options true
        

        Since you only allow one value, you can't have two options being true. I.e. you only need to care about 2. above. And that translates to

        $cond != 'daily' && 
        $cond != '3_times_week' &&
        $cond != 'no_mail'

          We have to have them in that order so we cannot do like demonstrated in in option 1.

          Can you show me how this might look in the real line of code?

          I always have trouble figuring out where to insert the new scripting.

          Here is the line of code how it looks today.....

          <input type="radio" name="matches_email" value="once_week" <?if($member->matches_email=='once_week'){?>checked<?}?>>Once a week

            I'm not a fan of conditions as complex as this used inline, since readability suffers.

            <input type="radio" name="matches_email" value="once_week"
            <?php
            (
              ($member->matches_email != 'daily' &&
              $member->matches_email != '3_times_week' &&
              $member->matches_email != 'no_mail')
                ?"checked":""
            )?>>Once a week
            

              Just as an alternative ...

              echo in_array($member->matches_email, array('daily', '3_times_week', 'no_mail'))? '': 'checked';

                Ah, that's definitely better 🙂

                Just a typo though. It's missing ! in front of in_array.

                  It is correct without the ! ...

                  In pseudo-code:

                  if in_array print '', else print 'checked'

                  Or you could have:

                  if not in_array print 'checked', else print ''

                    Oh, my bad. I never noticed that you switched the output around. Sorry for confusing things.

                      Write a Reply...