...

View Full Version : help creating math function



maltrecho
10-17-2003, 10:36 PM
As soon as a new post is inserted, the user is going to be taken to his/her new-last post (just like this forum). For that I am calculating the number of posts immediately after the new post is inserted.

Every page is set by an $offset, so if the $offset is 0 -> posts from 1 to 10 are shown; if $offset = 10 -> posts 11 to 20 are shown and on.

Example1: currently posts = 0; User inserts a new post; number of posts now = 1; $offset must be 0 -> User is taken to page showing posts from 1 to 10.

Example2: currently posts = 17; User inserts a new post; number of posts now = 18; $offset must be 10 -> User is taken to page showing posts from 10 to 20.

Example2: currently posts = 40. User inserts a new post. number of posts now = 41; $offset must be 40 -> User is taken to page showing posts from 40 to 50.

So I need a function to calculate the $offset needed to take the user to the required page knowing only the current number of posts.

Anybody good at maths?

mordred
10-17-2003, 10:54 PM
I'm not a hobby mathematician, but isn't what you ask for close to this?



function calculateOffset($currentPostCount) {
return floor(($currentPostCount) / 10) * 10;
}

// testing
$postCounts = array(0, 17, 40);
foreach ($postCounts as $key => $value) {
var_dump(calculateOffset($value));
}


Seemd quite straighforward to me, so I wonder if I understood your request completely...

missing-score
10-17-2003, 11:49 PM
that should work fine, assuming you understood right. (we both read it the same way.

Here is a function that I already had written that i use when i need to do something like this. This is for use with database querying, so it may be handy if ur using a db.



function MySQL_Limit($offset, $perpage){

$offset_start = ((floor($offset)/$perpage)*$perpage);
$offset_end = ($offset_start+$perpage);

return $offset_start.','.$offset_end;

}

// eg:

MySQL_Limit(32, 10);

// 32 = message number
// 10 = how many per page

maltrecho
10-17-2003, 11:55 PM
That rocks morded, ther's just one prob :eek: :

if one of the $postCounts is 10 or 40 or 70, $offset must be 0, 30 or 60.

$offset 0 will show up posts 1 to 10.

$offset 30 will show up posts 31 to 40

$offset 60 will show up posts 51 to 60
the above should be "...will show up posts 61 to 70"

with the function above $postCounts 40 will give us $offset 40, and that's wrong. That is the main problem. I think we have to subtract 1 somehow somewhere.

missing-score
10-17-2003, 11:58 PM
huh?

that doesnt make much sense...



if one of the $postCounts is 10 or 40 or 70, $offset must be 0, 30 or 60.

$offset 0 will show up posts 1 to 10.

$offset 30 will show up posts 31 to 40

$offset 60 will show up posts 51 to 60


it seems that you contradict urself on that line. Do you mean you need 61-70?

maltrecho
10-18-2003, 12:13 AM
That was a mistake. Should be 61 - 70. :p

missing-score: very interesting your function. I'll keep it for further projects (now it would be very annoying to change the way my queries and code are executed). I have always had problems to find out the last offset. You see this post?

missing-score
10-18-2003, 12:21 AM
try




function getOffset($num){

if($num <= 10){
$offset = 0;
} else {
$nearest10 = round($num, -1);
$offset = ($nearest10 >= $num) ? ($nearest10-10) : ($nearest10);
}
return $offset;
}

example:

echo getOffset(54);



does that work? I hope im understanding right!

maltrecho
10-18-2003, 12:46 AM
That is marvelous...! I always go mad when it comes to maths.

Thanks a million guys! :thumbsup:

maltrecho
10-18-2003, 06:12 PM
This is working pretty good too (I'm becoming better): :o


$num_posts = 40;
$last_offset = substr_replace(($num_posts-1), "0", -1);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum