2

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.

7
  • 2
    I would suggest that you echo out the article author and page author between the foreach and if statement to see if the values are similar. This is the right way to compare: $article->author() == $page->author()) not your echo way Commented Dec 18, 2014 at 15:24
  • 1
    PHP is not recursively embeddable, and php code embedded in a php string will NOT get executed. Commented Dec 18, 2014 at 15:26
  • Attempt echoing your article authors to determine whether or not it is well defined. Commented Dec 18, 2014 at 15:28
  • 1
    @Raoulito, what is page('magazine'), why it is not preseded with $ as an array/object var and is it wrapped up with $page->author()? Commented Dec 18, 2014 at 15:33
  • I have provided a working example below, assuming I have interpreted your data correctly. Commented Dec 18, 2014 at 15:36

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

That won't work as he saind that $page->author() only echos one article
Ah, I see, I didn't get the question right. Now I do, but I don't understand Raoulito's intension.
he'S trying to find a way to get all article titles sorted by the author
0

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.

Comments

0

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.

1 Comment

I don't see any use of the echo statements return value (or lack thereof).
0

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

2 Comments

The issue is actually that inside foreach($page->children() as $article): the writer's name $page->author() gets echoed one time for each $page->children() as $article. So the if condition cannot work anymore.
It works fine in the example - the output is at the bottom of the link. Of course this makes an assumption about the shape of your data, if you can provide more detail about the data structure involved I am happy to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.