Jump to content

How do I store, access and calculate temporary variables?


Lostnode

Recommended Posts

Ok here is what i got so far and now I am stuck.  The user enters a number of how many squares (rectangles is note accurate) they wish to calculate.  If its between 1 and 100 it continues, if its any other number it tells you to put in a number between 1 and 100.  That was easy for me to do.

 

If the number is between 1 and 100 it creates and input for length, width and height of each rectangle. creating the variables as it goes, i.e. "length$i" when $i is the while counter and increases to the ammount of rectangles you wished to view.  Again, not that hard for me.

 

So I am at the point where is I put in 50 I get length1 - through length30, width1 - width50, and height1 - height50.  THis works great.  However I now what to store those variables and calculate the area of each one via a while look which again creates an area$i variable which can be added (it would create area1 - area50 which I then wish to add together)

 

Here is what I have so far.

 

<?php
session_start();  


if (isset($_REQUEST['numpack'])) {
    if ($_REQUEST['numpack'] <= 0 || $_REQUEST['numpack'] > 100 ) {
    $error = "Please enter a number between 1 and 100";    
    echo $error;

?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="0" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td colspan="2">How many Squares</td>
    </tr>
  <tr>
    <td>
      <input name="numpack" type="text"  size="4" maxlength="3"></td>
    <td><input type="submit" name="button" id="button" value="Submit"></td>
  </tr>
</table>
</form>
<?php
} else {
?>
<form name="form2" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php   
   
   
   
   $i=1;
   while($i<=ceil($_REQUEST['numpack']))
  {
  ?>
  <table width="325" border="0" cellspacing="5" cellpadding="5" style="float: left;" class="aluminium">
  <tr>
    <td align="center" rowspan="2"><span class="transparent" style="color: #FFF; font-weight: bold;"><?=$i; ?>.</span></td>
    <td align="center">L</td>
    <td align="center">x</td>
    <td align="center">W</td>
    <td align="center">x</td>
    <td align="center">H</td>
  </tr>
  <tr>
    <td align="center"><input name="length<?=$i; ?>" type="text"  size="4" maxlength="3"></td>
    <td align="center">x</td>
    <td align="center"><input name="width<?=$i; ?>" type="text" size="4" maxlength="3"></td>
    <td align="center">x</td>
    <td align="center"><input name="height<?=$i; ?>" type="text"  size="4" maxlength="3"></td>
  </tr>
</table>
    <?php
  $i++;
  }
?>


</form>
<?php   
   
}
}
If (!isset($_REQUEST['numpack'])) {
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="0" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td colspan="2">How many Squares</td>
    </tr>
  <tr>
    <td>
      <input name="numpack" type="text"  size="4" maxlength="3"></td>
    <td><input type="submit" name="button" id="button" value="Submit"></td>
  </tr>
</table>
</form>    
<?php    
}
?>

 

Now I was reading up on session data and think this might be the best way to go, but again how do I do this? and two I want to make sure that if there are 2 people accessing it at the same time, it does not change the variables on them... (i.e. user one puts in 50 values, and user two puts in 50 different values I do not want cross contamination)

 

Any ideas on how to proceed?

Ok, that's what I was hoping.  So then if I use session there is no chance of cross contamination.

 

So how do I proceed?  I want to store each dynamically created variable, and then call them back again to calculate them.  How do I get them into the session and back out.  If it was a set number I don't think I would have a problem, its the fact that its dynamic that i have a issue with it.  A user could put in 4, then next could put in 50... this is where I get confused.

Ok, here is a thought though, do i even need to store them.  Say I keep sending the initial amount (in my codding the numpack variable) Couls I create where it does the following:

 

$i=1;
   while($i<=ceil($_REQUEST['numpack']))
  {
    $area$i = $width$i * $height$i * $length$i;
  }

 

If that works then I am stumped at the next part.  How do I add all the $area(n) together.

Use arrays, basically --- volume_array[0] = width_array[0] x height_array[0] x length_array[0]

ie

 

for($i = 0; $i<count$width_array; $i++) {
  $volume_array[$i] = $width_array[$i] x $height_array[$i] x $length_array[$i];
}

begin by using arrays in your form.

name="width[]"

name="length[]"

name="height[]"

then in your processing they will be in array for you to use

http://www.deepinphp.com/2011/02/using-array-as-field-names/

Ok, so let me get this strait (never done this before)

 

If I make then named as arrays:

 <table width="325" border="0" cellspacing="0" cellpadding="0" style="float: left;" class="aluminium">
  <tr>
    <td align="center" rowspan="2"><span class="transparent" style="color: #FFF; font-weight: bold;"><?=$i; ?>.</span></td>
    <td align="center">L</td>
    <td align="center">x</td>
    <td align="center">W</td>
    <td align="center">x</td>
    <td align="center">H</td>
  </tr>
  <tr>
    <td align="center"><input name="length[]" type="text"  size="4" maxlength="3" /></td>
    <td align="center">x</td>
    <td align="center"><input name="width[]" type="text" size="4" maxlength="3" /></td>
    <td align="center">x</td>
    <td align="center"><input name="height[]" type="text"  size="4" maxlength="3" /></td>
  </tr>
</table>

 

I can then do this:

for($i = 0; $i<count$width[]; $i++) {
  $volume[$i] = $width[$i] x $height[$i] x $length[$i];
}

 

Is that correct?

 

If so, how do I add teh $volume[] up afterwards?

Ok, I have something I think should work, but its not.

 

If (isset($_REQUEST['numarea'])) {
echo $_REQUEST['numarea'];
$vol=0;
$i=1;
   while($i<=ceil($_REQUEST['numarea']))
  {
    $volume[$i] = $width[$i] * $height[$i] * $length[$i];
    $vol = $vol + $volume[$i];  
    $i++;
  }
  echo $vol;  
}

 

$_REQUEST['numarea'] is the same as numpack, its a carry over through a hidden field. 

 

$vol is coming out as 0 (zero)

This is what I have and it does not work.  I need to pull in from the array I created in the form.

 

If (isset($_REQUEST['numarea'])) {
echo $_REQUEST['numarea'];
$vol=0;
$i=1;
   while($i<=ceil($_REQUEST['numarea']))
  {
    $volume[$i] = $_REQUEST['width[$i]'] * $_REQUEST['height[$i]'] * $_REQUEST['length[$i]'];
    echo $volume[$i];
    $vol = $vol + $volume[$i];  
    $i++;
  }
  echo $vol;  
}

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.