More efficient way to get total rows?
I need to find the total rows for a query, and at the moment I'm doing this:
$Hotels = new Pod('hotels');
$Hotels->findRecords('name ASC',1,'location.id = '.$RecordLocations->get_field('id').' AND t.publish = 1');
$totalHotels = $Hotels->getTotalRows();
Is there a more efficient way to do this? The query gets called 50+ times in a single page load and it's causing the page to be very laggy.
edited Jul 6 '10 at 11:20
6 Answers
You probably don't need to be generating this sidebar from a query every time you load the page. You could have a static file that displays here and then gets included every time the page renders. This would cut way down on your server time.
I'd say you want to write a post-save helper that gets called if a change is made to your HOTELS pod and writes a static file that contains the HTML code for your sidebar, basically replacing all the ECHO statements with FWRITE.
Your other option would be to bring your whole HOTELS pod into an array, then loop through the array to make the counts. It will still bog down as your site gains popularity, but it might be enough to get you by for a while...
I see with this, you've also got calls to another pod opened as $RecordLocation, making it that much less efficient.
Why are you running this command 50 times in one page, you only need it once .. or is there some special reason you are doing this for ?
After you've done this, you simply do something like this:
<pre>
if( $totalHotels>0 ) { while ( $Hotels->fetchRecord() ) {
$hotel_id = $Hotels->get_field('id');
$hotel_name = $Hotels->get_field('name');
$hotel_bio = $Hotels->get_field('description');
$hotel_photo = $Hotels->get_field('photo');
$hotel_photo = $hotel_photo[0]['guid'];
?>
<div class="hotel">
<h3><?php echo $hotel_name; ?></h3>
<p><?php echo $hotel_bio; ?></p>
<img src="<?php echo $hotel_photo; ?>" />
</div>
<?php
} // endwhile
} // endif
</pre>and your hotels will be all displayed..
I need to get the total number of hotels for each given area and show it to the right of the navigation item (see image below) but doing it the way I've done it seems very laggy

I already cache all my PODs output, but the cache gets cleared fairly regularly (once per day) so a fair number of visitors are going to experience the lag (the cache gets generated on the fly via a page request as there are thousands of combinations of data that can be outputted on the site).
Unfortunately, due to PODs not allowing queries such as location.region.country.id I can't get rid of the extra PODs call for $RecordLocation.
I guess that only leaves me with the array option, will see if that speeds things up.
The array method loads the page faster, will go with that fo rnow, cheers
that's pretty lame, but what about putting something like $somevar=$somevar+1; (or $somevar++, if i get it right) in the while cycle?
edited Jul 6 '10 at 5:07


