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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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;  
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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