joecooper Posted August 9, 2007 Share Posted August 9, 2007 i have a list of resolutions, and i want the code to be able to find which one is the closest. example of what im looking for: $resolution could equal any 2 numbers in ####x#### format eg 800x640, and the code needs to be able to choose which one is closest to that, so should return: 1024x768 the list i have is: 1920x1200 1680x1050 1440x900 1280x800 1600x1200 1280x960 1024x768 1280x1024 Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/ Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 Keep an array of the possible values to compare to and have them be in order. Then, when you get the value to test, iterate through the list comparing it to each one. Stop when you find one that is bigger (or smaller if you order it the other way.) Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/#findComment-319635 Share on other sites More sharing options...
joecooper Posted August 9, 2007 Author Share Posted August 9, 2007 but its using 2 numbers that i dont know how to check them both Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/#findComment-319646 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 try <?php function closest ($res) { $res_array = array ( '1920x1200', '1680x1050', '1440x900' , '1280x800' , '1600x1200', '1280x960' , '1024x768' , '1280x1024' ); $diff = 9999; $result = ''; list ($w, $h) = explode ('x', $res); foreach ($res_array as $str) { list ($w2, $h2) = explode ('x', $str); $d = abs($w2-$w) + abs($h2-$h); if ($d < $diff) { $result = $str; $diff = $d; } } return $result; } echo closest('800x640'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/#findComment-319648 Share on other sites More sharing options...
joecooper Posted August 9, 2007 Author Share Posted August 9, 2007 Hi thanks for that code, the size i put thought it is 1680x1050, but it only seems to return the 1024x768 Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/#findComment-319675 Share on other sites More sharing options...
joecooper Posted August 9, 2007 Author Share Posted August 9, 2007 <?php session_start(); require('db.php'); function closest ($res) { $res_array = array ( '1920x1200', '1680x1050', '1440x900' , '1280x800' , '1600x1200', '1280x960' , '1024x768' , '1280x1024' ); $diff = 9999; $result = ''; list ($w, $h) = explode ('x', $res); foreach ($res_array as $str) { list ($w2, $h2) = explode ('x', $str); $d = abs($w2-$w) + abs($h2-$h); if ($d < $diff) { $result = $str; $diff = $d; } } return $result; } $resolution=($_GET['rez']); $resolution=closest('$resolution'); die($resolution); ?> this always seems to return 1024x768 when i access the URL with http://joeyjoe.co.uk/wallpaper/getpaper.php?how=random&rez=1680x1050 Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/#findComment-319691 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 $resolution=closest('$resolution'); Dont put single quotes around $resoultion - passes the literal "$resolution" instead of it's contents. Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/#findComment-319695 Share on other sites More sharing options...
joecooper Posted August 9, 2007 Author Share Posted August 9, 2007 ah! its always something so simple i miss it Quote Link to comment https://forums.phpfreaks.com/topic/64137-return-the-closest-numbers/#findComment-319696 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.