0

Hi,
I have 7 variables...each vriable is having either of two values "0" or "1"
e.g. a=0, b=1, c=1, d=1, e=0, f=1, g=0
Now I need the total of all these 7 variables but only which are having value "1" (in the above case b+c+d+f)
My problem that it's dynamic...i.e everytime on submitting the form...these value will be different (0 or 1), how to check everytime???

I hope, I could explain my problem
Please Help.
Thank u.

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by phpHelp
0

You could simply add the values together. You don't need to worry about ignoring the variables where the value is 0 because they wont' affect the total anyway.

1

If you want it to be dynamic, pass all of the form values as a single array and use the array_sum function:

<form method="post" action="sum.php">
    <input type="text" name="sum[a]" />
    <input type="text" name="sum[b]" />
    <input type="text" name="sum[c]" />
    <input type="text" name="sum[d]" />
    <input type="text" name="sum[e]" />
    <input type="text" name="sum[f]" />
    <input type="text" name="sum[g]" />

    <input type="submit" value="Sum" />
</form>



<?php

$values = isset($_POST['sum']) ? $_POST['sum'] : array();
echo array_sum($values);

Edited by blocblue

0

Hi,
Let me explain u...
I have a set questions (7 questions) which are having 4 objective answers each...after submitting the form...i need to check the answers of each...
So I need to check whether at least 3 answers are write or not..If yes then "Nest Level"..and if no then redirect to the previous page...
I am checking all these by firing query for each answer..and the variables are having the value of Number of rows for each query...
How to restrict the checking...like...if only 3 answerd correctly..may b 4...may be all..may 0(zero).....
I am confused...plz suggest....

Thank u

0

Any short cut step for adding some value in php???
Plz help I am in need...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.