a2bardeals Posted October 25, 2006 Share Posted October 25, 2006 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 = 30from 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] Link to comment https://forums.phpfreaks.com/topic/25087-count-the-number-of-variables-that-qualify-for-if-statement/ Share on other sites More sharing options...
Barand Posted October 25, 2006 Share Posted October 25, 2006 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] Link to comment https://forums.phpfreaks.com/topic/25087-count-the-number-of-variables-that-qualify-for-if-statement/#findComment-114380 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.