Jump to content

Modulus Usage


AikenDrum

Recommended Posts

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
Share on other sites

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
Share on other sites

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
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.