Lostnode Posted February 16, 2011 Share Posted February 16, 2011 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? Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 16, 2011 Share Posted February 16, 2011 each user has their own session variables. Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 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. Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 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. Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 16, 2011 Share Posted February 16, 2011 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]; } Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 Ok, my php knowledge is lacking, so I apologize if I seem a bit DUMB. How do I get to the varaibles $width_array, $height_array and $length_array? Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 16, 2011 Share Posted February 16, 2011 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/ Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 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? Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 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) Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 Ok, I think I know what is wrong... I am not declaring $width[]... Normally when I pass a variable from a form i use the $_REQUEST[''] variable... i.e. $width = $_REQUEST['width']; So how do I do this with an array? Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 16, 2011 Share Posted February 16, 2011 form method="post" then access the form data using $_$POST['fieldnamehere'] Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 Instead of post I use $_REQUEST, does same thing reallu... howeever how do i call an arrayed variable? $_REQUEST['filed[$i]'] doesn;t seem to work right Quote Link to comment Share on other sites More sharing options...
Lostnode Posted February 16, 2011 Author Share Posted February 16, 2011 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.