Sum of table rows will appear in a textfield

Discussion in 'PHP' started by newphpcoder, Dec 13, 2010.

  1. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines
    Good day!
    I have a problem in adding codes in my php codes, but i have the concept of codes that i want to add in my php codes, but I don't know if it is right. Kindly check my codes thank you.
    I attached my whole codes for better understanding
    and here is the codes I want to add:
    Code:
    <?php
      if($_POST["clt_no"])
        {
        $query = "SELECT * FROM clt_transact WHERE  clt_no = '" . $_POST . "'";
        $result_no = mysql_query($query);
            if($result_no)
                {
                if(mysql_num_rows($result_no) > 0 )
                $operation2_input = 0;
                
                $query = "SELECT SUM(t.input_qty) AS operation_input FROM clt_traceability t, clt_transact p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name 'Operation2' AND p.clt_no='" . $_POST["clt_no"] . "'";
                $resultyield = mysql_query($query);
                if($resultyield)
                    {
                        if(mysql_num_rows($resultyield) > 0)$operation2_input =mysql_result($resultyield,0,"operation2_input");
                    }
                  $operation2_total = $operation2_input;
                }
        }
    ?>
    
    I want to happen is all the input qty that the user insert in input qty textfield will sum and the result will appear in operation2_total in input_qty textfield.
    I don't know how thus the result should be appear automatically in the input qty of operation2_total and if my code is correct.
    Thank you
     

    Attached Files:

  2. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines
    Re: Sum of input will appear in a textfield

    I have 5 operation name: operation1, operation2, operation2_total, operation3,operation4,operation5. As you notice I have a operation2_total

    How can I add the input qty of operation2 and the result will be appearing in input_qty of operation2_total.

    Thank you
     

    Attached Files:

  3. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    If you want the sum use the sum sql function

    SELECT SUM(COLUM_NAME_HERE) FROM table_name_here WHERE condition = 'some_condition_here'

    Check this picture example for getting the sum take note I'm using Microsoft SQL Server 2008 not MySQL.
    [​IMG]

    Also in your query your using the super global POST without getting a single value instead you'll get the entire array. You need to say a direct key like
    PHP:
    $_POST['cloth_typ']
    instead of $_POST. Also define variables to hold the contents of the desired POST values and if your using them as an array in the html (<input type="text" name = "inqty[]" />) use foreach instead of for because for is a count loop and associative arrays are slower in process time when you use a for loop. Lastly using * in your query returns all the values only use the values you actually need to decrease database retrieval load(getting the data you want). Also use this

    PHP:
    $myUrlVar $_POST[do];

    if(
    $myUrlVar)
    {

    swicth($myUrlVar)
    {

    case 
    "submit":
    //code to run
    break;
    case 
    "showtotal":
    //code to run
    break;
    default:
    //no value is set trigger error and show default page
    break;

    }


    }
    Also use for the total number or rows in the database

    Code:
    SELECT COUNT(*) FROM table_name_here WHERE condition = 'some_condition_here'
     
    Last edited: Dec 29, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice