math logic help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

math logic help

Post by chris12295 »

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&#1111;start])) $start = $_GET&#1111;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++) 
&#123; 
   print "$array&#1111;$x]<br>\n"; 
&#125; 
 


// Then for the "prev" button, use: 

if ($start > ($MAX_ITEMS - 1)) 
&#123; 
   print "<a href=""; 
   print $_SERVER&#1111;'PHP_SELF']; 
   print "?start=".($start - $MAX_ITEMS)."&$QSTRING" class="midTableLinks">Prev</a> \n"; 
&#125; 





// Then you can print the digits like this: 

if ($array_count > $MAX_ITEMS) 
&#123; 
   $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) 
   &#123; 
      if ($current_digit > ($MAX_DIGITS / 2)) 
      &#123; 
         $digit_start = $current_digit - ($MAX_DIGITS / 2); 

         if ($digit_start > ($total_digits - $MAX_DIGITS)) 
            $digit_start = ($total_digits - $MAX_DIGITS); 
      &#125; 

      $digit_end = $digit_start + ($MAX_DIGITS - 1); 
   &#125; 

   // Print the digits 
   for ($x = $digit_start; $x <= $digit_end; $x++) 
   &#123; 
      if ($x != $current_digit) 
      &#123; 
      // 0-based, so ($x + 1) is used 
         print "<a href=""; 
         print $_SERVER&#1111;'PHP_SELF']; 
         print "?start=".($x * $MAX_ITEMS); 
         print "&$QSTRING" class="midTableLinks">".($x + 1)."</a> "; 
      &#125; 
      else print "<b>" . ($x + 1)."</b> "; 
   &#125; 
   // For the "next" button, use: 

if ($start < ($array_count - $MAX_ITEMS)) 
&#123; 
   print "<a href=""; 
   print $_SERVER&#1111;'PHP_SELF']; 
   print "?start=".($start + $MAX_ITEMS)."&$QSTRING" class="midTableLinks">Next</a> \n"; 
&#125;
&#125;
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Not sure if I've completely understood you, but here's something I use to pick out a specified number of rows from a result resource (ie a select query). The method might be useful for you. The key is using the mysql_dataseek() fn.

For example, say forum posts are limited to $max, a constant - say 10 per page.

Assume we've already found all the posts and have declared $rows = mysql_num_rows($query).

$choice is just the page we're looking for. We'd get this from a link, defined elsewhere ( - I can post some more about that if you need it).

$pos defines the position of the resource pointer in the dataseek fn

---------------------------------------------
$pos = $choice * $max;
$pos -= $max;

while ($pos < ($choice * $max) AND $pos < $rows) {
mysql_data_seek($query, $pos);
$result = mysql_fetch_array($query);
$VAR = $result['VAR'];
#(...declare any other VARs ...)
#(... echo $VARs or do whatever else you're doing with them...)
}
----------------------------------------------

Actually there's a few checks you might need to do first. The following lines would go above the, er, above:

$total_pages = ceil($rows / $max);
# If $choice == 0 we choose the last page (so passing 0 in a link provides a way to specify last page when we don't know what page number it might be)
# Also, note that if we delete the only item on a multi-page list of topics, or add a new item which takes us...
# ...beyond the current page, $choice supplied to this function is no longer valid: want the last page instead, so:
IF ($choice == 0 OR $choice > $total_pages) {
$choice = $total_pages;
} ELSEIF (is_null($choice)) {
$choice = 1; # auto sets to first page if null
}


Hope that gives you some ideas. I think a lot of people use LIMIT in the SELECT to do something similar. Haven't tried that: would that be faster with a big found set?
Post Reply