Jump to content


Photo

tricky variable program


  • Please log in to reply
3 replies to this topic

#1 a2bardeals

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

Posted 24 October 2006 - 08:04 PM

i have page1 that asks how many tapes you have, which is sent as a variable to page2 as $tapes which is used in a foreach statement creating the correct number of input fields to ask the length of tape1, tape2, ect....

On the third page i carry over those thengths via the GET method aka tape1=20 tape2=45

Here's where it gets tricky...I would like to create a variable that represents a 60 or 120 min DVD and create a fuction to add the tapes to the dvd and subtract the $tape value from the $dvd value. I also need to not allow that function to be performed if there is not enough space left on the dvd. and a function to create a new 1 or 2 hour dvd. Then calculate how many tapes are on each dvd and how many dvds there are. Is this possisble or only a dream in a young programmers head?

#2 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,891 posts
  • LocationCanada

Posted 24 October 2006 - 09:08 PM

Ok, instead of passing multiple values in the variables tape1, tape2, etc. It would be easier to accomplish this by just passing a single variable with the times of all the tapes comma separated.

Then use the explode command to create an array. You could then accomplish what you want by doing something like this:
<?php
$tapes = explode(",", $_GET['tapes']);
$dvdlength = 120;
$currentDVD = 0;
$dvdIdx = 0;
$i = 0;

foreach ($tapes as $tape) {

  //See if the tape will fit on the current DVD,
  //If not, change to the next DVD
  if (($tape+$dvd[$i][length])>$dvdlength) {
    $dvdIdx++;
  }

  //Add the tape length & name to the current DVD
  $dvd[$dvdIdx][length] += $tape;
  if ($dvd[$dvdIdx][cont]) { $dvd[$dvdIdx][cont] .= ", "; }
  $dvd[$dvdIdx][cont]   .= "Tape-" . $i;
}

?>

that is very simplistic and has no error handling (such as if a tape is too big to fit an a single DVD), but it should be able to get you started.[/code]
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 a2bardeals

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

Posted 25 October 2006 - 05:05 PM

that makes sense however to gather the tape lengths i am using something like:
foreach(range(1, $no_of_tapes) as $no){
echo "<select name=tape'.$no.'>";
echo "<option value=30>30 min</option>";
echo "<option value=60>60 min</option>";
echo "<option value=90>90 min</option>";
echo "</select>";
}

which will always seem to pass mutiple variables even if i make the select name the same for all....

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 05:51 PM

ok, change it to...

foreach (range(1, $no_of_tapes) as $no){
   echo "<select name=tape[]>";
   echo "<option value=30>30 min</option>";
   echo "<option value=60>60 min</option>";
   echo "<option value=90>90 min</option>";
   echo "</select>";
}

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users