Trying to exclude a piece of code from running on certain pages. I would like to do something like the following, but this syntax isn't correct.
<?php
if (!is_page('Blog') || $post->post_parent=="19")) {
<?php breadcrumbs(); ?>
} else {}
?>
First error is - PHP tag was not closed.
And second is - there should be opening of PHP tag before else condition.
try this
<?php if (!is_page('Blog') || $post->post_parent=="19")) { ?>
<?php breadcrumbs(); ?>
<?php } else {
//
}
?>
Related
Code from: oc-content/themes/realestate/item.php
I have this piece of PHP code which is part of a 'framework' called OSClass. Basically, I am trying to 'hide' the user (the publisher of the advert) name to non-logged in users. In other words, only if a user is logged in, only then they can see if the publisher users advert.
I found out that I need to add this piece of code osc_is_web_user_logged_in(), which has worked perfectly for another section, however, due to the else statement, the publishers name is still displaying...
How can I amend the else statement? I could remove the else statement, but I'm worried it will 'break' something and I am unsure what osc_item_user_id() does... Do I add another if statement within the else statement (complete PHP newbie here).
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } else { ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
Thanks!
In my opinion you should simple write:
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
As you don't want to display publisher name when user is not logged, you don't need have else section.
You can add an else if statement.
if (osc_item_user_id() != null && osc_is_web_user_logged_in())
{
<?php echo osc_item_contact_name(); ?>
}
else if (user non logged)
{
// your stuff
}
else
{
<?php echo osc_item_contact_name(); ?>
}
if( $user->username == 'XYZ' )
{
echo "hello, XYZ";
}
else
{
echo "hello, guest";
}
In the above code, can i use pure html code which will get executed incase the IF statement is true instead of using echo ?
Yes, you can do it:
if( $user->username == 'XYZ' )
{
?>
hello, XYZ
<?
}
else
{
?>
hello, guest
<?
}
Sometimes it looks better and simplier. But in fact it is much better to put php and html code in different files (separate logic, styles and data).
if( $user->username == 'XYZ' )
{ ?>
<p>Hello <b><i>XYZ</i></b>
<?php }
else { ?>
<p>Hello <b><i>Guest</i></b>
<?php
}
I hope You Got it.
Of course you can write HTML directly by closing the <?php ?> tags.
<?php if( $user->username == 'XYZ') { ?>
hello, XYZ<br>
<?php } ?>
<?php else { ?>
hello, guest<br>
<?php } ?>
You can of course heredoc as well:
<?php
$str = <<<FOO
This is a
demo message
FOO;
echo $str;
?>
but i dont use it since i it messes with any highlighting editor (it thinks it's a text and i like my HTML highlighted)
What i like best is this, especially when my outputs are big:
<?php if( $user->username == 'XYZ') {
include("user_template.php");
}
<?php else { ?>
include("guest_template.php");
<?php } ?>
which are actually just rendering HTML contained there.
Can also be written like this, more readable for several lines.
<?php if ($user->username == 'XYZ') : ?>
Hello, XYZ
<?php else : ?>
Hello, Guest
<?php endif; ?>
I'm displaying all the posts in the category that have a valid date as follows -
<?php $blog = $pages->find('posts');
foreach($blog->children() as $blogpost): ?>
<?php if ($blogpost->title() <= $latest && $blogpost->category == $thisCat): ?>
//HTML for displaying post goes here
<?php endif ?>
<?php end foreach ?>
That works fine when those posts validate my condition, and displays nothing if it doesn't. What I want to do is display an error message (like 'there are no posts here') when there are no posts that pass the condition. I can't just do a simple else condition in that if query because it's inside the foreach loop. I can't take the if query out of the foreach loop because it relies on a variable that is defined as part of it ($blogpost).
Kind of stuck in this catch 22... Any suggestions?
How about...
<?php
$blog = $pages->find('posts');
$found_something = false;
foreach($blog->children() as $blogpost) {
if ($blogpost->title() <= $latest && $blogpost->category == $thisCat) {
$found_something = true;
//HTML for displaying post goes here
}
}
if(!$found_something) {
// display error message
}
?>
By the way, is there a specific reason why you're using the alternative PHP syntax?
I'd create a counter variable which increments each time when you display a post.
<?php $blog = $pages->find('posts');
$displayedPosts = 0;
foreach($blog->children() as $blogpost): ?>
<?php if ($blogpost->title() <= $latest && $blogpost->category == $thisCat):
$displayedPosts++; ?>
//HTML for displaying post goes here
<?php endif ?>
<?php end foreach ?>
<?php
if ($displayedPosts == 0) {
echo 'ERROR!';
}
?>
Using a boolean variable (as tmh did) is probably better in this case unless you want to count the posts.
Just count your posts , if it's 0 display just the message , else execute foreach loop :
<?php $blog = $pages->find('posts');
if(count($blog->children())==0){ echo 'No post Here'; }
else{
foreach($blog->children() as $blogpost): ?>
<?php if ($blogpost->title() <= $latest && $blogpost->category == $thisCat): ?>
//HTML for displaying post goes here
<?php endif ?>
<?php end foreach ?>
<?php } ?>
I have a foreach loop inside of an if statement. My code is in shorthand format. I do not want the foreach loop to execute if the if condition returns true and instead echo out a message 'sorry database is empty!'
Functional Code
<?php if (!$check == 0) : ?>
<?php foreach... ?>
...
<?php endforeach; ?>
<?php endif; ?>
<?php if ($check == 0) echo 'Sorry database is empty!'; ?>
What I don't like about this code is that I have two separate if conditions. Granted it works as it should but I would prefer to to have an else in the first if.
What is the proper syntax for what I want to do?
Alternatively if your answer is something like this:
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Explain how that would stop the foreach from executing.
Just use an else:
<?php if ($check !== 0) : ?>
<?php foreach... ?>
...
<?php endforeach; ?>
<?php else: echo 'Sorry database is empty!'; ?>
<?php endif; ?>
You can also shorten this:
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
to just this:
$var_is_greater_than_two = ($var > 2); // returns true
Why not just add an else to the if ? The code block looks like special rendering, similar to Zenf Framework view script. Unless the special syntax disallows an else, you should be able to do this way.
<?php if (!$check == 0) : ?>
<?php foreach... ?>
...
<?php endforeach; ?>
<?php else : ?>
Sorry database is empty!
<?php endif; ?>
And idea could be to extract the foreach loop to another method.
Another file or same file have your executeLoop method:
<?php
function executeLoop()
{
foreach(..)
{
//do something
}
}
?>
enter code here
A few lines of code later, you can :
<?php (condition)?executeLoop() : echo $something;?>
The good thing is you can reuse your code if you wish later.
I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1