calculation problem, someone good in math?

Discussion in 'PHP' started by falcondriver, Jan 8, 2007.

  1. #1
    hi there,

    im on a image gallery here, and i wanna display x new thumbs per day. if i dont have any new images to show, i wanna go back to the first picture and start over again.
    so if i have 10 images and wanna show 4 new each day, i must display:
    day1: 1-4
    day2: 5-8
    day3: 1-4 (forget 9 and 10, just start over again)
    day4: 5-8
    ...
    every thumb has a database entry, and for this problem i have this values:
    $thumbrate - number of thumbs to show each day, 4 in this case
    $age - age of this gallery in days
    $counter - total number or thumbs in this category, 10 here

    i need: $offset, the number of results to skip, must be 0 for day1 and day3, 4 on day 2 and day 4.

    can someone think about a formula to solve this problem please, or contact me on icq? thanks...
     
    falcondriver, Jan 8, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    // how many have we already shown?
    if ( $age ) {
        $alreadyShown = $age*$thumbrate;
        
        // show next or start over?
        if ( $alreadyShown+$thumbrate < $counter ) {
            $offset = $alreadyShown;
        } else {
            $offset = 0;
        }
    } else {
        $offset = 0;
    }
    PHP:
    I think that works... assuming $age starts at 0 rather than 1.
     
    rodney88, Jan 8, 2007 IP
    falcondriver likes this.
  3. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #3
    hm ok, you solved the easy part - the problem is after "else": that keeps it forever on $offset=0 after the first circle - but it should repeat the whole gallery over and over again.
     
    falcondriver, Jan 8, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're right, my fault. Working version:
    // if age is 0, offset 0, otherwise...
    if ( $age ) {
    	// days for a cycle
    	$daysPerCycle = floor($counter/$thumbrate);
    	// days occured so far in current cycle
    	$inCurrent = $age % $daysPerCycle;
    	// images already shown in this cycle
    	$alreadyShown = $inCurrent*$thumbrate;
    
    	// show next or start over?
    	if ( $alreadyShown+$thumbrate < $counter ) {
    		$offset = $alreadyShown;
    	} else {
    		$offset = 0;
    	}
    } else {
        $offset = 0;
    }
    PHP:
    Hopefully that's better.
     
    rodney88, Jan 8, 2007 IP
  5. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #5
    excellent - its even very close the my non-working solution :)
     
    falcondriver, Jan 8, 2007 IP