I am trying to do an if statement if a function returns true and the else if it doesn't
Basically i need to show a html class in the return 1. I have try that below but I got an error.
syntax error, unexpected 'if' (T_IF)
This is what I have tried
<div class="inner">
<div class="stat"
<?php $access->getAccess()
if (getAccess == 1) {
class="unpublished"
}
else {
}
?>
>
</div>
</div>
this is what the function looks like
public function getAccess()
{
return $this->access;
}
How can I do this right?
Try with:
<div class="stat <?php echo $idea->getAccess() ? 'unpublished' : ''; ?>"></div>
You PHP code isn't echoing anything, plus you are missing semicolons.
Try to simplify this a bit:
<div class="stat <?=$idea->getAccess() == 1 ? 'unpublished' : ''?>">
P.S. You need to put all your classes into the same attribute.
To call a function you need to use brackets:
$access = getAccess();//Not getAccess
I note that you call another getAccess function too so you may have meant to do:
$access = $idea->getAccess() ;
if ($access == 1) {
Edit: Others have pointed out you are also missing a ;.
You need to conceptually separate PHP from your HTML. What is happening is that your PHP is being interpreted by your web server resulting in a HTML document that will be delivered to the client. What you want is to add a class to the div if an $idea has access, something like this:
<div class="inner">
<div class="stat <?php echo $idea->getAccess() ? "unpublished" : ""?>">
</div>
</div>
This is equivalent to a much more complex (and in my opinion harder to read):
<div class="inner">
<div class="stat <?php
if ($idea->getAccess()) {
echo "unpublished";
}
?>">
</div>
</div>
You forgot a semi-colon:
getAccess();
Related
I have an if statement which is throwing a syntax error, unexpected T_VARIABLE error and I have no idea why.
The code:
<?php if($contentTop || $contentLeft || $contentCenter || $contentRight): ?>
<div>a bunch of html</div>
<?php endif; ?>
The variable contains eiter an empty string or a bunch of html.
I have tried changing the || to or but it doesn't seem to change anything. neither does opening and closing the if statement with curly brackets. It only seems to work when i only have one variable in the statement. I have also tried using !empty($contentTop) to check the variables.
The server is running PHP version 5.3.10-1ubuntu3.26 but as far as I can see, that should not change anything regarding if statements
EDIT:
On request of the full markup:
<?php
$contentTop = get_field('section2new_content');
$contentLeft = get_field('section2new_left');
$contentCenter = get_field('section2new_center');
$contentRight = get_field('section2new_right');
if( $contentTop || $contentLeft || $contentCenter || $contentRight ): ?>
<div class="bbh-inner-section teaser-3-col" id="section2temp">
<?php if($contentTop): ?>
<div class="grid-container top">
<div class="row">
<div class="col-sm-12">
<?php echo $contentTop; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if($contentLeft || $contentRight || $contentCenter): ?>
<div class="grid-container bottom">
<div class="row">
<div class="col-sm-4 left">
<?php echo $contentLeft; ?>
</div>
<div class="col-sm-4 center">
<?php echo $contentCenter; ?>
</div>
<div class="col-sm-4 right">
<?php echo $contentRight; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
As i noticed myself i have second if / || statement further down, which seems to work fine. It's also not that I mistakenly inserted a wrong type of space or whateever, I have manually rewritten the code multiple times
I tried #Poiz answer which worked. Being a bit suspicious i tried my own original code again, which now also worked. Human error once again. Maybe it was some illegal version of a space or tab.
Would you be open to trying something a little odd? Anyways, it is essentially the same thing you did except that we turned the if Statements into Ternary Operations. This is because like You said; you've tried almost all combinations of if without success, except when only one Condition is supplied.... The Idea here is simply to crunch down the Line that does the main Comparison to a single, simple Boolean Value, which can then be used within the Subsequent if. Perhaps this works for you since your Magical if works only in the Case you have but One Condition to test for...
<?php $state = ($contentTop || $contentLeft || $contentCenter || $contentRight) ? true : false; ?>
<?php if($state): ?>
<div>a bunch of html</div>
<?php endif; ?>
I have a requirement of showing 2 div's based on a condition like if value is '1' show one div else if value is '0' display another div. Below is my code:
<div class="csv" <?php if($campaign[0]->method != 'CSV'){ echo "style='display:none'"; } ?> >
-----------
------------
</div>
<div class="api" <?php if($campaign[0]->method != 'API'){ echo "style='display:none'"; } ?> >
----------
----------
</div>
Code is looking fine but not working properly. I am trying to achieve this in Laravel 5.4 framework. Any help would be appreciated. Thanks
you can do inline if statements like this:
<div class="csv" <?php echo ($campaign[0]->method != 'CSV' ? 'style="display: none;" : ''); ?>></div>
() - starts the if statement and closes
? - is the if condition is met
: - is the else
It is easy to drop into and out of the PHP processor which will allow you to put the logic into the html file. The following also uses PHP Alternative syntax for control structures
I removed the style="display: none"; bits as I assume that was included to hide the alternate <div>.
<?php //drop into PHP processor mode
if($campaign[0]->method != 'CSV'): //now, back to html processing mode ?>
<div class="csv">
<!-- html markup-->
</div>
<?php elseif($campaign[0]->method != 'API'): ?>
<div class="api">
<!-- html markup-->
</div>
<?php endif; ?>
This is not an exact response to your answer, but you need to have in mind that doing display: none you can always show it playing with the css a bit. In order to prevent that, you should use conditional in php to only render what you desire to show, to prevent the code to be visible. I'd do this way:
<?php if($campaign[0] == 'CSV'):?>
<div class="csv">
-----------
------------
</div>
<?php elseif($campaign[0] == 'API'):?>
<div class="api">
----------
----------
</div>
<?endif;
Or using a switch if there are more options than CSV and API
Use short form of if statement, and write it inside of curly braces as laravel blades' echo.
<div class="api" {{ $campaign[0]->method != 'API' ? "style='display:none'" : "" }}>
It may help you
Using this code:
<div class="venus"><span class="ricon">Established</span><span class="locko"><?php the_field('br_estd'); ?></span></div>
However, I need to wrap this within another PHP IF statement:
<?php $mista = get_field('br_presence'); if ( strval($mista) == 'Worldwide') {echo "PLACE CODE ABOVE HERE" ;} ?>
How could I syntax this?
PHP does have an alternative syntax format for control statements that work well within HTML, although it is generally recommended to use templates and keep logic out of your view as much as possible. That being said, you could do something like:
<?php if (strval(get_field('br_presence')) == 'Worldwide') : ?>
<div class="venus">
<span class="ricon">Established</span>
<span class="locko"><?php the_field('br_estd'); ?></span>
</div>
<?php endif; ?>
could be you are looking for
<?php
$mista = get_field('br_presence');
if ( strval($mista) == 'Worldwide') {
echo '<div class="venus">
<span class="ricon">Established</span>
<span class="locko"><?php the_field("br_estd"); ?></span>
</div>';
//echo "PLACE CODE ABOVE HERE" ;}
?>
I have following code.
<?php if(ot_get_option('wide') == 'on') : ?>
<div class="nothing">
<?php else : ?>
<div class="container">
<?php endif; ?>
Some other codes
</div>
Can I put two divs in if statement and close it with one div? Or what is the best practice?
Yes you can in this case, as there will always be one div tag and one closing div tag.
The only issue you might encounter is if you're using an IDE, it might validate your HTML and give you an error due to that, as it'll see two divs and only one closing tag.
If you want a more "proper" way to do it or you want to avoid what I've mentioned, you can do this:
<?php $myClass = null; ?>
<?php if(ot_get_option('wide') != 'on') $myClass = 'container'; ?>
<div class="<?php echo $myClass; ?>">
</div>
I created a conditional statement for my custom theme in Concrete5. My codes goal is to toggle layout. If the current page has a child pages under it, it will display an additional sidebar (<div class="grid_3">) to list the subpages items. If there's no child page it would display a full layout (<div class="grid_13">).
Unfortunately I get a different result. there's something I probably had missed on my condition statement. Instead of just display one layout, It is rendering the two layout.
Below is what my code look like:
<? if($c->getNumChildren()) { ?>
<div class="grid_3">
<?php
$bt_sidenav = BlockType::getByHandle('autonav');
$bt_sidenav->controller->orderBy = 'display_asc';
$bt_sidenav->controller->displayPages = 'below';
$bt_sidenav->controller->displaySubPages = 'all';
$bt_sidenav->render('view');
?>
</div>
<div id="main-content-container" class="grid_10">
<div id="main-content-inner">
<?php
$a = new Area('Main');
$a->display($c);
?>
</div>
</div>
<? } else { ?>
<div id="main-content-container" class="grid_13">
<div id="main-content-inner">
<?php
$a = new Area('Main');
$a->display($c);
?>
</div>
</div>
<? } ?>
While your content generation portions of PHP use proper PHP tags (<?php … ?>), your if/else statements use short tags (<? … ?>) which are often disabled.
Use <?php instead.
try to do like this may this will solve the issue
<?php if($c->getNumChildren()!='') { ?>
...
<?php } else { ?>
...
<?php } ?>