0

I have a php statement inside my body so that my one page does not take property of body

<body id="page-top" class="single" <?php $actionId = $this->context->action->id; $pages =Yii::$app->params['page']; if(!in_array($actionId,$pages) ? 'style="padding-top:10px;"' :'')  ?>>

Everything is working but not CSS. Have I inserted it in wrong place?

'style="padding-top:10px;"' :'')
1
  • 4
    You're using ternary incorrectly. You don't need an if. Commented Sep 1, 2017 at 14:52

2 Answers 2

8

You're not echoing the style. Furthermore, you're mixing ternary operator and normal if syntax.

<body id="page-top" class="single" <?php $actionId = $this->context->action->id; $pages =Yii::$app->params['page']; echo (!in_array($actionId,$pages)) ? 'style="padding-top:10px;"' :''  ?>>

You can use either the ternary operator:

echo ($condition) ? "true" : "false";

Or the normal syntax:

if ($condition) echo "true";
Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks for the comment! I copied this code, it has no errors , but the page is still same . The css is not working
It must be something with the operands in the condition, did you check them? See here, I set $pages to [2, 3] and $actionId as 1, and it shows the style correctly.
It worked! the only problem was i had to use echo (in_array($actionId,$pages)) instead of echo (!in_array($actionId,$pages))
I see, you had the condition the other way around :) glad you got it working!
1

Did you just forgot the echo before 'style="padding-top:10px;"'?

Also you use the if AND ?-Operator. Just use one of them :)

<body id="page-top" class="single" 
    <?php 
        $actionId = $this->context->action->id;
        $pages =Yii::$app->params['page'];
        echo (!in_array($actionId,$pages)) ? 'style="padding-top:10px;"' : '';
    ?>
>

2 Comments

This has no errors but CSS is still not working on the page!
Sorry, i forgot a closing )

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.