www.webdeveloper.com
Results 1 to 3 of 3

Thread: Running Subtotals and Grand Total

  1. #1
    Join Date
    Jul 2012
    Posts
    3

    Running Subtotals and Grand Total

    I am trying to generate a layout based on data in a table called MM. The data in the MM table looks like this:


    ID... datem ..............item1 ....item2........item3....i1price....i2price....i3price
    1.....2012-07-16.......Cookies......HotDog......Pizza.....1.00......2.00......2.50
    2.....2012-07-17.......Soup.........Salad.........Candy.....1.00.....2.00......0.50

    i1price is the price of item1.
    i2price is the price of item2
    i3price is the price of item3.

    I need to create a form where the user can buy each of the items, for each day.
    The user can input how many units of item1 can be purchased, how many of item2 and how many of item3.

    The form has a tabular structure to make it easy for data entry.
    datem ..............item1 .....acc1.....item2.....acc2...item3.....acc3.....total
    2012-07-16.......Cookies...____....HotDog...____....Pizza....____.....
    2012-07-17.......Soup.......____....Salad......____....Candy...____.....

    For each row displayed, after the user types in the quantities the total should be calculated based on the price of the item,when the Apply button is pressed.
    For example:

    datem ...........item1 .....acc1.....item2.....acc2....item3.....acc3.....total..button
    2012-07-16....Cookies..._2__....HotDog...__1_....Pizza...._1_...._6.50_..Apply
    2012-07-17....Soup......._2__....Salad......__1_....Candy..._2_...._5.00_...Apply

    The value 6.50 is created by:
    2*(price of cookies i.e. 1.00)+1*(price of HotDog i.e 2.00) + 1* (price of Pizza ie. 2.50) = 6.50

    To get this i have written the following code :
    <? ob_start(); ?>
    <?php
    include_once '../../core/init.php';

    $sql="SELECT ID, datem, item1,item1price, item2, item2price, item3,item3price from mm order by datem";
    $result= mysql_query($sql);
    if (!$result)
    {
    echo 'Error fetching items'.mysql_error();
    }
    else
    {
    while ($row = mysql_fetch_array($result))
    {
    $menus[] = array('ID' => $row['ID'],
    'datem' => $row['datem'],
    'item1' => $row['item1'],
    'i1price'=> $row['i1price'],
    'item2' => $row['item2'],
    'i2price'=> $row['i2price'],
    'item3' => $row['item3'],
    'i3price'=> $row['i3price'],

    );
    }

    }

    if (isset($_POST['dailybuy']) and $_POST['dailybuy'] == 'Apply')
    {
    how do i generate the totals when the apply button is selected for each row???
    }

    ?>
    <? ob_flush(); ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Hot Lunch - School Administration</title>
    <link rel="stylesheet" href="../../css/primary.css">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>

    <body>
    <form id="poster" action="" method="POST" style="display: none;"></form>
    <table class="stable" width="900px">
    <thead>
    <tr class="th"><td>Id</td><td>Date</td><td>Daily Menu Items </td><td></td><td></td><td></td><td></td><td></td><td></td><td>Total</td></tr>
    </thead>

    <tbody>
    <?php foreach ($menus as $menu): ?>
    <tr>
    <td><?php echo $menu['ID']; ?></td>
    <td><?php echo $menu['datem']; ?></td>

    <td><?php echo $menu['item1'].'-'.$menu['i1price']; ?> </td>
    <td> <input type="text" name="q1" size="2" value="<?php if (isset($_POST['q1'])) echo $_POST['q1'];?>"></td>
    <td><?php echo $menu['item2'].'-'.$menu['i2price']; ?></td>
    <td><input type="text" name="q2" size="2" value="<?php if (isset($_POST['q2'])) echo $_POST['q2'];?>"></td>
    <td><?php echo $menu['item3'].'-'.$menu['i3price']; ?></td>
    <td><input type="text" name="q3" size="2" value="<?php if (isset($_POST['q3'])) echo $_POST['q3'];?>"></td>

    <td><input type="text" name="subtotal"
    value="<?php echo $_POST['q1'] * $menu['item1price'] +
    $_POST['q2'] * $menu['item2price'] +
    $_POST['q3'] * $menu['item3price']; ?>" >
    </td>
    <td>
    <input type="submit" name="dailybuy" value="Apply" onclick="submitForm(this);" ></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    </body>
    </html>

    =================
    Can someone please give me some pointers in how I can obtain the desired output. Your time and help is greatly appreciated.

  2. #2
    Join Date
    Jun 2012
    Location
    FL, USA
    Posts
    48
    I took a brief look at this.

    Basically you need javascript if you want the totals to calculate realtime. PHP won't do it realtime

  3. #3
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    The totaling can all be done in your MySQL query.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles