SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Doubles in PHP

Hybrid View

  1. #1
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Doubles in PHP

    I'm trying to write a page generation script but some of my calculations will come out to decimal places.

    For example

    $var=10/3;

    would give about 3.333 I would need to round that to 4

    or basically

    $var=$a/$b and always round up, how would I do that?

    Thanks

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $var=ceil($a/$b
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, got a logic question related to this,

    Let's say i'm displaying 3 items per page and I generate a query string to tell me which page i'm on:

    example: http://domain.com/thepage?pageOn=page

    $pageOn=$_GET['pageOn'];

    so if i'm on page 1 I start listing things starting from position 1 in the database table, then if I'm on Page 2 I start listing from point 4 and if i'm on page 3 I start listing from Page 7. How would I calculate where to start from.

    page 1=start 1;
    page 2=start 4;
    page 3=start 7;

    I think it's some type of Algebraic Equation.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    C'mon, this is absolutely basic mathematics:
    Code:
    $start = (($page -1) * $items_per_page) + 1
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're running into the same problem I am, because after page 3 (it works fine up till page 3) on page 4 it resolves to starting at point 10 when it should start at 11, like below:

    page 1=start 1;
    page 2=start 4;
    page 3=start 7;
    page 4=start 11;
    page 5=start 15;
    page 6=start 18;

  6. #6
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait hold on, my problem was in the way I was displaying the pages, you did it right (I was doing it right except for the page calculations) it's working.

    Thanks!

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, they call this pagination if you want to search for more info.

  8. #8
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good to know, thanks


Bookmarks

Posting Permissions

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