Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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(01740);
    foreach (
    $postCounts as $key => $value) {
        
    var_dump(calculateOffset($value));

    Seemd quite straighforward to me, so I wonder if I understood your request completely...
    De gustibus non est disputandum.

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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(3210);

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

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by maltrecho; 10-18-2003 at 12:15 AM.

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #8
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is marvelous...! I always go mad when it comes to maths.

    Thanks a million guys!

  • #9
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    •