Jump to content

Checkboxes Into Php


miocene
 Share

Recommended Posts

OK following my reasonably successful first php/sql script of a simple comment box I'm trying something a little more tricky.I'm trying to make a site that organises payments between a group of people (e.g. housemates) so that when someone pays for something that is for the group (or some members of the group) they enter it into the form and the script adds it to the database and calculates how much everyone owes/is owed.To get a better idea visit here where the "add payment" form is. (Just for 2 people for the time being)You will see there are checkboxes that can be used to indicate weather or not each person should pay a share of the item.What the code does is work out a "fair share" to be paid by each participating group member based on the number of check boxes ticked. I have achieved this but I can't correspond a checkbox to a group member.fair share = total item price / number of people paying for itI.e in psuedo code:if (person1_checkbox = TRUE) then{amount_to_pay1 = fair_share - amount_already_paid1} //amount to pay by person 1 is what they have paid already - the fair shareelseif (person1_checkbox = FALSE) then{amount_to_pay1 = 0 - amount_already_paid1} //that is - the amount to pay by person 1 is negative so the person is owed moneyHow do I get this in php langauge?---I then enter all the info into an SQL table with the following fields:ID | company | item | amount_payed_by_person | fairshare | amount_to_pay | personSo I would have a row for each person in the group for every payment. e.g:ID | company | item | amount_payed | fairshare | to_pay | person1 | pie shop | pies | 2.00 | 3.00 | 1.00 | jack2 | pie shop | pies | 4.00 | 3.00 | -1.00 | johnOK enough blabber here's my code so far:The form:

<form name="addpayment" action="addpayment.php" method="post">		Company Name: <input name="company" type="text" /> <br />		Item Purchased: <input name="item" type="text" /> <br />		<br />		Harry Paid: £<input name="person1paid" type="text" /> <br />		Ed Paid: £<input name="person2paid" type="text" /> <br />		<br />		Who should pay for this?<br />		<input name="whopays[]" type="checkbox" value="1pays" checked="checked" /> Harry <br />		<input name="whopays[]" type="checkbox" value="2pays" checked="checked" /> Ed <br />		<br />		<input type="submit" value="Submit" />	</form>

And the php:

<?php$whopays = $_POST['whopays'];$no_people = 2;$fsd = $no_people;$person1paid = $_POST['person1paid'];$person2paid = $_POST['person2paid'];$total = $person1paid + $person2paid;for ($i=0; $i<=$no_people - 1; $i++){echo $i . " corresponds to ";if (!$whopays[$i]){$fsd--;}echo $fsd . "<br/>";}$fs = $total / $fsd;echo "<br/>";echo "So the final fs = " . $fs;echo "<br/>";echo $total . " is the amount it cost";$topay1 = $person1paid - $fs;$topay2 = $person2paid - $fs;//INSERTION INTO TABLE CODE$con = mysql_connect("localhost","******","******");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("miocene1_fairshare", $con);$sql="INSERT INTO payments (companyname, item, 1payed, 2payed, fs, topay)VALUES('" . mysql_real_escape_string(htmlspecialchars($company)) . "','" . mysql_real_escape_string(htmlspecialchars($item)) . "','$person1paid','$person2paid', '$fs', '$topay')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }mysql_close($con);/**/}?>

Any suggestions would be great! :) Edit: I did this in MS VBA for excel a while back and ultimately I want a statement looking like this:2q1dnvq.jpg

Edited by miocene
Link to comment
Share on other sites

The main thing to keep in mind with a checkbox is that if the box is not checked, it is not submitted at all. The name/value only gets submitted if the box was checked. If you need a yes/no, where it always gets submitted, use radio buttons or a dropdown instead.That being said, the two ways to get information with a checkbox are in the name and value. You can have a bunch of checkboxes with the same name like you have and different values, and check the array to see which of the boxes were checked, or you can give each checkbox its own name and then see if those were submitted to find out if they were checked. You can use the isset function to check if a variable in $_POST was set. If you need to group a bunch of checkboxes together, you can put the group name as part of either the name or value.

Link to comment
Share on other sites

you can give each checkbox its own name and then see if those were submitted to find out if they were checked.
Thanks for the rapid response!How would I do this without having loads of if statments...?like if ($person1 exists in $checkboxes) then{$person1 pays}if (person2 ....That's what I did in my vba version of it:Sub fairshare(i, a)'Checks who is paying and allocates fair shareIf Sheet1.CheckBox1.Value = True And Sheet1.CheckBox2.Value = True And Sheet1.CheckBox3.Value = True Thenfs = a / 3Sheet1.Cells(i, 7).Value = fsSheet1.Cells(i, 10).Value = fsSheet1.Cells(i, 13).Value = fsElseIf Sheet1.CheckBox1.Value = True And Sheet1.CheckBox2.Value = False And Sheet1.CheckBox3.Value = False Thenfs = aSheet1.Cells(i, 7).Value = fsSheet1.Cells(i, 10).Value = 0Sheet1.Cells(i, 13).Value = 0ElseIf Sheet1.CheckBox1.Value = False And Sheet1.CheckBox2.Value = True And Sheet1.CheckBox3.Value = False Thenfs = aSheet1.Cells(i, 7).Value = 0Sheet1.Cells(i, 10).Value = fsSheet1.Cells(i, 13).Value = 0ElseIf Sheet1.CheckBox1.Value = False And Sheet1.CheckBox2.Value = False And Sheet1.CheckBox3.Value = True Thenfs = aSheet1.Cells(i, 7).Value = 0Sheet1.Cells(i, 10).Value = 0Sheet1.Cells(i, 13).Value = fsElseIf Sheet1.CheckBox1.Value = True And Sheet1.CheckBox2.Value = True And Sheet1.CheckBox3.Value = False Thenfs = a / 2Sheet1.Cells(i, 7).Value = fsSheet1.Cells(i, 10).Value = fsSheet1.Cells(i, 13).Value = 0ElseIf Sheet1.CheckBox1.Value = False And Sheet1.CheckBox2.Value = True And Sheet1.CheckBox3.Value = True Thenfs = a / 2Sheet1.Cells(i, 7).Value = 0Sheet1.Cells(i, 10).Value = fsSheet1.Cells(i, 13).Value = fsElseIf Sheet1.CheckBox1.Value = True And Sheet1.CheckBox2.Value = False And Sheet1.CheckBox3.Value = True Thenfs = a / 2Sheet1.Cells(i, 7).Value = fsSheet1.Cells(i, 10).Value = 0Sheet1.Cells(i, 13).Value = fsElseIf Sheet1.CheckBox1.Value = False And Sheet1.CheckBox2.Value = False And Sheet1.CheckBox3.Value = False ThenMsgBox "Need at least one payer"a = 0EndEnd IfEnd SubAs you can see very inneffecient and not well scalable Edited by miocene
Link to comment
Share on other sites

You can use in_array to check if an item exists in an array:http://www.php.net/manual/en/function.in-array.phpIf you want to check if an array key exists, you can use isset:if (isset($_POST['boxname']))or apparently there's also an array_key_exists function.

Edited by justsomeguy
Link to comment
Share on other sites

You can use in_array to check if an item exists in an array:http://www.php.net/manual/en/function.in-array.phpIf you want to check if an array key exists, you can use isset:if (isset($_POST['boxname']))or apparently there's also an array_key_exists function.
So could I have an array containing the users who have their boxes ticked like:
<?php$users = array("person1", "person2", "person3", "person4");for ($i=1; $i<=$no_of_users; $i++)if (in_array("person2", $users)) {	echo "person2 pays";}

Link to comment
Share on other sites

If you have your checkboxes like this:<input name="whopays[]" type="checkbox" value="1pays" checked="checked" /> Harry <br /><input name="whopays[]" type="checkbox" value="2pays" checked="checked" /> Ed <br />You can do this in PHP:

if (isset($_POST['whopays']))  $whopays = $_POST['whopays'];else  $whopays = array(); // empty if no boxes checkedif (in_array('1pays', $whopays))  echo 'person 1 pays';

Your example will work, it's just always going to find that value in the array, since the array doesn't change.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...