Jump to content

Archived

This topic is now archived and is closed to further replies.

156418

Multiply some numbers

Recommended Posts

I have the following code, which is posted through from a form on a previous page:

[code]<?php echo $_POST['AdultQuantity']; ?> X <?php echo $_POST['ATicketType']; ?> @ £<?php echo $_POST['APrice']; ?> Each<br>
<?php echo $_POST['ChildQuantity']; ?> X <?php echo $_POST['CTicketType']; ?> @ £<?php echo $_POST['CPrice']; ?> Each<br>
<?php echo $_POST['PresentQuantity']; ?> X <?php echo $_POST['PTicketType']; ?> @ £<?php echo $_POST['PPrice']; ?>[/code]

what I'd like to do is multiple the Quantity by the Price for each of the three, and then total all three together in another part on the page

Is that possible, or will I need to add it into the Database and pull the figures back out?

Share this post


Link to post
Share on other sites
Multiply: [code]$var1 * $var2[/code]

Addition: [code]$var1 + $var2[/code]

Share this post


Link to post
Share on other sites
Thanks, It looks simple enough, but how do i get it to display the output rather than the command


[code]<?php echo $_POST['$AdultQuantity * $APrice']; ?>[/code]

Share this post


Link to post
Share on other sites
[code]<?php echo $_POST['AdultQuantity'] * $_POST['APrice']; ?>[/code]

each $_POST value is its own variable.

Share this post


Link to post
Share on other sites
Ah, I see. Would I be right in assuming that I wont be able to add the three totals together without first posting the results?

Share this post


Link to post
Share on other sites
You could use Javascript to do it instantly on the page as it's entered.

Depending on how you 'define' the $_POST variables on the form you could have it do the calculations on loading the page (which i presume is a shopping basket or something). If you could use the code for the page that posts the data we may be able to give you some code.

Share this post


Link to post
Share on other sites
Hi, its similar to a shopping cart but not quite! It takes a Quantity and simply posts it to the next page, also there's some hidden text boxes which post the amount for each item (I know thats cheating, but I cant find any other way easily!

This is the page prior to that one, which posts the Quantity and price through

[code]<form method="POST" action="Stage6-Final.php">
<table width="70%" border="0">
  <tr>
    <td width="13%" bgcolor="#E6D0DD">Quantity</td>
    <td width="74%" bgcolor="#E6D0DD">Ticket Type </td>
    <td width="13%" bgcolor="#E6D0DD">Price</td>
  </tr>
  <tr>
    <td bgcolor="#E6D0D2"><div align="center">
      <input name="AdultQuantity" type="text" value="0" size="5">
    </div></td>
    <td bgcolor="#E6D0D2">Adult
    <input type="hidden" name="ATicketType" value="Adult" size="13" maxlength="20"></td>
    <td bgcolor="#E6D0D2">&pound;9
      <input type="hidden" name="APrice" value="9" size="10" maxlength="1">
</td>
  </tr>
  <tr>
    <td bgcolor="#E6D0D2"><div align="center">
      <input name="ChildQuantity" type="text" value="0" size="5">
    </div></td>
    <td bgcolor="#E6D0D2">Child
      <input type="hidden" name="CTicketType" value="Child" size="13" maxlength="20">
</td>
    <td bgcolor="#E6D0D2">&pound;6
      <input type="hidden" name="CPrice" value="6" size="10" maxlength="1">
</td>
  </tr>
  <tr>
    <td bgcolor="#E6D0D2"><div align="center">
      <input name="PresentQuantity" type="text" value="0" size="5">
    </div></td>
    <td bgcolor="#E6D0D2">Present
      <input type="hidden" name="PTicketType" value="Present" size="13" maxlength="20">
</td>
    <td bgcolor="#E6D0D2">&pound;3
      <input type="hidden" name="PPrice" value="3" size="10" maxlength="1"></p>
</td>
  </tr>
</table>


</form>[/code]

It also posts some other stuff, user details etc which is being pulled from the Database.

On the page that recieves it, I have the following:

[code]Tickets: </div>            </td>
            <td width="79%"><span class="style11"><?php echo $_POST['AdultQuantity']; ?> X <?php echo $_POST['ATicketType']; ?> @ £<?php echo $_POST['APrice']; ?> Each  <input type="input" name="AT" value="<?php echo $_POST['AdultQuantity'] * $_POST['APrice']; ?>" size="13" maxlength="20"><br>
  <?php echo $_POST['ChildQuantity']; ?> X <?php echo $_POST['CTicketType']; ?> @ £<?php echo $_POST['CPrice']; ?> Each<input type="input" name="CT" value="<?php echo $_POST['ChildQuantity'] * $_POST['CPrice']; ?>" size="13" maxlength="20"><br>
  <?php echo $_POST['PresentQuantity']; ?> X <?php echo $_POST['PTicketType']; ?> @ £<?php echo $_POST['PPrice']; ?> Each<input type="input" name="PT" value="<?php echo $_POST['PresentQuantity'] * $_POST['PPrice']; ?>" size="13" maxlength="20"><br></span></td>
          </tr>
          <tr>
            <td width="21%">
              <div align="right" class="style11 style12 style14">Total to Pay:  </div>            </td>
            <td width="79%"><span class="style11 style12 style14"><?php echo $_POST['AT'] * $_POST['PT']; ?></span></td>
          </tr>[/code]

Thats a cut down of it, the rest is just posting the rest of what was sent from the page before.
There's a lot of multiple posting of the same variables here as well.

No doubt there's a quicker way to do it all, adding and pulling back out from a DB, but I dunno where to begin on making that!

Share this post


Link to post
Share on other sites

×

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.