I’m using a continuously scrolling jquery script which requires the php to echo divs for the content to be loaded.
The DIVs contain the link to the php script which will pull comments as results in groups of 10.
So I need a div printed for each group of 10. Ordinarily I just paginated results but I need to have them appear on demand as they scroll down.
I’m not sure how the math and loop should look. So lets say, if there are 82 comments…
I assume I need to divide that number by 10 and if its not even add 1 and then loop echo’ing the DIVs that number of times?
Is that the best way to accomplish this? If so, how would I loop for that number of times?
Here is what I came up with that seems to work as expected.
// Check for Decimal
function is_decimal( $val )
{
return is_numeric( $val ) && floor( $val ) != $val;
}
// Load More Comments on Scroll
if ($rowsTotal > 10) {
$comtotal = ($rowsTotal - 10);
$comtotal = ($comtotal / 10);
$comtotal++;
if (is_decimal($comtotal) == 1) {
$comtotal = floor($comtotal);
$comtotal++;
}
// START HERE - FOLLOW OLD PAGINATION FOR MATH LOGIC
for($c = 2; $c <= $comtotal; $c++) {
echo '<div style="padding-bottom:300px">Scroll Down To View More Comments</div>';
echo '<span class="screw" rel="/piccomments.php?id=' . $idbu . '&n=' . $c . '"></span>';
}
}
You want to know about Modulus
It gives the remainder from dividing. So your is_decimal() function can be replaced with that.
if( 0 == ( $num % 10 ) ) {
// $num is a multiple of 10
}
And all this calculating isn’t needed.
$comtotal = ($rowsTotal - 10);
$comtotal = ($comtotal / 10);
$comtotal++;
Boils down to
$comtotal = $rowsTotal / 10;
Plus, for the case where $rowsTotal is <= 10, your code doesn’t define $comtotal so you’ll want to initialize that variable. Aside from this one issue, your code here should work. The stuff above is optimization. Good job finding your own solution!