Jump to content

checking something exists, issue


Joss
 Share

Recommended Posts

I am using a file field that has two files in it.

I want to check anything is in it before displaying it, so:

            if($page->Files){
                echo "<div class='attachments'>";
                echo "<h3>Attachements</h3>";
                foreach($page->Files as $file) {
                echo "<p><a href='{$file->url}'>{$file->description} - {$file->name}</a></p>";
            }
                echo "</div>";
            }
 
The problem is, even if I have no files loaded to the page, the DIV still displays.
 
I have been up half the night with a bad shoulder, so this is probably just my brain dying
 
 
help!
 
 
Joss
Link to comment
Share on other sites

Doh! ... I knew that!

Thanks, Wanze

More sleep needed!

Since I know Pw my average sleep time's getting shorter and shorter.

Yesterday I went to bed @ 23.00, thinking around Pw-Projects and ideas till midnight and finally realized I couldn't sleep – standing up again to do some coding on a new module till 01.30. :huh:

  • Like 3
Link to comment
Share on other sites

I think the preferred method is actually:

if ($page->files->count() > 0) {

Though I can't for the life of me remember why there was a performance benefit to using PW's method above, I'm sure there definitely was one... possibly because the count is already part of what's loaded into memory in the PageArray and you're not counting things again unnecessarily.

Or I could be talking utter rubbish. Who knows :D

Link to comment
Share on other sites

There is no performance benefit to count($a) vs. $a->count(). They both end up going through the same route. The reason that you can do count($a) on any WireArray derived types is that they implement PHP/SPL's Countable interface. I kind of like the count($a) syntax because it's consistent with array counting syntax. 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...