I'm filtering some content on my website via country specific code, I'm trying to add else statements so it doesn't have to run each piece as individual code except whatever I try gives an error:
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
The above gives the following error: Parse error: syntax error, unexpected T_ELSEIF in and refers to the first elseif
As you're combining PHP with direct HTML / OUTPUT. During your code you're printing whitespace in between the ending bracket and the elseif keyword.
PHP's Interpreter looks directly after the } for the elseif keyword but what it finds is a block of outputted data, so it raises an error.
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
What you need to do is to remove the whitespace like so.
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php}}?>
You will notice the lack of space OUTSIDE the PHP blocks.
This should resolve your problem.
Else cannot evaluate a condition, it will be executed if all other conditions are false, think of it in terms of the default statment of a switch.
This:
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
Needs to be this:
<?php else if(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
Or even better to use switch-case for many conditions.
For example:
switch($x) {
case 1:
?> some html <?php
break;
case 2:
?> more html <?php
break;
}
Instead of closing the tags you can echo/print the html.
This will work:
<?php if (function_exists('isCountryInFilter')) {
if(isCountryInFilter(array("us", "ca"))) {
?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }
}
?>
instead of that format use
<?php if($foo == 1): ?>
html code
<?php else if($foo == 2): ?>
other html
<?php else: ?>
more html code
<?php end; ?>
FrEaKmAn's answer will probably work, but I prefer the curly braces myself. Try changing
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
to
<?php } elseif(isCountryInFilter(array("au"))) { ?>
Also, in general, it's good to minimize the number of times you break in and out of php blocks...
(Once this piece is fixed, you'll need to deal with the problem pointed out by DamienL.)
Related
I'm puzzled by the behaviour of PHP 7.4.21. Inside a Wordpress page I have this piece of code:
<span class="team-member-social">
<?php
if(the_field('facebook_link')) { ?>
<i class="<?php the_field('facebook_icon'); ?>"></i>
<?php }
if(the_field('github_link')) { ?>
<i class="<?php the_field('github_icon'); ?>"></i>
<?php }
if(the_field('dribble_link')) { ?>
<i class="<?php the_field('dribble_icon'); ?>"></i>
<?php }
if(the_field('tumblr_link')) { ?>
<i class="<?php the_field('tumblr_icon'); ?>"></i>
<?php } ?>
</span>
the function the_field() is a function of ACF (Advanced Custom Fields) plugin and returns a string with the content the user has set inside the custom field.
The strange behviour I'm referring to is that inside the ifs all the blocks of HTML code (<a...><i...></i></a>) is returned like a simple text containing the value of <?php the_field('XXX_link'); ?> and not in the correct format (a series of social network icons).
When I remove the ifs the pieces of code are shown correctly.
Any suggestion on how this may occur?
the_field() echos the data, you have to use get_field() in the if statement
From the ACF docs here
Please note this function is the same as echo get_field().
like this
<span class="team-member-social">
<?php
if(get_field('facebook_link')) { ?>
<i class="<?php the_field('facebook_icon'); ?>"></i>
<?php }
if(get_field('github_link')) { ?>
<i class="<?php the_field('github_icon'); ?>"></i>
<?php }
if(get_field('dribble_link')) { ?>
<i class="<?php the_field('dribble_icon'); ?>"></i>
<?php }
if(get_field('tumblr_link')) { ?>
<i class="<?php the_field('tumblr_icon'); ?>"></i>
<?php } ?>
</span>
I have the following code.
while ($slideNews = mysqli_fetch_array($slideImage )) {
$cutNews = LimitarCaracteres($slideNews['textNews'], $maxCaracteres = 300);
?>
<li>
<a href="<?php echo ROOT;?>/News/<?php print $slideNews["id"].'/'.$slideNews["friendlyURL"];?>">
<span>
<?php echo $cutNews; ?>
</span>
<h2 class="nomeGame"><?php print $slideNews["game"];?></h2>
<img src=" <?php echo $slideNews['secondImage']; ?> ">
</a>
</li
<?php } ?>
I need to do that in this part of the code:
<h2 class="nomeGame"><?php print $slideNews["game"];?></h2>
html does not run, I just need the part of the text
Is there a way to add a button for a if/else statement? This is my code
<?php if(isset($_SESSION["steamname"]))
//If steamname not equals 0
{
<a class="button-logout" href="steamauth/logout.php">Log Out</a>
}
else
{
<a class="button-login" href="steamauth/login_steam.php">Log In</a>
}
?>
But my server keeps saying that it's a invalid. My understanding of php isn't that great but what I'm trying to do is to make it so that if a user is logged in a logout button will appear and if not it will be login. My current method doesn't work so is it even possible? Thanks.
P.S. I've tried echoing it out, no luck either.
P.S.S I don't think it has anything to do with my isset command. I did a plain echo and it worked out fine.
You need to echo the HTML you want:
<?php if(isset($_SESSION["steamname"]))
//If steamname not equals 0
{
echo '<a class="button-logout" href="steamauth/logout.php">Log Out</a>';
}
else
{
echo '<a class="button-login" href="steamauth/login_steam.php">Log In</a>';
}
?>
Without the echo, PHP will try to parse your HTML as PHP, which won't work.
change your code to. You have to put html tags out side PHP
<?php if(isset($_SESSION["steamname"]))
{ ?>
<a class="button-logout" href="steamauth/logout.php">Log Out</a>
<?php }
else
{ ?>
<a class="button-login" href="steamauth/login_steam.php">Log In</a>
<?php } ?>
OR
You can echo html tags
<?php if(isset($_SESSION["steamname"]))
{
echo '<a class="button-logout" href="steamauth/logout.php">Log Out</a>';
}
else
{
echo '<a class="button-login" href="steamauth/login_steam.php">Log In</a>';
}
?>
If you don't want to echo html as a string, you can do it like this with alternative syntax:
<?php if(isset($_SESSION['steamname'])): ?>
<a class="button-logout" href="steamauth/logout.php">Log Out</a>
<?php else: ?>
<a class="button-login" href="steamauth/login_steam.php">Log In</a>
<?php endif; ?>
I have a simple navigation which uses php to know which "current page" you are on allowing my css to indicate this.
my php code:
<?php echo "\n"; if ($currentPage == 'about.php') { ?>
What I'm trying to do is have this button active as the current page within its child pages?
is there any way of having multiple pages within the above code?
I tried this but it doesn't work
<?php echo "\n"; if ($currentPage == 'about.php about2.php about3.php') { ?>
You can use in_array:
<?php $pages = Array("about.php","about2.php","about3.php"); ?>
<?php echo "\n"; if (in_array($currentPage,$pages)) { ?>
Which will basically go through an array and compare the value ($currentPage) with each of those in the array.
You could use the in_array() function : http://php.net/manual/en/function.in-array.php
if (in_array($currentPage, array('about.php', 'about2.php'))) {
// Do Something
}
(NOT AN ANSWER JUST AN UPDATE)
This is what I have at the moment: (only the about.php activates the switch)
<?php $pages = Array("about.php","about2.php","about3.php"); ?>
<?php echo "\n"; if (in_array($currentPage,$pages)) { ?>
<li class="button on">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
About
</li>
<?php } else { ?>
<li class="button off">
<a class="nav" href="about.php">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
About
</a>
</li>
<?php } ?>
If I change the pages to home.php, contact.php (which were in the navigation originally) they all work turning them all on at the same time?
So im not sure what I need to do in order to include my new pages?
I this is one of my other buttons:
<?php echo "\n"; if ($currentPage == 'contact.php') { ?>
<li class="button on">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
Contact
</li>
<?php } else { ?>
<li class="button off">
<a class="nav" href="contact.php">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
Contact
</a>
</li>
<?php } ?>
Is this why it is picking up the original pages but not my new ones?
I'm getting Parse error: syntax error, unexpected '{'
Here is my code (used in a wordpress comment loop)
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) { ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php } ?>
Try:
<?php if (current_user_can('edit_comment',$comment->comment_ID)) : ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php endif;?>
I'm a big fan of sticking to one language at a time. Context switches are confusing.
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
printf( "<a href='%s' title='Edit Comment'><img src='%s/images/edit-link.php'/></a>",
get_edit_comment_link(),
get_template_directory_uri()
);
}
?>
Though this is personal preference, and I don't see anything technically wrong with the code in your question.
Try this
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"get_edit_comment_link()\" title=\"Edit Comment\">
<img src=\"get_template_directory_uri()/images/edit-link.png\"/>
</a>";
} ?>
also why not
<?php $a=get_edit_comment_link(); $b=get_template_directory_uri();
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"$a\" title=\"Edit Comment\">
<img src=\"$b/images/edit-link.png\"/>
</a>";
} ?>