Add value of mutiple fields

Discussion in 'PHP' started by Fracisc, Aug 22, 2013.

  1. #1
    I have:
    <input type="text" name="qty'.$i.'">

    I have a few instances of this field. How can I add the value of all of them?
     
    Fracisc, Aug 22, 2013 IP
  2. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #2
    <?php
        if (isset($_POST['submit'])){
            $qtyLength = 0 ;
            $qtySum = 0 ;
            //calculate number of input elements with qty in their name
            foreach ($_POST as $key => $post) {
                if (stristr($key, 'qty')){
                    $qtyLength++ ;
                }
            }
            //Calculate sum
            for($i  = 1; $i <= $qtyLength ; $i++ ){
                $qtySum += $_POST['qty'.$i] ;
            }
            echo 'Sum is ' .$qtySum ;
        }
    
    
    ?>
    
    
    PHP:
    
    <form method="post" action="">
        <input type="text" name="qty1" />
        <input type="text" name="qty2" />
        <input type="text" name="qty3" />
        <input type="submit" value="submit" name="submit">
    </form>
    HTML:
    Ofcourse validation is missing , but it works
    HTH :)
     
    kutchbhi, Aug 23, 2013 IP
    Fracisc likes this.
  3. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #3
    Thank you!
     
    Fracisc, Aug 23, 2013 IP
  4. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #4
    I would like to add that a few things are missing from the above code. Like
    the check if $_POST['qty'.$i] isset before attempting to read it. etc.
     
    kutchbhi, Aug 23, 2013 IP
  5. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #5
    Yup, I saw that. :)
     
    Fracisc, Aug 23, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    I would suggest changing this:
    <input type="text" name="qty'.$i.'">

    To this:
    <input type="text" name="qty['.$i.']">

    Then you could just do
    $total = array_sum($_POST['qty']);

    Since $_POST['qty'] would then be an ARRAY.

    So to rework kutchbhi's example:
    <form method="post" action="">
    	<input type="text" name="qty[0]" />
    	<input type="text" name="qty[1]" />
    	<input type="text" name="qty[2]" />
    	<input type="submit" value="submit" name="submit">
    </form>
    Code (markup):
    <?php
    if (
    	isset($_POST['qty']) &&
    	is_array($_POST['qty'])
    ) {
    	echo 'Sum is ',array_sum($_POST['qty']),'<br />';
    } else {
    	echo 'Unable to create sum, [QTY] either nonexistant or not an array!<br />';
    }
    ?>
    Code (markup):
    A lot simpler, no? One of the big tricks to getting really good at PHP is to realize that in a LOT of cases there already are functions or object methods of handling most anything you can think of... That's why PHP is best used as glue between markup, it's function library, and external engines like SQL -- it blows chunks as a general computing language, so don't "Brute force code" anything you don't have to.

    Though as kutchbhi said, you might want to do some validation first... though I added some simple/primitive checking.
     
    Last edited: Aug 23, 2013
    deathshadow, Aug 23, 2013 IP
  7. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #7
    Thanks deathbyshadow ! I Didn't know one could simply create an array like that. nice.
     
    kutchbhi, Aug 25, 2013 IP