If statement giving error php - php

I'm very new in PhP and trying to modify the code of may website.
I want to display a line only under certain condition but when I use if in the code, the site is showing blank
Thanks for your support
<?php
if ( $property->post_type != 'land')
{
<div class="property-drow">
<span>
<?php
if($site_language=='en_US') {
echo 'Age of Construction';
} else {
echo 'Âge Construction';
}
?>
</span><p> {echo $construction;} ?></p>
</div>
}?>

Probably the best answer would be to tell you to add some error reporting to your code as you test
Add to the top of your file(s) while testing right after your opening PHP tag for example
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// normal code
In this case you are not starting and stopping the PHP interpreter in the right places
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if ( $property->post_type != 'land')
{
?>
<div class="property-drow">
<span>
<?php
if($site_language=='en_US') {
echo 'Age of Construction';
} else {
echo 'Âge Construction';
}
?>
</span>
<p> <?php echo $construction;?></p>
</div>
<?php
}
?>

You have some html escaping issues.
To dry up your code you could consider using the ternary operator and short echo tag:
<?php if ($property->post_type != 'land'): ?>
<div class="property-drow">
<span><?=
$site_language=='en_US'
? 'Age of Construction'
: 'Âge Construction'
?></span><p><?= $construction ?></p>
</div>
<?php endif; ?>

Related

How to Wrap A Condition Inside Another Condition in PHP

Can anyone please help me wrap the condition inside another condition in php.
I have this code #1 that I want to be inside code #2.
Here's code #1
<?php if( get_field('highlights') ): ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
Here's code #2
<?php if(strstr($_SERVER['HTTP_REFERER'],'www.example.com'))
{
echo '**CODE #1 should be placed here**';
}
?>
Sorry, I don't haev any knowledge in PHP.
Wrapping code 1 inside code 2
After several trial and error, here's what I have to make it work. Please correct me if there's something wrong or to improve.
<?php if (strstr($_SERVER['HTTP_REFERER'], 'www.google.com')){ ?>
<?php if( get_field('highlights') ):?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
<?php } ?>
This should work in theory. Though I'm unsure what the highlights field is for.
<?php
if(strstr($_SERVER['HTTP_REFERER'],'www.example.com')){
if( get_field('highlights') ){ ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php
}
}
?>
You may want to replace <?php the_field('highlights'); ?> with something like <?=highlight ?> and iterate through the highlights in a loop. That depends on the situation though.

This php if else code on my website is not working [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
<?php if (get_the_author_meta('description')) { ?>
<?php
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php else { ?>
<?php if ($author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php } ?>
It is a code to display author name and hyper link it.
From the first if statement if author has a description then go inside
Second if statement: if authors ID is 4 then execute the code below if not then
Else, there is one extra div inside the else statement which is for <div class="mm-author-box"> which is outside the if statement.
The problem is that when I put this code it breaks the page.. Only the header of the website loads and the content below it doesn't because i have placed this code in the php file which is a template for the page content.
I think there is some syntax problem coz I used the code without else statement and it worked.
You missed to close a curly bracket in the end. Add below line as a last line and try :
<?php } ?>
I don't know how strict that template engine is, but this could be the problem;
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
You're closing the div inside the if which was started outside the div. So , place that last </div> below the <?php } ?> and see if that helps
<?php if (get_the_author_meta('description')) {
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } else if ($mh_author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } ?>
</div>
<?php } ?>
And as AnkiiG pointed out, you missed a closing tag, which I fixed without knowing while formatting my answer
Your if else structure is like this,
<?php if (get_the_author_meta('description')) { ?>
<?php if ($author_ID === 4) { ?>
<?php } ?>
<?php else { ?>
<?php if ($mh_author_ID === 9) { ?>
<?php } ?>
You're not closing the first if statement

PHP echo as condition inside PHP if statement

I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

PHP goto alternative

I've just recently started using PHP 5.4 and have noticed as of 5.3 you can use goto to jump to sections of code which I am using to jump from a loop section. My question is after having read this post... Is GOTO in PHP evil? is this bad practice in this case or is it a viable solution?
<?php
while ($thisResult = mysql_fetch_array($result)) {
if($article && $i > 0) {
goto comments;
}
?>
<h2>
<?=$thisResult['post_title']?>
<span><?=$thisResult['post_modified_gmt']?></span>
</h2>
<p class="content">
<?=nl2br($thisResult['post_content']);?>
</p>
<br />
<?php
comments:
if ($article) {
?>
<p class="comment"><?=$thisResult['comment_content']?>
<?php
}
$i++;
}
?>
This is called spaghetti programming and is a bad practice.
http://en.wikipedia.org/wiki/Spaghetti_code
here is what you can do instead for your code
<?php
while ($thisResult = mysql_fetch_array($result)) {
if($article && $i > 0) {
}
else {
?>
<h2>
<?php $thisResult['post_title']?>
<span><?php $thisResult['post_modified_gmt']?></span>
</h2>
<p class="content">
<?php nl2br($thisResult['post_content']);?>
</p>
<br />
<?php
}
if ($article) {
?>
<p class="comment"><?php $thisResult['comment_content']?>
<?php
}
$i++;
}
?>
A simple else solves it. you can probably find more elegant solutions using switch cases or flags.
The problem is that your code will be hard to edit and add stuff to it.
This is rewrite of the same code without using goto, to show you that you can always make it without it
<?php
while ($thisResult = mysql_fetch_array($result)):
if(!$article || $i <= 0): ?>
<h2>
<?php echo $thisResult['post_title']; ?>
<span><?php echo $thisResult['post_modified_gmt']; ?></span>
</h2>
<p class="content">
<?php echo nl2br($thisResult['post_content']); ?>
</p>
<br />
<?php endif; ?>
<?php if ($article): ?>
<p class="comment"><?php echo $thisResult['comment_content']; ?></p>
<?php endif;
$i++;
endwhile;
I used alternative syntax for control structures, and replaced <?= with <?php echo as it's much more readable to me. Also, other commenters gave you some good suggestions regarding separation of markup and db functions etc, so please give it a thought

Categories