fbords Posted July 10, 2007 Share Posted July 10, 2007 Thanks to this forum, I have gotten a little further in my script. I have a multidimensional array populating a drop down box, and I'm pulling variables from that array for my confirmation page. My Array: $Shirts = array ( //Shirt Type => Shirt Price "Cotton T-shirt $7" => "7.00", "Cotton Long Sleeve T-shirt $7" => "7.00" ); $Sizes = array ( //Shirt Size => Add'l Cost "Small (+$0)" => "+0.00", "Medium (+$0)" => "+0.00", "Large (+$0)" => "+0.00", "XL (+$0)" => "+0", "XXL (+$1)" => "+1.00", "XXXL (+$1)" => "+1.00", ); Select Boxes echo '<SELECT name=Shirt1Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'_'.$Shirt_Price.'>'.$Shirt_Type.'</option>'; } echo '</select>'; echo '<SELECT name=Shirt1Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'_'.$Addto_Price.'>'.$Shirt_Size.'</option>'; } echo '</select>'; The First is a size with a base Price (ex. $7.00), depending on the Size, you may have to add an extra dollar. (See Array). To pull from the Arrays on the confirmation page I use the following: list($s1Type,$s1Price) = explode("_",$_POST['Shirt1Type']); list($s2Type,$s2Price) = explode("_",$_POST['Shirt2Type']); list($s3Type,$s3Price) = explode("_",$_POST['Shirt3Type']); list($s4Type,$s4Price) = explode("_",$_POST['Shirt4Type']); list($s5Type,$s5Price) = explode("_",$_POST['Shirt5Type']); list($s6Type,$s6Price) = explode("_",$_POST['Shirt6Type']); list($s7Type,$s7Price) = explode("_",$_POST['Shirt7Type']); list($s8Type,$s8Price) = explode("_",$_POST['Shirt8Type']); list($s1Size,$s1AddPrice) = explode("_",$_POST['Shirt1Size']); list($s2Size,$s2AddPrice) = explode("_",$_POST['Shirt2Size']); list($s3Size,$s3AddPrice) = explode("_",$_POST['Shirt3Size']); list($s4Size,$s4AddPrice) = explode("_",$_POST['Shirt4Size']); list($s5Size,$s5AddPrice) = explode("_",$_POST['Shirt5Size']); list($s6Size,$s6AddPrice) = explode("_",$_POST['Shirt6Size']); list($s7Size,$s7AddPrice) = explode("_",$_POST['Shirt7Size']); list($s8Size,$s8AddPrice) = explode("_",$_POST['Shirt8Size']); Next, I try to create individual totals: $Shirt1Total = (($s1Price * $Shirt1Qty) + ($s1AddPrice * $Shirt1Qty)); $Sub_Total = $Shirt1Total; With a little formatting I get the desired result: echo number_format($Sub_Total,2); However, if I try to add a second total, it gets disregarded: $Shirt1Total = (($s1Price * $Shirt1Qty) + ($s1AddPrice * $Shirt1Qty)); $Shirt2Total = (($s2Price * $Shirt2Qty) + ($s2AddPrice * $Shirt2Qty)); $Sub_Total = $Shirt1Total + $Shirt2Total; I have about 8 individual totals I need to add up with the same "formula" as $Shirt1Total. This is where it stops working. No matter what, it only adds up $Shirt1Total. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 10, 2007 Share Posted July 10, 2007 This is off topc a tad, but to save you some work try this original code <?php list($s1Type,$s1Price) = explode("_",$_POST['Shirt1Type']); list($s2Type,$s2Price) = explode("_",$_POST['Shirt2Type']); list($s3Type,$s3Price) = explode("_",$_POST['Shirt3Type']); list($s4Type,$s4Price) = explode("_",$_POST['Shirt4Type']); list($s5Type,$s5Price) = explode("_",$_POST['Shirt5Type']); list($s6Type,$s6Price) = explode("_",$_POST['Shirt6Type']); list($s7Type,$s7Price) = explode("_",$_POST['Shirt7Type']); list($s8Type,$s8Price) = explode("_",$_POST['Shirt8Type']); list($s1Size,$s1AddPrice) = explode("_",$_POST['Shirt1Size']); list($s2Size,$s2AddPrice) = explode("_",$_POST['Shirt2Size']); list($s3Size,$s3AddPrice) = explode("_",$_POST['Shirt3Size']); list($s4Size,$s4AddPrice) = explode("_",$_POST['Shirt4Size']); list($s5Size,$s5AddPrice) = explode("_",$_POST['Shirt5Size']); list($s6Size,$s6AddPrice) = explode("_",$_POST['Shirt6Size']); list($s7Size,$s7AddPrice) = explode("_",$_POST['Shirt7Size']); list($s8Size,$s8AddPrice) = explode("_",$_POST['Shirt8Size']); ?> try this: <?php $Sizes = array ( //Shirt Size => Add'l Cost "Small (+$0)" => "+0.00", "Medium (+$0)" => "+0.00", "Large (+$0)" => "+0.00", "XL (+$0)" => "+0", "XXL (+$1)" => "+1.00", "XXXL (+$1)" => "+1.00", ); $i = 1; while ($i <= { list($s[$i]['Type',$s[$i]['Price']) = explode("_",$_POST['Shirt.$i.Type']); list($s[$i]['Size'],$s[$i]['AddPrice']) = explode("_",$_POST['Shirt.$i.Size']); $total[$i] = (($s[$i]['Price'] * $S[$i]['Qty']) + ($s[$i]['AddPrice'] * $s[$i]['Qty'])); //Don't know where the Qty is coming from explain it please $i++; } $subtotal = array_sum($total); ?> This method returns it in an array which will make it easier to work with Quote Link to comment Share on other sites More sharing options...
fbords Posted July 10, 2007 Author Share Posted July 10, 2007 The Qty comes from another field in the previous page. I'll post all PHP code for your review. This is without making your original change. Form Page: <?php $Shirts = array ( //Shirt Type => Shirt Price "Cotton T-shirt $7" => "7.00", "Cotton Long Sleeve T-shirt $7" => "7.00" ); $Sizes = array ( //Shirt Size => Add'l Cost "Small (+$0)" => "+0.00", "Medium (+$0)" => "+0.00", "Large (+$0)" => "+0.00", "XL (+$0)" => "+0", "XXL (+$1)" => "+1.00", "XXXL (+$1)" => "+1.00", ); $Colors = array ( "White", "Ash", "Heather Indigo", "Yellow Haze", "Carolina Blue", "Heather Cardinal", "Vegas Gold", "Gold", "Sport Grey", "Lime", "Pistachio", "Natural", "Light Blue", "Sand", "Light Pink ", "Tangerine", "Tan", "Ice Grey", "Stone Blue", "Serene Green", "Azalea", "Daisy", "Iris", "Violet", "Dark Chocolate", "Black", "Indigo Blue", "Cedar", "Cardinal Red", "Forest", "Heliconia", "Jade Dome", "Kelly Green", "Blue Dusk", "Eggplant", "Maroon", "Olive", "Navy", "Orange", "Purple", "Chestnut", "Red", "Prairie Dust", "Royal", "Sapphire", "Charcoal", "Steel Green", "Uniform Navy", "Pine", "Military Green", "Metro Blue", "Texas Orange", "Irish Green", "Oceana", "Dark Heather", "Leaf" ); ?> <table border=0 cellpadding=0 cellspacing=0 width="100%"> <tr style="background: #eee;"> <td align="center"><strong><em>Item</em></strong></td> <td align="center"><strong><em>Size</em></strong></td> <td align="center"><strong><em>Color</em></strong></td> <td align="center"><strong><em>Qty</em></strong></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt1Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'_'.$Shirt_Price.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt1Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'_'.$Addto_Price.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt1Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt1Qty" size="2" maxlength="2"></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt2Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt2Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt2Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt2Qty" size="2" maxlength="2"></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt3Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt3Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt3Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt3Qty" size="2" maxlength="2"></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt4Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt4Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt4Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt4Qty" size="2" maxlength="2"></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt5Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt5Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt5Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt5Qty" size="2" maxlength="2"></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt6Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt6Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt6Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt6Qty" size="2" maxlength="2"></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt7Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt7Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt7Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt7Qty" size="2" maxlength="2"></td> </tr> <tr> <td> <?php echo '<SELECT name=Shirt8Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt8Size> <option selected value="">-----Size-----</option>'; foreach ($Sizes as $Shirt_Size => $Addto_Price) { echo '<OPTION value='.$Shirt_Size.'>'.$Shirt_Size.'</option>'; } echo '</select>'; ?> </td> <td> <?php echo '<SELECT name=Shirt8Color> <option selected value="">---------Color---------</option>'; foreach ($Colors as $Shirt_Color) { echo '<OPTION value='.$Shirt_Color.'>'.$Shirt_Color.'</option>'; } echo '</select>'; ?> </td> <td><input type="text" name="Shirt8Qty" size="2" maxlength="2"></td> </tr> </table> The Confirmation page: <?php $Name = $_POST['Name']; $Department = $_POST['Department']; $Phone = $_POST['PhoneNumber']; $Email = $_POST['EmailAddress']; $Shirt1Type = $_POST['Shirt1Type']; $Shirt1Size = $_POST['Shirt1Size']; $Shirt1Color = $_POST['Shirt1Color']; $Shirt1Qty = $_POST['Shirt1Qty']; $Shirt2Type = $_POST['Shirt2Type']; $Shirt2Size = $_POST['Shirt2Size']; $Shirt2Color = $_POST['Shirt2Color']; $Shirt2Qty = $_POST['Shirt2Qty']; $Shirt3Type = $_POST['Shirt3Type']; $Shirt3Size = $_POST['Shirt3Size']; $Shirt3Color = $_POST['Shirt3Color']; $Shirt3Qty = $_POST['Shirt3Qty']; $Shirt4Type = $_POST['Shirt4Type']; $Shirt4Size = $_POST['Shirt4Size']; $Shirt4Color = $_POST['Shirt4Color']; $Shirt4Qty = $_POST['Shirt4Qty']; $Shirt5Type = $_POST['Shirt5Type']; $Shirt5Size = $_POST['Shirt5Size']; $Shirt5Color = $_POST['Shirt5Color']; $Shirt5Qty = $_POST['Shirt5Qty']; $Shirt6Type = $_POST['Shirt6Type']; $Shirt6Size = $_POST['Shirt6Size']; $Shirt6Color = $_POST['Shirt6Color']; $Shirt6Qty = $_POST['Shirt6Qty']; $Shirt7Type = $_POST['Shirt7Type']; $Shirt7Size = $_POST['Shirt7Size']; $Shirt7Color = $_POST['Shirt7Color']; $Shirt7Qty = $_POST['Shirt7Qty']; $Shirt8Type = $_POST['Shirt8Type']; $Shirt8Size = $_POST['Shirt8Size']; $Shirt8Color = $_POST['Shirt8Color']; $Shirt8Qty = $_POST['Shirt8Qty']; list($s1Type,$s1Price) = explode("_",$_POST['Shirt1Type']); list($s2Type,$s2Price) = explode("_",$_POST['Shirt2Type']); list($s3Type,$s3Price) = explode("_",$_POST['Shirt3Type']); list($s4Type,$s4Price) = explode("_",$_POST['Shirt4Type']); list($s5Type,$s5Price) = explode("_",$_POST['Shirt5Type']); list($s6Type,$s6Price) = explode("_",$_POST['Shirt6Type']); list($s7Type,$s7Price) = explode("_",$_POST['Shirt7Type']); list($s8Type,$s8Price) = explode("_",$_POST['Shirt8Type']); list($s1Size,$s1AddPrice) = explode("_",$_POST['Shirt1Size']); list($s2Size,$s2AddPrice) = explode("_",$_POST['Shirt2Size']); list($s3Size,$s3AddPrice) = explode("_",$_POST['Shirt3Size']); list($s4Size,$s4AddPrice) = explode("_",$_POST['Shirt4Size']); list($s5Size,$s5AddPrice) = explode("_",$_POST['Shirt5Size']); list($s6Size,$s6AddPrice) = explode("_",$_POST['Shirt6Size']); list($s7Size,$s7AddPrice) = explode("_",$_POST['Shirt7Size']); list($s8Size,$s8AddPrice) = explode("_",$_POST['Shirt8Size']); $Shirt1Total = (($s1Price * $Shirt1Qty) + ($s1AddPrice * $Shirt1Qty));// The next 3 lines aren't working properly. Will need to $Shirt2Total = (($s2Price * $Shirt2Qty) + ($s2AddPrice * $Shirt2Qty));// have up to 8 totals to add up using this formula. $Sub_Total = $Shirt1Total + $Shirt2Total; ?> <table border=0 cellpadding=2 cellspacing=2 width="100%" align="center"> <tr style="background: #eee;"> <td align="center" width="50%"><strong><em>Item</em></strong></td> <td align="center" width="20%"><strong><em>Size</em></strong></td> <td align="center" width="20%"><strong><em>Color</em></strong></td> <td align="center" width="10%"><strong><em>Qty</em></strong></td> </tr> <tr> <td align="center"><?php echo $s1Type; ?></td> <td align="center"><?php echo $s1Size; ?></td> <td align="center"><?php echo $Shirt1Color; ?></td> <td align="center"><?php echo $Shirt1Qty; ?></td> </tr> <tr> <td align="center"><?php echo $s2Type; ?></td> <td align="center"><?php echo $s2Size; ?></td> <td align="center"><?php echo $Shirt2Color; ?></td> <td align="center"><?php echo $Shirt2Qty; ?></td> </tr> <tr> <td align="center"><?php echo $s3Type; ?></td> <td align="center"><?php echo $s3Size; ?></td> <td align="center"><?php echo $Shirt3Color; ?></td> <td align="center"><?php echo $Shirt3Qty; ?></td> </tr> <tr> <td align="center"><?php echo $s4Type; ?></td> <td align="center"><?php echo $s4Size; ?></td> <td align="center"><?php echo $Shirt4Color; ?></td> <td align="center"><?php echo $Shirt4Qty; ?></td> </tr> <tr> <td align="center"><?php echo $s5Type; ?></td> <td align="center"><?php echo $$s5Size; ?></td> <td align="center"><?php echo $Shirt5Color; ?></td> <td align="center"><?php echo $Shirt5Qty; ?></td> </tr> <tr> <td align="center"><?php echo $s6Type; ?></td> <td align="center"><?php echo $s6Size; ?></td> <td align="center"><?php echo $Shirt6Color; ?></td> <td align="center"><?php echo $Shirt6Qty; ?></td> </tr> <tr> <td align="center"><?php echo $s7Type; ?></td> <td align="center"><?php echo $s7Size; ?></td> <td align="center"><?php echo $Shirt7Color; ?></td> <td align="center"><?php echo $Shirt7Qty; ?></td> </tr> <tr> <td align="center"><?php echo $s8Type; ?></td> <td align="center"><?php echo $s8Size; ?></td> <td align="center"><?php echo $Shirt8Color; ?></td> <td align="center"><?php echo $Shirt8Qty; ?></td> </tr> <div align="right"> <?php //get current timestamp and subtract one hour $date = time() - 3600; //output time echo date("M/d/Y, g:ia", $date); ?> </div> <tr> <td colspan="4" align="right"><strong><em>Subtotal: </em></strong>$<input type="text" name="Subtotal" size="6" maxlength="5" readonly="" style="text-align: right;" value="<? echo number_format($Sub_Total,2); ?>"></td> </tr> <tr> <td colspan="4" align="right"><strong><em>Tax:</em></strong><input type="text" name="Tax" size="6" maxlength="5" value="7.8%" disabled style="text-align: right;"></td> </tr> <tr> <td colspan="4" valign="top"><hr width="100%" size="1" style="border:1px solid thin black"></td> </tr> <tr> <td colspan="4" align="right"><strong><em>TOTAL:</em> </strong>$<input type="text" name="Total" size="6" maxlength="5" readonly="" style="text-align: right;"></td> </tr> <tr> <td colspan="4" align="center" valign="top"><em><br>Please Click Finish to Submit Your Order, or click Back to make changes.</em><br><br><input type="button" value="<< Back" onclick="history.go(-1);return false;" class="button"> <input type="submit" value="Finish >>" class="button"></td> </tr> </table> Anything you can suggest to clean this mess up will be much appreciated. I'm fairly new and am just "reinventing the wheel" I suppose. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 10, 2007 Share Posted July 10, 2007 Can i see your form in action, secondly the form values are meant for you not the user so make them easy on your self if you want to have dynamic selects with clean looking values and readable (to user) actual text try making a switch for them that will generate a good text string and value string so its easier for you to work with Quote Link to comment Share on other sites More sharing options...
fbords Posted July 10, 2007 Author Share Posted July 10, 2007 sent the link via email, I send you the actual pages if you want as well. Quote Link to comment Share on other sites More sharing options...
fbords Posted July 10, 2007 Author Share Posted July 10, 2007 Sorry, I meant i CAN send you the actual pages if you want. 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.