Jump to content


Photo

count the number of variables that qualify for if statement?


  • Please log in to reply
1 reply to this topic

#1 a2bardeals

a2bardeals
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationAnn Arbor, MI

Posted 25 October 2006 - 06:58 PM

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:
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?


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 25 October 2006 - 07:07 PM

Use an array
<?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 />";
}  
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users