Jump to content

pick the middle 5 numbers


pagegen

Recommended Posts

Hi guys,

 

I need to pick the middle five numbers from a range of numbers

 

example

 

$lowest number = 0;  // this will allways be 0

 

$highest_number = 10; // this can very from 1 to 10000

 

$selected_number = 5; // this can also very but it will allways be between 0 and highest number

 

the middles numbers from the above 3 varuables should be

3 4 5 6 7 --- the $selected_number number allways needs to be the middle number

 

 

// some of the issue we need to watch out for is, if hughest number is 4 then it should just go

1 2 3 4

 

 

 

 

Thank you guys

Link to comment
Share on other sites

Hey mate

 

cheers for your help,

 

I am not looking in a string to get 5 numbers

 

I have 3 varuables

 

$highest

 

$lowest

 

$selected

 

and i need to basicly read

 

the middle from highest and lowest but they need to be reveloved around the $selected

 

all numbers

Link to comment
Share on other sites

Hey mate

 

cheers for your help,

 

I am not looking in a string to get 5 numbers

 

I have 3 varuables

 

$highest

 

$lowest

 

$selected

 

and i need to basicly read

 

the middle from highest and lowest but they need to be reveloved around the $selected

 

all numbers

 

Please clarify what you want, if you want middle number then this formula will work,

( Sum of all numbers/number of numbers)

 

this will return the average number between those so called middle number....:)

Link to comment
Share on other sites

Wrote this for a pagination routine awhile back

the variables needed

$range = Amount of numbers to display

$curpage = Current number

$pages = Highest Page count

 

        $midrange=intval(floor($range/2));
        $range_s=(($curpage-$midrange)<1?1:(($curpage+$midrange)>$pages?($pages-$range+1):$curpage-$midrange));
        $range_e=(($curpage+$midrange)>$pages?$pages:($curpage-$midrange<1?($range):$curpage+$midrange));

 

I love the trenary operator keeps me from using a bunch of if...elseif..else statements

Link to comment
Share on other sites

Wrote this for a pagination routine awhile back

the variables needed

$range = Amount of numbers to display

$curpage = Current number

$pages = Highest Page count

 

        $midrange=intval(floor($range/2));
        $range_s=(($curpage-$midrange)<1?1:(($curpage+$midrange)>$pages?($pages-$range+1):$curpage-$midrange));
        $range_e=(($curpage+$midrange)>$pages?$pages:($curpage-$midrange<1?($range):$curpage+$midrange));

 

I love the trenary operator keeps me from using a bunch of if...elseif..else statements

 

nice one mate,

 

I am currently doing the same thing haha, will test and let u knw how I got on

Link to comment
Share on other sites

Hi mate

 

can you please expalin a bit more on this code

 

$midrange=intval(floor($range/2));
$range_s=(($curpage-$midrange)<1?1:(($curpage+$midrange)>$pages?($pages-$range+1):$curpage-$midrange));
$range_e=(($curpage+$midrange)>$pages?$pages:($curpage-$midrange<1?($range):$curpage+$midrange));

 

my current function does this

 

for($page = 1; $page <= $maxPage; $page++) {
		   if ($page == $pageNum) {
			  $nav .= " $page "; // no need to create a link to current page
		   }
		   else {
				$nav .= " <a href=\"$query_string&page=$page\">$page</a> ";
		   } 
		}

 

but that just prints every single page to screen, where we only want to display the mid range

Link to comment
Share on other sites

$midrange=intval(floor($range/2));         // find the middle of the range

// FInd the starting range
if(($curpage-$midrange)<1)    // If we subtract midrange from the current page, is it below page 1? 
  $range_s=1;    // Yes, well put the start at page 1 then
elseif(($curpage+$midrange)>$pages) // No, Well than If we add the midrange to the current page, are we above the highest page?
  $range_s=($pages-$range+1)    // Yes, well start at the lowest possible range than
else   $range_s=$curpage-$midrange; // No,  than go ahead subtract midrange from the current page

// find the ending range
if(($curpage+$midrange)>$pages)    // If we add the current page to our midrange, are we above the page count?
  $range_e=$pages;     // Yes, Well use the page count
elseif(($curpage-$midrange<1)    // No, Well than does the Lowest page invalid? 
  $range_e=$range;     // Yes, Well than use the Range instead of calculating
else   $range_e=$curpage+$midrange;   // No,  than go ahead add midrange to the current page

 

Link to comment
Share on other sites

No probs, glad ya likes that piece of code :) Works like a dandy for me :)

as u can see why i love the trenary operator, it cut down the if...elseif...else block to one line. but that was a intermediate example of what it can do. some of my other trenary statements get nested 3 levels deep, setting variables along the way. it gets pretty crazy, so using the parens helps when making the trenary statements.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.