SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to change a number based on what someone chooses?

    Hi Everyone,

    In the below URL if a person chooses "whatever" or "whatever2", I would like to get the price to adjust upwards or downwards based on what they choose. My syntax is below. I'm getting a silly error but I can't seem to fix what I'm doing wrong. Any help would be great.

    Thanks!

    http://whatsmyowncarworth.com/auto-m.../math/math.php


    PHP Code:
    <?php

    //Connect to the database through our include 
    include "connect_to_mysql.php";
      
    // Query member data from the database and ready it for display
    $sql mysql_query("SELECT price FROM car_data2");
    while(
    $row mysql_fetch_array($sql)){

    $price $row["price"];

    }

    ?>

    <?php
      error_reporting
    (E_ALL E_STRICT);
      
    ini_set("display_errors"1);
    ?>

    <?php

    $priceMods 
    = array ('whatever' => 100'whatever2' => -100);

    if (isset (
    $priceMods[$_POST['choose']]) {

        
    $price += $priceMods[$_POST['choose']];
        
    }

    ?>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000">
    <?php echo "$price"?>
    <form action="math.php" method="post" enctype="multipart/form-data" name="form" id="form" onSubmit="return validate_form ( );">
      <table>
        <tr> 
          <td>Choose:</td>
          <td> 
            <select name="choose">
              <option value="whatever">Whatever</option>
              <option value="whatever2">Whatever2</option>
            </select>
          </td>
        </tr>
        <tr> 
          <td width="99"> 
            <input type="submit" name="submit" value="Contact Us">
          </td>
        </tr>
      </table>
    </form>
    </body>
    </html>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Check all opening and closing brackets in this line:
    PHP Code:
    if (isset ($priceMods[$_POST['choose']]) { 
    By the way, you should this on top of your script, not in the middle:
    PHP Code:
    <?php
      error_reporting
    (E_ALL E_STRICT);
      
    ini_set("display_errors"1);
    ?>
    And the result of this loop will be only the last price:
    PHP Code:
    while($row mysql_fetch_array($sql)){

    $price $row["price"];


    If you want all prices, you should use $price[] so all prices will be stored in an array.

    And a last advice: since the mysql_ database extension is becoming deprecated, you might want to take a look at mysqli or pdo

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your information. It's working great!

    I just have one minor question. The original price is 7895.00. If you click on "contact us" and choose the "whatever" options the prices (depending on what option you choose) will decrease. My question is, is their anyway to get back to the original price of 7895.00? How can they choose that?

    Thanks again for everyone's help. Much appreciated.

    PHP Code:
    <?php
     
    /* error_reporting(E_ALL | E_STRICT);
      ini_set("display_errors", 1); */
    ?>


    <?php

    //Connect to the database through our include 
    include "connect_to_mysql.php";
      
    // Query member data from the database and ready it for display
    $sql mysql_query("SELECT price FROM car_data2");
    while(
    $row mysql_fetch_array($sql)){

    $price $row["price"];

    }

    ?>


    <?php

    $priceMods 
    = array ('whatever' => -100'whatever2' => -200'whatever3' => -300'whatever4' => -400);

    if (isset (
    $priceMods[$_POST['choose']])) {
        
        
    $price += $priceMods[$_POST['choose']];
        
    }

    ?>

    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000">
    <?php echo "$price"?>
    <form action="math.php" method="post" enctype="multipart/form-data" name="form" id="form" onSubmit="return validate_form ( );">
      <table>
        <tr> 
          <td>Choose:</td>
          <td> 
            <select name="choose">
              <option value="whatever">Whatever</option>
              <option value="whatever2">Whatever2</option>
              <option value="whatever3">Whatever3</option>
              <option value="whatever4">Whatever4</option>
            </select>
          </td>
        </tr>
        <tr> 
          <td width="99"> 
            <input type="submit" name="submit" value="Contact Us">
          </td>
        </tr>
      </table>
    </form>
    </body>
    </html>

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Add an option with discount 0 ?

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guido2004. The code is working great. Appreciate the help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •