• PHP Help PHP Newbies
  • Scripting a MySQL Database to calculate the price of several option values/drop downs

How would one script a MySQL database to determine the total amount based on the selections of the several drop down lists/option values in a form?

[option value1 price][v]
+
[option value2 price][v]
+
[option value3 price][v]
+
[option value4 price][v]
+
[option value5 price][v]

(etc.)

= Total Price

And just for craps and giggles, how would I add any shipping and/or tax charges to the total price? Let's say the tax price is 5%, but that number can change. Please help. I need to get the business up and running as soon as possible.

    If they're all integers being returned to PHP, you can actually use simple arithmetic operators:

    $POST['option1']+$POST['option2']...

    ~Brett

      Ok, so, now I know how to add the drop down menu values, but, I still need to know how to call values pertaining to the different drop down menus from the MySQL database(s).

        Elaborate on what you're trying to do. Are you trying to retrieve the values as passed from the form to PHP, or are you trying to dynamically create the drop-downs from a query in a mySQL database?

        ~Brett

          I have been, and am trying to do both. Any help would be appreciated.

            Here's what I use as my dynamic select box generation:

            <?php
            
            $query = "SELECT * FROM `table_name` WHERE something = '$something'";
            // Note, it's a pseudo query
            $result = mysql_query($query);
            
            echo '<select name="myselect">
            while($row = mysql_fetch_array($result))
            {
                echo '<option value="'.$row['column_name'].'">'.$row['column2_name'].'</option>';
            }
            echo '</select>';
            
            ?>

            As for reading them after the form is submitted (using the above example):

            <?php
            // We'll grab the selected options value:
            $selected = $_POST['myselect'];
            echo $selected;
            ?>

            That will grab the value, and echo it to the browser.

            ~Brett

              Thanks, again, I'll mess around with it and see if I can get it to work for me.

                Originally posted by bpat1434
                If they're all integers being returned to PHP, you can actually use simple arithmetic operators:

                $POST['option1']+$POST['option2']...

                ~Brett

                I am using the value tags of the option values in my form to add up the total price at the end of the form. Can you please tell me how I can display the total price in a text box in the form with the click of a button, or dynamically, as the user is changing their options in the drop down menus?

                  That is called JavaScript. Google it. I'm not a javascript programmer. Sorry I can't help you further.

                  ~Brett

                    Write a Reply...