1

I am trying to have a web form use different times on the calendar based on if the form is a certain ID, if it is use the first group, if not use the default. I am hoping someone can help me here. I am a novice at php and intermediate at best in coding, I would appreciate any help. Thank you!

public function is_time_disabled($dt, $h, $m) {
$monday = 1;
$tuesday = 2;
$wensday = 3;
$thursday = 4;
$friday = 5;
$saturday = 6;
if (($form_id == 31)
return (
(($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);} 

) if else return (
(($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
}
endif;))
Rob
  • 19
  • 2
  • 1
    Lines 1-6 of your functions are the same. It looks like you just want a conditional on the return. Your conditionals also are a mess. You are mixing every type of syntax PHP offers. – user3783243 Mar 19 '20 at 18:37
  • `if ($form_id = 31)` < that will always be true/equal to 31. You're assigning `=` instead of comparing `==` / `===`. – Funk Forty Niner Mar 19 '20 at 18:48
  • I am more so asking about how to properly have the IF run and then if not that form ID then do the ELSE and endif properly. The == all work as I need them to – Rob Mar 19 '20 at 18:59
  • Only define function once and put the `if` `else` inside it with the `return`s. – AbraCadaver Mar 19 '20 at 19:04
  • I just edited the question to hopefully reflect what you said, where do I go from here? – Rob Mar 19 '20 at 19:09

1 Answers1

0

This could be optimized (alot) and this is just a pointer in the right direction using your existing code, one funtion and two returns:

public function is_time_disabled($dt, $h, $m) {
    $monday = 1;
    $tuesday = 2;
    $wensday = 3;
    $thursday = 4;
    $friday = 5;
    $saturday = 6;

    if ($form_id == 31) {   
        return (
        (($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
        );
    } else {
        return (
        (($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
        );
    }
}

If you anticipate adding other forms then a switch might be better. If $form_id is not found in any of the case statements then default will be executed:

switch ($form_id) {
    case 10:
        //if the same as 31 then fall into the next one (no break no return)
    case 31:
        return (
        (($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
        );
    case 99:
        //return something else
    default:
        return (
        (($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
        );
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Awesome, thank you very much. I am going to give this a whirl and see if it explodes the site shortly. – Rob Mar 19 '20 at 19:17
  • So the case X area would be to call the form ID correct? – Rob Mar 19 '20 at 19:20
  • Yes exactly. Not needed for just 2 outcomes but for more than 2 it is very handy. – AbraCadaver Mar 19 '20 at 19:20
  • Well that didn't cause the site to explode but it still didn't work as expected, there has to be something else in these file overwriting that, in the first file of the settings it has this called, $form_id = 8; $scheduler_page_id = 7199; so i think that would be the problem. Any chance you do freelance work? – Rob Mar 19 '20 at 20:06