Raoulito
Raoulito

Reputation: 361

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.

Upvotes: 2

Views: 1451

Answers (5)

FantasticJamieBurns
FantasticJamieBurns

Reputation: 3004

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

Upvotes: 0

KhorneHoly
KhorneHoly

Reputation: 4766

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

Upvotes: 1

Tormi Talv
Tormi Talv

Reputation: 217

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.

Upvotes: 0

Kieranmv95
Kieranmv95

Reputation: 828

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.

Upvotes: 0

matthias
matthias

Reputation: 247

    <?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

Upvotes: 0

Related Questions