Really basic PHP array break

Soldato
Joined
27 Dec 2005
Posts
17,288
Location
Bristol
I'm using PHP and an array to print HTML divs in rows of 3s for a grid-layout. The code is basically this, less the array content:

Code:
<div class="row ht3" data-aos="fade-up">
<?php
$divcount = 0;
foreach ($work as $value){
print "$value \n";
$divcount = $divcount + 1;
if($divcount % 3 == 0){
?>
</div>
<div class="row ht3" data-aos="fade-up">
<?php
}
}
?>
</div>

What I want is for the foreach() to break so that the last row of the grid contains a full 3, rather than 1 or 2, effectively dropping the last 1 or 2 contents of the array.

I could do a clunky solution where I count the array, divide it by 3, then count the rows as they get created in the foreach() and break it based on that but that seems longwinded.

What's a neater solution?
 
Soldato
OP
Joined
27 Dec 2005
Posts
17,288
Location
Bristol
I would do it by discarding the extra items from the array before looping through it

while($work.lenth % 3 != 0){
array_pop($work);
}

Looks neat! But:

Warning: Use of undefined constant lenth - assumed 'lenth' (this will throw an Error in a future version of PHP) in index.php on line 67
Warning
: A non-numeric value encountered in index.php on line 67
Notice
: Array to string conversion in index.php on line 67

(Also tried .length as I assume that's a typo!)
 
Soldato
OP
Joined
27 Dec 2005
Posts
17,288
Location
Bristol
Yeah it's a fair point. It's never really impacted SEO - we're #1 for a few key local search terms - but obviously every little helps. And for UX, for sure, especially those with **** connections. Given what we do though I wouldn't want to go too far with compression, but I should defo look at dropping the res for the gallery images. And I'll be honest, I've never looked at lazy load before but it's on my radar.

Would you recommend doing it browser-level?
 
Soldato
OP
Joined
27 Dec 2005
Posts
17,288
Location
Bristol
Get it working?
Having checked the site it's miles better. Much much less page load and no page loader gif, which is way better.
My only opinion and take it as you will, is that the first object on the page shouldn't have an animation class set on it. It's fine on desktop but I find on mobile that sometimes the page doesn't load until you start scrolling.

Good work.

Yeah fair point on the first object.

And yeah all fine, most of the work was just sorting out legacy issues. Work page which is one of the biggest (certainly of the top level and most visited) is now 2.1MB, so thanks for that.

At some point I need to move it all over to a proper CMS, but this 'mid' point so to speak would be a prerequisite of that anyway - getting all the projects organised and in a db, sorting out the thumbs properly (folder structure was a legacy mess) - and it's all done in a few days. I also have less experience building with a CMS' from the ground up, apart from blog pages, so there'll be a bit more of a learning curve/time needed before anything changes.
 
Soldato
OP
Joined
27 Dec 2005
Posts
17,288
Location
Bristol
You can drop that 2.1MB a little further. There is a large png of some lady, Adele I think it was, which is quite large and doesn't appear to be displayed as far as I can tell. You might be able to optimise that if it's supposed to be there or remove the reference to it on that page :)

Haha, amazing. It is shown but I've optimised the image and got the page down to 1.4mb.
 
Back
Top Bottom