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" ;}
?>
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; ?>
feel like i have tried every possible way..right now it is just skipping over the if statement and posting out the html.
<? if ($departmentID == 1 : ?>
<h4>Outreach Questions <?= $departmentID ?><h4>
<h4>Why Interested</h4>
<ul>
<strong>Why are you interested in volunteering as an outreach docent? </strong> <?php echo $whyInterested ?>
</ul>
<h4>Wildlife Issues</h4>
<ul>
<strong>What’s an environmental or wildlife issue you feel passionately about, and why? </strong> <?php echo $wildlifeIssue ?>
</ul>
<h4>Public Speaking</h4>
<ul>
<strong>Do you have prior experience speaking to the public? Please describe. </strong> <?php echo $publicSpeaking ?>
</ul>
<h4>Wildlife Groups</h4>
<ul>
<strong>Do you belong to any animal rights groups (PETA, The Humane Society, etc.)? If so, which ones? </strong> <?php echo $wildlifeGroup ?>
</ul>
<h4>Contributions</h4>
<ul>
<strong>What do you think you’d bring to the outreach volunteer team?</strong> <?php echo $bringToTeam ?>
</ul>
<? endif; ?>
</div>
the idea is to do an else if with id ==2 and so on..but it is pushing out all the HTML regardless of the departmentID.
thanks in advance.
<?php if ($departmentID == 1) {
echo " <h4>Outreach Questions = '$departmentID' </h4> #all html below
</ul>";} # ends block and closes echo
?>
</div>
Try this it uses php to echo out all of the html if the case is true otherwise no html gets echoed
Isn't the pretty first line wrong? You didn't close it:
Change the first line:
<?php if ($departmentID == 1) : ?>
Issue
I'm currently attempting to create a "cell" for each mysql row similar to how twitter has it, but for some reason i'm logging
PHP Parse error: parse error, expecting `','' or `';''
Code
I'm fairly new to PHP I am unsure of why my code is creating this error.
<?php
while ($row = mysql_fetch_array($rs)) {
$unformatted = $row['mcap_HKD'];
$output = number_format($unformatted);
$time = time_elapsed_string($row['time']);
echo
"<div class="content">
<div class="headers">
<div class="info">
<strong class="scode">".$row['scode']."</strong><br>
<span class="sname">".$row['sname']."</span><br>
<span class="industry">".$row['main_industry']."</span>
</div>
<small class="time">."$time."</small>
</div>
<div class="news">
".$row['title']."
</div>
</div>";
}
I know that through testing that I am able to reach the server and that it will output the data just fine on its own, and even when i output single variables onto one div, but for some reason this is not working when i put extra divs like i do above. I have attempted to echo each line by itself but it still returns the same error.
Issue
PHP use " as a string delimiter (it has several string delimiters). When, in you echo statement, you are not escaping the ", it's as if you stop the string. So PHP is waiting for a string concatenation . or , or the ; end of statement character.
Resolution
You must escape " characters in you echo statement as such: \" ; it should look like:
Escape string delimiters
echo
"<div class=\"content\">
<div class=\"headers\">
<div class=\"info\">
<strong class=\"scode\">".$row['scode']."</strong><br>
<span class=\"sname\">".$row['sname']."</span><br>
<span class=\"industry\">".$row['main_industry']."</span>
</div>
<small class=\"time\">."$time."</small>
</div>
<div class=\"news\">
".$row['title']."
</div>
</div>";
Mix HTML and PHP
or do something like:
<?php while ($row = mysql_fetch_array($rs)): ?>
<?php
$unformatted = $row['mcap_HKD'];
$output = number_format($unformatted);
$time = time_elapsed_string($row['time']);
?>
<div class="content">
<div class="headers">
<div class="info">
<strong class="scode"><?= $row['scode']; ?></strong><br>
<span class="sname"><?= $row['sname']; ?></span><br>
<span class="industry"><?= $row['main_industry']; ?></span>
</div>
<small class="time"><?= $time; ?></small>
</div>
<div class="news">
<?= $row['title']; ?>
</div>
</div>
<?php endwhile; ?>
This has the benefit to not use PHP to print HTML tags, and with the <?= PHP opening tags, it's more readable!
I hope this helps you! :)
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();
Anything wrong with this code, it works great, but I don't understand the forth line. Why is closing bracket all by itself? I am a fairly new to PHP and always Google for answers, but I cant figure this one out. Hopefully, I can help others someday. Thanks
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Nothing is wrong with it. You can break in and out of PHP, and that is what this code is doing. Sometimes it's easier to break out of a PHP block to write some HTML, then go back into PHP
It's ending the if statement created on line 2, but line 3 is outputting HTML so php is ended, only to begin on the next line to finish the open statement.
Write it like this you suddenly know:
<div class="errorbox">
<?php
if(isset($error2)) {
echo '<strong class="error">' . $error2 . '</strong>';
}
?>
</div>
Or like so:
<div class="errorbox">
<?php
if(isset($error2)) {
?>
<strong class="error"><?php echo $error2;?></strong>
<?php
}
?>
</div>
This is normal PHP templating. It is ouputting HTML. First bracket is opening, second bracket is closing
There are several ways of doing it:
The best way is as descibed in the question:
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Another way is by echoing:
echo "<div class="errorbox">";
<?php
if(isset($error2)){
echo "<strong class="error">". $error ."</strong>";
}
?>
echo "</div>";