PHP echo as condition inside PHP if statement - php

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).

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.

Php Session issue?

I have stored an error message in session variable and it gets displayed on page when condition becomes false. Now, i want that on refreshing the page the message disappear thus , freeing up the Session Variable.
Code : To show the error message
<?php
if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php endif; ?>
Storing the error
$_SESSION['er'] = array("Salary Must be between 10000 and 80000.");
header("Location:addSalary.php");
Just unset it right after you printed error. So it will be shown only once
<?php if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php unset($_SESSION['er']); ?>
<?php endif; ?>
Then once you have used/sent the error message you can remove it.
<?php
if (isset($_SESSION['er'])): ?>
echo '<div class="form-errors">';
foreach ($_SESSION['er'] as $mistake):
echo "<p>$mistake</p>";
endforeach;
echo '</div>';
unset($_SESSION['er']);
endif;
?>

get language id to Drupal 7 Display suite template

I am trying to add the current node language to the div container of the template file ( I am using Drupal 7 ), here is the current content file I have override:
<?php if (isset($title_suffix['contextual_links'])): ?>
<?php print render($title_suffix['contextual_links']); ?>
<?php endif; ?>
<?php print $ds_content; ?>
I want it like so:
<div lang='en'>
<?php if (isset($title_suffix['contextual_links'])): ?>
<?php print render($title_suffix['contextual_links']); ?>
<?php endif; ?>
<?php print $ds_content; ?>
</div>
How can I get the language field where I have only $ds_content variable?
Any help, guide, example or reference appreciated!
You get the current node id through this way.
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
/** get node language **/
$language = node_load($nid)->language;
}
Then pass the language to div
<div lang='<?php print $language; ?>'>
<?php if (isset($title_suffix['contextual_links'])): ?>
<?php print render($title_suffix['contextual_links']); ?>
<?php endif; ?>
<?php print $ds_content; ?>
</div>

PHP if condition does not work anymore when inside a PHP foreach statement

One of the conditions in my if statement does not work anymore when the if statement is inside a foreach statement. I understand where does the issue come from, but don't know how to solve it:
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 example, 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.

PHP keeping $variable outside scope of if statement

I've picked up a project from another developer and I'm trying to finish and clean up a bit. This part of the code renders products from a database after checking for a language cookie:
<?php if (get_cookie('spanish')) : ?>
<?php if ($product['details_spanish'] != '') : ?>
<?php $details = explode(':',$product['details_spanish']); ?>
<h3>Especificaciones</h3>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Especificaciones</h3>
<?php endif ?>
<?php endif; ?>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Specifications</h3>
<?php endif; ?>
<?php endif; ?>
<ul>
<?php if ($details) : ?>
<?php foreach ($details as $detail) : ?>
<?php $detail = split(',',$detail); ?>
<?php if ($detail[0] != '' && $detail[1] != '') : ?>
<li>
<strong><?=ucwords($detail[0])?></strong> : <?=$detail[1]?>
</li>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endif; ?>
Sorry, I know this is hard to read due to inconsistency and bad coding (not my project initially. What it's doing is checking for a spanish cookie on the users machine. If it finds it, it pulls the values from the spanish column of the table instead of the regular. It falls back on the english value if there is no spanish value in the table. Also, it creates a $details variable that contains one of the values of the database, and splits it where the : is.
The problem I'm having is on some products, it gives me an error saying it can't find the $details variable, I'm assuming it is because the variable doesn't live outside the scope of the first if statement?
I'm a php noob really but even I know this isn't good practice. is there a better way to clean this up and have my $detail variable available?
Quick workaround is to pre-initialize that variable at the very top:
<?php $details = ''; ?>

Categories