AikenDrum Posted February 10, 2010 Share Posted February 10, 2010 Hi there, I am looking for help with the following : I have cameras. I have dvr's - 3 types - 4 channel, 8 channel, and 16 channel. Someone on the web page enters the amount of cameras - I need to work out how many dvr's (dependent on channels) they need My code so far is pretty poor - if($qty % 4 >= 0){ $dvr = 4; } if ($qty > 4 && $qty < 9) { $dvr = 8; } if ($qty > 8 && $qty < 17) { $dvr = 16; } if ($qty > 16) { $mod = $qty % 4; echo "mod = ". $mod."<br><br>"; } echo "You need a ".$dvr. " port dvr"; Example - if $qty=19; It should read "1 * 16 port dvr and 1 * 4 port dvr" Example - if $qty=24; It should read "1 * 16 port dvr and 1 * 8 port dvr" Example - if $qty=30; It should read "2 * 16 port dvr" I tried mucking around with the mod function - and probably need some sort of recursion - but am a bit lost now. Any help greatly appreciated kindest Regards AIken D Link to comment https://forums.phpfreaks.com/topic/191635-modulus-usage/ Share on other sites More sharing options...
kathas Posted February 10, 2010 Share Posted February 10, 2010 Correct me if I understood wrong. you need to cover all the cameras with the minimum amount of dvrs ??? if so a loop will do // $qty your variable $dvrs = array(16=>0,8=>0,4=>0); while ($qty > 0) { if ($qty > { $dvr[16]++; $qty-=16; } else if ($qty > 4) { $dvr[8]++; $qty-=8; } else { $dvr[4]++; $qty-=4; } } Link to comment https://forums.phpfreaks.com/topic/191635-modulus-usage/#findComment-1010253 Share on other sites More sharing options...
AikenDrum Posted February 10, 2010 Author Share Posted February 10, 2010 Hey Kathas - that's it ! MANY thanks - works a treat Kindest Regards AikenD Link to comment https://forums.phpfreaks.com/topic/191635-modulus-usage/#findComment-1010379 Share on other sites More sharing options...
AikenDrum Posted February 10, 2010 Author Share Posted February 10, 2010 Hi there - sorry - I spoke to soon. I have found what I think is a bug: Example 1: Qty = 24 Array ( [16] => 1 [8] => 1 ) CORRECT Example 2: Qty = 25 Array ( [16] => 2 ) WRONG I would expect to see ( [16] => 1 [8] => 1 [4] =>1 ) Excuse me if I was not clear enough - Minimum ports for 25 cameras = 1*16 + 1*8 + 1*4 Perhaps you can see whay is wrong ? Thank you very much AikenD Link to comment https://forums.phpfreaks.com/topic/191635-modulus-usage/#findComment-1010419 Share on other sites More sharing options...
yozyk Posted February 11, 2010 Share Posted February 11, 2010 $dvr16 = floor($qty/16); $dvr8 = floor(($qty-16*$dvr16)/8); $dvr4 = ceil(($qty-16*$dvr16-8*$dvr8)/4); Link to comment https://forums.phpfreaks.com/topic/191635-modulus-usage/#findComment-1010835 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.