I am building a page which allows our members to select their notification preferences based on a number of options. For example sake, I am giving the option for the member to select notifications when a new message arrives and when an update has occured. They can receive the notification via email, sms, both, or neither.
If I simply build it out as a number of:
HTML code
<tr>
<td>Alert me when a new message comes in:</td>
</tr>
<tr>
<td>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="EMAIL" <?php if ($smscode == "7" || $smscode == "4") { ?>checked="checked"<?php } ?> tabindex="15" />Email</label>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="SMS" <?php if ($smscode == "7" || $smscode == "5") { ?>checked="checked"<?php } ?> />SMS</label>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="BOTH" <?php if ($smscode == "7" || $smscode == "6") { ?>checked="checked"<?php } ?> tabindex="15" />Both</label>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="NONE" <?php if ($smscode == "0") { ?>checked="checked"<?php } ?> />Don't notify me</label>
</td>
</tr>
<tr>
<td>Alert me when a new update to my site occurs:</td>
</tr>
<tr>
<td>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="EMAIL" <?php if ($smscode == "7" || $smscode == "1") { ?>checked="checked"<?php } ?> tabindex="15" />Email</label>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="SMS" <?php if ($smscode == "7" || $smscode == "2") { ?>checked="checked"<?php } ?> /> SMS</label>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="BOTH" <?php if ($smscode == "7" || $smscode == "3") { ?>checked="checked"<?php } ?> tabindex="15" />Both</label>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="NONE" <?php if ($smscode == "0") { ?>checked="checked"<?php } ?> />Don't notify me</label>
</td>
</tr>
Variable Encoding and Storage
<?php
if ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 15;
} elseif ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "SMS") {
$notif = 14;
} elseif ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 13;
} elseif ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "NONE") {
$notif = 12;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 11;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "SMS") {
$notif = 10;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 9;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "NONE") {
$notif = 8;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 7;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "SMS") {
$notif = 6;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 5;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "NONE") {
$notif = 4;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 3;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "SMS") {
$notif = 2;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 1;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "NONE") {
$notif = 0;
}
?>
I am left to code for 16 possible variables (and thus creating over 100 lines of code). Can anybody think of a better way to consolidate this code? Based on the selections made, I want the result to equal a single digit (i.e. 28 equals, send email and SMS notifications for both new messages and updates).
Creating a new table or database and making reference calls is not a solution so please do not suggest that.
Thank you!
This is an example on how NOT knowing C puts you against a wall when developing simple things. As far as I can see, the simplest option is the best, just use binary!:
define('SEND_EMAIL',1);
define('SEND_SMS',2);
/* The values are packed together, low bits represent 'update' options,
* high bits represent 'message' options
* You can save up to 4 variants (ON/OFF) with 0xf
*/
$options = ((intval($_POST['message']) & 0xf) << 4) | (intval($_POST['update']));
...
// Retrieve options from, say, stored option
$message = ($options >> 4) & 0xf;
$update = $options & 0xf;
/* For readability, this can be a function */
if ($message == (SEND_SMS|SEND_EMAIL)) {
$message = 'Both';
}
else if ($message == SEND_SMS) {
$message = 'SMS';
}
else if ($message == SEND_EMAIL) {
$message = 'Email';
}
It sounds like what you're really looking for is a bitwise solution. Using bits, you're able to store a lot of boolean switches into a single integer. This answer uses some roundabouts to keep things clear - you could use the int values directly instead of the pow(2,X) shown below... consider it "teaching a man to fish".
If you'd like a more succint, though complex to understand solution, take a look at Ast Derek's answer. They both do the same thing and operate on the same principle.
In order to store these, let's do two simple switches:
switch($_GET['x']) {
case 'Email': $x = pow(2,0); break; // 1
case 'Sms': $x = pow(2,1); break; // 2
case 'Both': $x = pow(2,0) + pow(2,1); break;// 3
default: $x = 0;
}
switch($_GET['y']) {
case 'Email': $y = pow(2,2); echo "Y Email"; break; // 4
case 'Sms': $y = pow(2,3); echo "Y SMS"; break; // 8
case 'Both': $y = pow(2,2) + pow(2,3); echo "Y Both"; break; // 12
default: $y = 0;
}
As you can see, the None options are absent. None is simply the absence of a either Email or SMS. Also, the Both option is defined not as a separate option, but as a combination of both.
Now that we have these value, we can combine these two numbers into a single number, since their relevant bits are both in different ranges.
$z = $x | $y;
What happens when looking at the bits is the following - assume that we've got X = Email, and Y = Both.
x = 0001 -> (0 + 0 + 0 + 1) -> 1
y = 1100 -> (8 + 4 + 0 + 0) -> 12
-----
OR: 1101 -> (8 + 4 + 0 + 1) -> 13
What this will give you is the following possible results:
0: x = none, y = none
1: x = email, y = none
2: x = sms, y = none
3: x = both, y = none
4: x = none, y = email
5: x = email, y = email
6: x = sms, y = email
7: x = both, y = email
8: x = none, y = sms
9: x = email, y = sms
10: x = sms, y = sms
11: x = both, y = sms
12: x = none, y = both
13: x = email, y = both
14: x = sms, y = both
15: x = both, y = both
To detect which have chosen, simply reverse the operation.
So you can test things, I've put the whole setup in a Github Gist for you to enjoy and tinker with: http://gist.github.com/505272
Feel free to ask if you need clarification; I'm not sure I explained it very clearly :/
Related
I'm using PHP's preg_match to help determine the value of a string.
But the code only ever prints 1 or 2.
Why aren't the last two cases of my if statement ever matched?
$atype = strtolower($userData['user_type']); // let say data is :: company introducer
if ($atype == "individual introducer" || $atype == "individualintroducer" ||
(preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {
$atype = 1 ;
} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||
(preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {
$atype = 2;
} elseif ($atype == "company introducer" || $atype == "companyintroducer" ||
(preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){
$atype = 3;
} elseif ($atype == "company investor" || $atype == "companyinvestor" ||
(preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){
$atype = 4;
}
echo $atype;
You need to explain your question in a better way.
But i guess as you say the data assumed is company introducer.
So it already matches condition for the first if block.
For ex:
In company introducer
The preg_match will return true.
if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ){
$atype =1 ;
}
I have a problem with my if and else statement in PHP where it never runs the else statement.
The input form is in HTML:
<input type="radio" name="marital_stat" id="single" value="single" />Single
<input type="radio" name="marital_stat" id="married" value="married" />Married
<input name="age" type="text" size="5" maxlength="3" placeholder="30" required/>
<input name="work" type="radio" id="employee" value="employee" />Employee
<input name="work" type="radio" id="own" value="own" />
Own Business
<input name="work" type="radio" id="jobless" value="jobless" />Jobless
<input name="place" type="radio" id="urban" value="urban" />Urban
<input name="place" type="radio" id="rural" value="rural" />Rural</td>
Here is the PHP Code:
if ($marital_stat == 'married')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'jobless')
{
if ($place == 'rural') { $loan_credibility == 5; }
}
}
}
else if ($marital_stat == 'single')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'employee')
{
if ($place == 'rural') { $loan_credibility == 1; }
}
}
}
Here is a condition that will display some output:
$A = 'positive';
$B = 'negative';
if ($loan_credibility == 5 ){
echo $B ;}
else{
echo $A;
}
As I see you make $loan_credibility == 5; or 1; the == only used in equality statement it checks the two sides if they equal and you have to use = to set value, not == so it will be $loan_credibility = 5; or 1;
You have no else clause (which logically is necessary when using else if)...
if (condition) {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Do any of the following (#1 or #2 make the most sense)...
Make your else if into a separate if block
Change else if to else
Or add an else clause
Also, as #Mohamed Belal pointed out, when setting variables use = not ==.
So two things to fix your problem: 1) if-else if-else logic and 2) = vs ==...
if ($marital_stat == 'married')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'jobless')
{
if ($place == 'rural') { $loan_credibility = 5; }
}
}
}
if ($marital_stat == 'single')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'employee')
{
if ($place == 'rural') { $loan_credibility = 1; }
}
}
}
My charity does "Poker Runs" motorcycle runs where player go to each stop and pick a card. We are looking to make this an easier way to track the winners without having to manually sort through each card.
I believe I have all other functions done, I just am unsure how to check for the full house with the method I am using. And also how to score just for a high card hand.
<?php
$card_one_suit = $_POST['card_one_suit'];
$card_two_suit = $_POST['card_two_suit'];
$card_three_suit = $_POST['card_three_suit'];
$card_four_suit = $_POST['card_four_suit'];
$card_five_suit = $_POST['card_five_suit'];
$card_one = $_POST['card_one'];
$card_two = $_POST['card_two'];
$card_three = $_POST['card_three'];
$card_four = $_POST['card_four'];
$card_five = $_POST['card_five'];
$player = $_POST['name'];
$total_card_amount = $card_one + $card_two + $card_three + $card_four + $card_five;
$card_list = array();
array_push($card_list, $card_one, $card_two, $card_three, $card_four, $card_five);
$card_suits = array();
array_push($card_suits, $card_one_suit, $card_two_suit, $card_three_suit, $card_four_suit, $card_five_suit);
$rank = 0;
foreach($card_list as $card)
{
$count_values[$card]++;
}
foreach($card_suits as $cards)
{
$count_suit_values[$cards]++;
}
//echo "$count_suit_values[$card_one_suit]";
//print_r($count_suit_values);
// ROYAL FLUSH
if(($total_card_amount == "60") && ($count_suit_values[$card_one_suit] == 5))
{
$rank = 1;
echo "ROYAL FLUSH";
}
// STRAIGHT FLUSH
else if (($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40") &&
($count_suit_values[$card_one_suit] == 5))
{
$rank = 2;
echo "STRAIGHT FLUSH";
}
// FOUR OF A KIND
else if ($count_values[$card_one] == 4 || $count_values[$card_two] == 4 || $count_values[$card_three] == 4)
{
$rank = 3;
echo "FOUR OF A KIND";
}
// FULL HOUSE
// HOW TO FIGURE THIS OUT?
// FLUSH
else if ($count_suit_values[$card_one_suit] == 5 || $count_suit_values[$card_two_suit] == 5 || $count_suit_values[$card_three_suit] == 5)
{
$rank = 5;
echo "FLUSH";
}
// STRAIGHT
else if ($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40")
{
$rank = 6;
echo "STRAIGHT";
}
// THREE OF A KIND
else if ($count_values[$card_one] == 3 || $count_values[$card_two] == 3 || $count_values[$card_three] == 3 || $count_values[$card_four] == 3)
{
$rank = 7;
echo "THREE OF A KIND";
}
// TWO PAIR
else if ($count_values[$card_one] == 2 && $count_values[$card_two] == 2 || $count_values[$card_one] == 2 && $count_values[$card_three] == 2
|| $count_values[$card_one] == 2 && $count_values[$card_five] == 2 || $count_values[$card_two] == 2 && $count_values[$card_three] == 2)
{
$rank = 8;
echo "TWO PAIR";
}
// ONE PAIR
else if ($count_values[$card_one] == 2 || $count_values[$card_two] == 2 || $count_values[$card_three] == 2 || $count_values[$card_four] == 2)
{
$rank = 9;
echo "ONE PAIR";
}
// HIGH CARD
else
{
$rank = 10;
echo "NO MATCHES. DETERMINE HIGH CARD";
}
?>
For the hand to be a full house, the array count_values must contain two entries, one of those entries must have a value of 3.
else if (count($count_values) == 2 && (array_values($count_values)[0] == 3 || array_values($count_values)[1] == 3)) {
echo 'FULL HOUSE';
}
I am stuck on some php coding, that seemed initially easy to accomplish. Here is what i would like to do :
<?php
$amountOfDigits = 1;
$numbers = range(1,3);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
while ( have_posts() ) : the_post();
static $count = 0;
if ($digits == '1') {
//Do this if statement
if ($count == "2") }
elseif ($digits == '2') {
//Do this if statement
if ($count == "2" || $count == "3") }
elseif ($digits == '3') {
//Do this if statement
if ($count == "2" || $count == "3" || $count == "4") }
{ //here the rest of the code
?>
So depending on the $digits variable, the if statement is formatted to be used on the line above //the rest of the code
How to put this in PHP properly?
Thanks.
Robbert
If I understand your question properly, you want something like this:
if ($digits == '1')
$mycond = ($count == "2");
elseif ($digits == '2')
$mycond = ($count == "2" || $count == "3")
elseif ($digits == '3')
$mycond = ($count == "2" || $count == "3" || $count == "4")
then you can further use
if($mycond){
// blahblahblah
}
Well if you want the same execution block on each case, there is a very simple solution for that.
You should simply use a function that check status of "count" depending on "digits".
<?php
function checkCountAgainstDigits($count, $digits){
switch($digits){
case 1:
return $count === 1;
case 2:
return $count === 2 || $count === 3;
case 3:
return $count === 2 || $count === 3 || $count === 4;
default:
// The default case "ELSE"
return FALSE;
}
}
if(checkCountAgainstDigits($count, $digits)){
// do
}
?>
If you want a different one, your solution is correct.
I am trying to perform some calculations with a form but every time i try to work with checkboxes it goes wrong.
The checkboxes are beign set on value 1 in the form itselff and are being checked if there checked or not.
$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0;
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0;
When i try to do calculations every works expect for the options with the checkboxes.
When both checkboxes (telefoon & netwerk) are selected the value should be 30.
If only one is selected the value should be 20.
But no mather what i have tried to write down it always give problem, and it always uses 20, never the value 30.
How do i solve this problem? Or suppose i am writing the syntax all wrong to lay conditions to a calculation? Any input appreciated.
$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];
if ($oppervlakte <= 10)
$tarief = 100;
if ($oppervlakte > 10 && $oppervlakte <= 20)
$tarief = 90;
if ($oppervlakte > 20)
$tarief = 80;
if($verdieping == 1)
{
$prijsVerdieping = $oppervlakte * 120;
}
else
{
$prijsVerdieping = 0;
}
if(($telefoon == 1) && ($netwerk == 1))
{
$prijsCom = 30; // never get this value, it always uses 20
}
if(($telefoon == 1) || ($netwerk == 1))
{
$prijsCom = 20;
}
$prijsOpp = $tarief * $oppervlakte; // works
$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom; //prijsCom value is always wrong
Regards.
EDIT: full code below in 2 php files
<?php
if (!empty($_POST))
{
$standnaam = $_POST["standnaam"];
$oppervlakte = $_POST["oppervlakte"];
//value in the form van checkboxes op 1 zetten!
$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0; //if checkbox checked value 1 anders 0
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0;
if (is_numeric($oppervlakte))
{
$_SESSION["standnaam"]=$standnaam;
$_SESSION["oppervlakte"]=$oppervlakte;
$_SESSION["verdieping"]=$verdieping;
$_SESSION["telefoon"]=$telefoon;
$_SESSION["netwerk"]=$netwerk;
header("Location:ExpoOverzicht.php"); //verzenden naar ExpoOverzicht.php
}
else
{
echo "<h1>Foute gegevens, Opnieuw invullen a.u.b</h1>";
}
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
<tr>
<td>Standnaam:</td>
<td><input type="text" name="standnaam" size="18"/></td>
</tr>
<tr>
<td>Oppervlakte (in m^2):</td>
<td><input type="text" name="oppervlakte" size="6"/></td>
</tr>
<tr>
<td>Verdieping:</td>
<td><input type="checkbox" name="verdieping" value="1"/></td>
<!--value op 1 zetten voor checkbox! indien checked is value 1 -->
</tr>
<tr>
<td>Telefoon:</td>
<td><input type="checkbox" name="telefoon" value="1"/></td>
</tr>
<tr>
<td>Netwerk:</td>
<td><input type="checkbox" name="netwerk" value="1"/></td>
</tr>
<tr>
<td><input type="submit" name="verzenden" value="Verzenden"/></td>
</tr>
</table>
2nd page with calculations:
<?php
$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];
if ($oppervlakte <= 10)
$tarief = 100;
if ($oppervlakte > 10 && $oppervlakte <= 20)
$tarief = 90;
if ($oppervlakte > 20)
$tarief = 80;
if($verdieping == 1)
{
$prijsVerdieping = $oppervlakte * 120;
}
else
{
$prijsVerdieping = 0;
}
if(($telefoon == 1) && ($netwerk == 1))
{
$prijsCom = 30;
}
if(($telefoon == 1) || ($netwerk == 1))
{
$prijsCom = 20;
}
$prijsOpp = $tarief * $oppervlakte; // werkt
$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom;
echo "<table class=\"tableExpo\">";
echo "<th>Standnaam</th>";
echo "<th>Oppervlakte</th>";
echo "<th>Verdieping</th>";
echo "<th>Telefoon</th>";
echo "<th>Netwerk</th>";
echo "<th>Totale prijs</th>";
echo "<tr>";
echo "<td>$standnaam</td>";
echo "<td>$oppervlakte</td>";
echo "<td>$verdieping</td>";
echo "<td>$telefoon</td>";
echo "<td>$netwerk</td>";
echo "<td>$totalePrijs</td>";
echo "</tr>";
echo "</table>";
?>
Terug naar het formulier
</body>
</html>
One problem I've noticed are these lines,
if(($telefoon == 1) && ($netwerk == 1)) {
$prijsCom = 30; // will get set to 30.
}
if(($telefoon == 1) || ($netwerk == 1)) {
$prijsCom = 20; // will now be set to value of 20.
}
Here is why, if $telefoon and $netwerk are both 1, $prijsCom is set to the value of 30. It leaves that if block and goes down onto the next one, i.e.
if(($telefoon == 1) || ($netwerk == 1)) {
$prijsCom = 20;
}
It will evaluate to true since $telefoon == 1 evaluates to true and will override the value of $prijsCom to be 20.
Depending on how the code will be used, as a possible work-around, you could add the || condition first, so the value is set to 20 whether $telefoon or $netwerk is set to 1 and then check to see if they both are 1.
Update:
When looking at your code, I notice you are using $_SESSION variables, but you have not called session_start() at the beginning of the file,
<?php
session_start(); // <--you need to call this first
$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
...
This may or may not be where your other problem lies, but whenever you use $_SESSION you need to call session_start first.
Call session_start(); after <?php.