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.

Recommended Answers

All 4 Replies

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.

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" />


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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.