math logic help
Posted: Sat Feb 08, 2003 12:21 pm
I am lost on how to do this math logic. I need to be able to show wich results are being displayed, I.E : Showing results 4-6, but i can't figure out the math equation that would do it. any body got an idea? here is my
Code: Select all
<?
//Let's say you have an array:
//And the following settings
$start = 0;
if (isset($_GETїstart])) $start = $_GETїstart];
$MAX_ITEMS = 3; // We'll use 3 for this example
$start -= ($start % $MAX_ITEMS);
$array_count = mysql_num_rows($allsearchresults); // The number of items in the array
echo $showing;
$QSTRING = "submit=$submit&keywords=$keywords&year=$year&make=$make&model=$model&price1=$price1&price2=$price2&city=$city&state=$state&category=$category&pics=$pics&sortby=$sortby";
// You can print the data that is in the array, based on the above settings, like this:
$num_items = ($array_count - $start);
if ($num_items > $MAX_ITEMS) $num_items = $MAX_ITEMS;
for ($x = $start; $x<($num_items + $start); $x++)
{
print "$arrayї$x]<br>\n";
}
// Then for the "prev" button, use:
if ($start > ($MAX_ITEMS - 1))
{
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=".($start - $MAX_ITEMS)."&$QSTRING" class="midTableLinks">Prev</a> \n";
}
// Then you can print the digits like this:
if ($array_count > $MAX_ITEMS)
{
$MAX_DIGITS = 10; //maximum number of digits to show must be an even number
$total_digits = (int)((int)($array_count - 1) / $MAX_ITEMS) + 1;
// Uses 0-based index (ie: the first index is 0):
$current_digit = ($start / $MAX_ITEMS);
// Get the first & last digits (0-based)
$digit_start = 0;
$digit_end = ($total_digits - 1);
if ($total_digits > $MAX_DIGITS)
{
if ($current_digit > ($MAX_DIGITS / 2))
{
$digit_start = $current_digit - ($MAX_DIGITS / 2);
if ($digit_start > ($total_digits - $MAX_DIGITS))
$digit_start = ($total_digits - $MAX_DIGITS);
}
$digit_end = $digit_start + ($MAX_DIGITS - 1);
}
// Print the digits
for ($x = $digit_start; $x <= $digit_end; $x++)
{
if ($x != $current_digit)
{
// 0-based, so ($x + 1) is used
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=".($x * $MAX_ITEMS);
print "&$QSTRING" class="midTableLinks">".($x + 1)."</a> ";
}
else print "<b>" . ($x + 1)."</b> ";
}
// For the "next" button, use:
if ($start < ($array_count - $MAX_ITEMS))
{
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=".($start + $MAX_ITEMS)."&$QSTRING" class="midTableLinks">Next</a> \n";
}
}
?>