Jump to content

Sorting pages by multiple values


janlonden
 Share

Recommended Posts

Thanks kongondo! This is what I came up with after reading some of those articles:

<?php $books = $page->children($selector); ?>
<?php if (count($books)) : ?>
<ul>
<?php foreach($books as $book) : ?>
    <li>
        <p>
            <a href="<?=$book->url?>"><?=$book->title?></a>
            Publisher: <?=$book->book_publisher?><br>
            Number of chapters: <?=$book->book_chapters?><br>
            Number of pages: <?=$book->book_pages?>

        </p>
    </li>
<?php endforeach ?>
</ul>
<?php else : ?>
<p>No books were found.</p>
<?php endif ?>

Much better, right?

This style of coding reminds me of the past when i used to use WordPress :)

Link to comment
Share on other sites

I don't think there's anything wrong with your first code snippet. And I really don't think it's necessary to add those CR and TAB characters unless you need the HTML source to look that way. But if you are looking for ways to improve it, you might consider taking a route that reduces the punctuation a bit. For instance, this would produce the same output, but is a little easier to read and type: 

$photos = $page->children($selector);
if(count($photos)) {
  foreach($photos as $photo) echo "
    <ul>
      <li><a href='$photo->url'>$photo->title</a></li>
      <li>Model: $photo->camera_model</li>
      <li>Resolution: $photo->photo_resolution</li>
      <li>Size: $photo->photo_size</li>
    </ul>
    ";
} else {
    echo '<p>No photos found.</p>';
}
Link to comment
Share on other sites

I was just about to say ryan seems to prefer echoing blocks of HTML rather than opening and closing PHP tags in side HTML blocks, but he beat me to it - at the end of the day coding standards should be seen as good suggestions, but use whatever works best for you and whatever is legible :)

I prefer using PHP tags in my HTML templates but I can tell you from experience when you're doing something complicated like a table of data where there are 10 cells on a row all presenting data from fields it can soon get messy opening and closing all those tags.

Link to comment
Share on other sites

I was just about to say ryan seems to prefer echoing blocks of HTML rather than opening and closing PHP tags in side HTML blocks, but he beat me to it - at the end of the day coding standards should be seen as good suggestions, but use whatever works best for you and whatever is legible :)

I prefer using PHP tags in my HTML templates but I can tell you from experience when you're doing something complicated like a table of data where there are 10 cells on a row all presenting data from fields it can soon get messy opening and closing all those tags.

Yeah whatever works :)

I also like to use PHP tags in HTML, being a designer.

Link to comment
Share on other sites

I agree with most of the opinions here - it really is personal preference. So long as it is clear, well indented and commented, it doesn't really matter. There are some conventions which make sense, but there are no definitive rules. I prefer Ryan's code block above, but I actually prefer to echo with single quotes and use double quotes inside since this is what the HTML output looks like. I guess the downside to my approach is the need to: ' . $variable . ' but for some reason that seems better to me - don't really know why. I guess Ryan's combo is probably the simplest - maybe I should switch - hard to break old habits :)

Link to comment
Share on other sites

I prefer Ryan's code block above, but I actually prefer to echo with single quotes and use double quotes inside since this is what the HTML output looks like.

The reason I don't do that is because variables aren't parsed within single quoted strings. Also, HTML and XHTML don't care whether you use single or double quotes, so long as you close the attribute with the same one you started it with. But if you want to use double quotes and have variables parsed, your best bet is to use the heredoc syntax: 

foreach($photos as $photo) echo <<< _OUT
  <ul>
    <li><a href="$photo->url">$photo->title</a></li>
    <li>Model: $photo->camera_model</li>
    <li>Resolution: $photo->photo_resolution</li>
    <li>Size: $photo->photo_size</li>
  </ul>
_OUT; 

I don't use heredocs that often just because the closing identifier (like _OUT) can't be indented at all. So I find it interrupts the flow of code. But if I had a preference for double quoted HTML attributes, I'd probably use them all the time. 

Thanks Ryan for the tip! But I decided to use a different approach. See my post above your post and tell me your thoughts on it

I think that the approach you used is also good. For me personally, that's still a lot of extra punctuation and jumping in/out of PHP. But there's nothing wrong with it, it's good syntax (I sometimes use it too), and it's always good to use whatever you find the most easy to follow for a particular situation.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...