Jump to content

count the number of variables that qualify for if statement?


a2bardeals

Recommended Posts

so i have a foreach statement that includes an if statement. There are say, 5 camcorder tapes with the corresponding variables representing the length of each tape.

$tape1 = 30
$tape2 = 60
$tape3 = 30
$tape4 = 90
$tape5 = 30

from get action of a form i have these variables carry over to the next page where i will try to decide which/how many tapes can fit on a 2 hour dvd or which ones need multiple dvds. eg. if 3 tapes are 30 min and 1 tape is 180 min i can fit the 30 min 3 tapes on one 2 hour dvd but the 180 min will require 2 dvds. (this will eventually genereate a price quote for how many master dvds company_x has to make)

ok, so i have the following code:
[code]
foreach ( range(1, $no_of_tapes) as $number){
$name = 'tape'.$number;
  if ($_GET[$name] == 30){

// **HERE IS WHERE I NEED TO COUNT THE NUMBER OF TAPES THAT ARE 30 MIN AND DIVIDE THAT NUMBER BY 4 WHICH WILL GIVE ME THE NUMBER OF DVDS NEEDED.

  }elseif ($_GET[$name] == 60{

// **AGAIN I NEED TO COUNT THE NUMBER OF TAPES THAT ARE 60 MIN AND DIVIDE THAT NUMBER BY 2 WHICH WILL GIVE ME THE NUMBER OF DVDS NEEDED.

  }

}

is this easy to do or even possible?[/code]
Use an array
[code]
<?php
$tape[1] = 30;
$tape[2] = 60;
$tape[3] = 30;
$tape[4] = 90;
$tape[5] = 30;

$counts = array_count_values($tape);

foreach ($counts as $len => $num)  {
    echo "$len : $num<br />";

?>
[/code]

Archived

This topic is now archived and is closed to further replies.

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