0

I'd like to combine this into an if else but whatever I am doing is breaking? Any ideas ? I am new to PHP. Would I would like to do is have the input changed based on the $perms being FALSE.

   <? if ($perms['user_profiles|edit_billable']===TRUE) { ?> 
    <div class="field">
        <label><?=l(273)?></label>
        <div class="input">
               <input type="checkbox" name="billable" value="1" data-bind="checked: userProfile().billable, disable:isBillable();">
        </div>
      </div>
    </div>
<? } ?>

   <? if ($perms['user_profiles|edit_billable']===FALSE) { ?>
    <div class="field">
        <label><?=l(273)?></label>
        <div class="input">
     <!-- This is where the else would go -->     
                <input type="checkbox" name="billable" value="1" data-bind="checked: userProfile().billable, enable:isBillable();">
        </div>
      </div>
    </div>
<? } ?>
8
  • 3
    are short tags enabled? checked for errors? Commented Nov 10, 2017 at 13:38
  • 1
    Could you add what happens and what you expect to happen? Commented Nov 10, 2017 at 13:38
  • could you post your error logs ? Commented Nov 10, 2017 at 13:39
  • Code updated. Will post error log soon. Commented Nov 10, 2017 at 13:39
  • If you are new to PHP, things appear strange at first, because many PHP servers have disabled error displaying and disabled short tags by default. Enable both and you should be fine. (ini_set('display_errors', 1); ini_set('short_open_tags', 1);) Commented Nov 10, 2017 at 13:40

4 Answers 4

1

Until we get those error logs, we won't know if this really does solve your problem, but at least we can pair the code down quite a bit by using a ternary and only outputting the changed section.

<? $data-bind = $perms['user_profiles|edit_billable'] ? "checked: userProfile().billable, enable:isBillable();" : "checked: userProfile().billable, disable:isBillable();" ?> 
<div class="field">
    <label><?=l(273)?></label>
    <div class="input">
        <input type="checkbox" name="billable" value="1" data-bind="<?=$data-bind?>">
    </div>
</div>

Also I'll mention that using short tags, like <? aren't recommended if you intend this code to be portable and used by other people, since support for those tags can be turned off. Instead format like this:

<?php $data-bind = $perms['user_profiles|edit_billable'] ? "checked: userProfile().billable, enable:isBillable();" : "checked: userProfile().billable, disable:isBillable();" ?> 
<div class="field">
    <label><?php echo l(273)?></label>
    <div class="input">
        <input type="checkbox" name="billable" value="1" data-bind="<?php echo $data-bind?>">
    </div>
</div>

While more verbose, is executable by a greater number of people.

Sign up to request clarification or add additional context in comments.

5 Comments

Why would you possibly post this? This has nothing to do with the problem. People might incorrectly consider this a solution to the problem and waste time because of it.
This is a solution to the problem? OP asked how to pair down their code to use better logic, and a ternary like I've set up does just that. Do you really not understand this?
@Glubus, if you could elaborate on what you think would make this a better answer, I'm always willing to learn.
His problbem is that whatever he tried is not working. even though your answer might acchieve OP's end goal, it's not a solution to the problem OP is facing, i..e that his changes cause errors. Your suggestion to change syntax skips the issue OP is facing, and therefor oppertunity to learn. Future readers won't get an answer to this problem. This suggestion would function great as a sidenote or a comment.. Im sorry for my tone in the first comment, I assumed incorrectly that you only posted this as an attempt to get some easy points.
Yeah, I agree until OP posts his code/error pairing, we won't know what the real problem is/how to solve it. Once he does I'll update accordingly. My hope was that by posting paired down code, it might make it easier for OP to debug. I also just got commenting ability, forgot I could have made a short comment on OP. Thanks for the feedback.
0

Have you tried short syntax?

<?php if ($perms['user_profiles|edit_billable']): ?>
      <div class="field">
        <label><?=l(273)?></label>
        <div class="input">
               <input type="checkbox" name="billable" value="1" data-bind="checked: userProfile().billable, disable:isBillable();">
        </div>
      </div>
    </div
<?php else: ?>
      <div class="field">
        <label><?=l(273)?></label>
        <div class="input">
     <!-- This is where the else would go -->     
                <input type="checkbox" name="billable" value="1" data-bind="checked: userProfile().billable, enable:isBillable();">
        </div>
      </div>
    </div>
<?php endif; ?>

probably, you would need to enable those syntax in your php.ini as well

short_open_tag=On

2 Comments

Is this an answer? Or a guess?
Why are you suggesting different syntax? The problem states the if and else are not working, In PHP, they should work. This is not a solution to his problem.
0

Your if and else works properly, but

<?=l(273)?>

Not working. If "l" is custom function, use

<? l(273);?>

instead and it will work.

2 Comments

Elaborate on how it is not working, maybe l() is a function that be defined in an outer scope
Thanks, that's just a localization code - that's working fine.
0

Try this, may be it will solve the problem.

<?php

// true
if ($perms['user_profiles|edit_billable']===TRUE) {  

?>
<div class="field">
    <label><?php=l(273)?></label>
    <div class="input">
           <input type="checkbox" name="billable" value="1" data-bind="checked: userProfile().billable, disable:isBillable();">

    </div>
  </div>
</div>


<?php


}elseif ($perms['user_profiles|edit_billable']===FALSE){
// false
    ?>

<div class="field">
    <label><?php=l(273)?></label>
    <div class="input">
 <!-- This is where the else would go -->     
            <input type="checkbox" name="billable" value="1" data-bind="checked: userProfile().billable, enable:isBillable();">

    </div>
  </div>
</div>



<?php 
 }
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.