ereignis
02-28-2008, 02:21 AM
i am dynamically assigning a number of cells per row to populate a directory page that shows thumbnails, based on the amount of thumbnails available, in an attempt to have as many cells per row throughout (i.e., avoid having a row where there is only one cell). i have the following code which works, but i keep thinking there's a more efficient way to go about doing this... any clues?
<?php
function getNumCells($j) {
for ($i=6;$i>=3;$i--) { // max of 6 cells/row, min of 3
if (($j % $i) == 0) { // take most cells with no remainder if any
$good = $i;
break;
} else {
$val[$i] = ($j % $i); // otherwise store divisor and remainders in array
}
}
if (!isset($good)) { // if no 'clean' result...
$val=array_flip($val); // flip array...
ksort($val); // sort array by remainder...
$good = end($val); // and get number of cells for highest remainder
}
return $good;
}
$nrows=149; // will actually be value of mysql_num_rows
$success = getNumCells($nrows);
echo $success; // will actually be used to limit # of cells per row
?>
<?php
function getNumCells($j) {
for ($i=6;$i>=3;$i--) { // max of 6 cells/row, min of 3
if (($j % $i) == 0) { // take most cells with no remainder if any
$good = $i;
break;
} else {
$val[$i] = ($j % $i); // otherwise store divisor and remainders in array
}
}
if (!isset($good)) { // if no 'clean' result...
$val=array_flip($val); // flip array...
ksort($val); // sort array by remainder...
$good = end($val); // and get number of cells for highest remainder
}
return $good;
}
$nrows=149; // will actually be value of mysql_num_rows
$success = getNumCells($nrows);
echo $success; // will actually be used to limit # of cells per row
?>