Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: help creating math function

1. ## help creating math function

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?

• I'm not a hobby mathematician, but isn't what you ask for close to this?

PHP Code:
``` 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...

• 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.
PHP Code:
``` 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  ```

• That rocks morded, ther's just one prob :

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
[edited] the above should be "...will show up posts 61 to 70" [/edited]

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.

• 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?

• That was a mistake. Should be 61 - 70.

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?

• try

PHP Code:
``` 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!

• That is marvelous...! I always go mad when it comes to maths.

Thanks a million guys!

• This is working pretty good too (I'm becoming better):
PHP Code:
``` \$num_posts = 40; \$last_offset = substr_replace((\$num_posts-1), "0", -1);  ```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•