pagegen Posted November 25, 2010 Share Posted November 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/ Share on other sites More sharing options...
upp Posted November 25, 2010 Share Posted November 25, 2010 you could use strlen() to get the number of numbers in the string, then calculate where the middle 5 would be and use substr() to get the 5 numbers in the middle Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139476 Share on other sites More sharing options...
pagegen Posted November 25, 2010 Author Share Posted November 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139482 Share on other sites More sharing options...
aabid Posted November 25, 2010 Share Posted November 25, 2010 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.... Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139501 Share on other sites More sharing options...
laffin Posted November 25, 2010 Share Posted November 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139503 Share on other sites More sharing options...
pagegen Posted November 25, 2010 Author Share Posted November 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139515 Share on other sites More sharing options...
pagegen Posted November 25, 2010 Author Share Posted November 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139553 Share on other sites More sharing options...
laffin Posted November 25, 2010 Share Posted November 25, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139589 Share on other sites More sharing options...
pagegen Posted November 25, 2010 Author Share Posted November 25, 2010 nice one mate works great Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139596 Share on other sites More sharing options...
laffin Posted November 25, 2010 Share Posted November 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219807-pick-the-middle-5-numbers/#findComment-1139693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.