Php : Logical query with some complications - php

I have basic knowledge to write codes in php but I'm stucked in logical process. Hope I can get some help here.
Problem :
I want to compare and declare winner between 2 workers based on performance. Details are given below :
$working_first = 2;
$available_canches = 5;
$target_minutes = 40;
Details of Worker1
$worker1 = 2 ;
$worker1_chances_taken = 4;
$worker1_points = 80;
$worker1_minutes = 40;
Details of Worker2
$worker2 = 5 ;
$worker2_chances_taken = 5;
$worker2_points = 95;
$worker2_minutes = 35;
Expected Solution:
I want to run some process...
if $working_first == $worker1 AND $target_minutes>$worker1_minutes OR $available_chances>$worker1_chances_taken
OR
if $working_first != $worker1 AND $worker1_points>$worker2_points AND $target_minutes>$worker2_minutes OR $available_chances>$worker2_chances_taken
Syntax I'm Using For Output:
if (($working_first==$worker1 && ($target_minutes>$worker1_minutes || $available_chances>$worker1_chances_taken)) || ($working_first != $worker1 && $worker1_points>$worker2_points && ($target_minutes>$worker2_minutes || $available_chances>$worker2_chances_taken)) {
echo 'Process Continues';
}
else {
echo 'Process Ends';
}
I'm unable to get desired output.
Please help me to get it.
Thanks.

You haven't closed the brackets correctly. Try with this:
if (
(
$working_first == $worker1
&&
(
$target_minutes > $worker1_minutes
||
$available_chances > $worker1_chances_taken
)
)
||
(
$working_first != $worker1
&&
$worker1_points > $worker2_points
&&
(
$target_minutes > $worker2_minutes
||
$available_chances > $worker2_chances_taken
)
)
) {
echo 'Process Continues';
} else {
echo 'Process Ends';
}

Related

Hiding output with 0 query value with php [duplicate]

This question already has answers here:
PHP check for not > 0
(3 answers)
Closed 1 year ago.
Can anyone help with this? I have a query that displays a value on the website but I would like to hide the output if it is 0 (zero). There are 2 fields, one called COI and the other AVK. They display as a percent such as COI: 12.00% AVK: 34.56%.
If the value is 0 then they display as COI: 0.00% and/or AVK 0.00%. I would like to not show this at all.
The code is specific and I can't find examples that would apply to this situation. Thought maybe an an else if but I just can't quite figure it out.
Appreciate any help!
UPDATE: Found a solution & have edited the code to show what was added. Hopefully this will help someone else if they have a similar issue.
echo "<span class='subjectdetails'>";
$nIndex = 11;
$nMarkIndex = -1;
foreach ($aExtraFields as $extraField)
{
$extraField = trim($extraField);
$subField = trim($details[$nIndex]);
if (($extraField == 'COI') and ($subField > 0)) <----- added this
$subField = sprintf("%.2f%%", $subField * 100);
if (($extraField == 'AVK') and ($subField > 0)) <----- added this
$subField = sprintf("%.2f%%", $subField * 100);
if (!empty($subField) && $extraField[0] != '_')// Don't print extra fields if they begin with underscore
if ((!empty($subField) && $extraField[0] != 'C') || $extraField == 'COI')
if ((!empty($subField) && $extraField[0] != 'A') || $extraField == 'AVK')
if (!empty($subField) && $extraField[0] != 'M')// Don't print extra fields if they begin with M
echo "$extraField: $subField<br/>" ;
if ($extraField == '_Marks')
$nMarkIndex = $nIndex;
$nIndex++;
}
You can just check $subfield with another if. If it's 0 then set $subfield to '';
if ($extraField == 'COI') {
if($subField) {
$subField = sprintf("%.2f%%", $subField * 100);
} else {
$subField = '';
}
}

Why is my IF statement not working to define membership level?

I am very new to PHP and trying to get an IF statement to work to change the membership level based on the possibility of multiple answers provided in the registration form. However, it's not working. If anyone can help me understand what I am doing wrong/missing that would be a great help. Here is my code. Thank you.
function my_pmpro_checkout_level($level)
{
if ( ! empty( $_REQUEST['registrationlevel']) && $level->id == '1' ) {
if (( $_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['experience'] != 'morethansixyears') || ($_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['timeassesment'] == 'no')) {
$level->id = '3';
} elseif ( $_REQUEST['registrationlevel'] == 'eligibleforprovisional' && $_REQUEST['experience'] != 'noexperience') {
$level->id = '3'}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");

Multiple checkbox validation before sql insert

I have a problem similar to the post:
how to check multiple $_POST variable for existence using isset()?
My issue is, I have a rather large list of checkboxes I need to know if they were checked in order to set the status of $row. it would be exactly like the mentioned script,all and's, except I have and OR thrown in to the works.
if ( isset($_POST['Facial1'])
&& ($_POST['Facial2'])
&& ($_POST['Facial3'])
&& ($_POST['Facial4'])
&& ($_POST['Intra-Oral1'])
&& ($_POST['Intra-Oral2'])
&& ($_POST['Intra-Oral3'])
&& ($_POST['Intra-Oral4'])
&& ($_POST['Intra-Oral5'])
&& ($_POST['Intra-Oral6'])
&& (
(
($_POST['X-Ray1'])
&& ($_POST['X-Ray3'])
)
|| ($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
Each time I run a test, as soon as it gets to an element that is not checked it throws an error: undefined index: xxx. Where xxx is the first none checked item.
Checkboxes that are not checked do not get sent as part of the POST body. Your code needs to use isset:
if ( isset($_POST['Facial1'])
&& isset($_POST['Facial2'])
&& isset($_POST['Facial3'])
&& isset($_POST['Facial4'])
&& isset($_POST['Intra-Oral1'])
&& isset($_POST['Intra-Oral2'])
&& isset($_POST['Intra-Oral3'])
&& isset($_POST['Intra-Oral4'])
&& isset($_POST['Intra-Oral5'])
&& isset($_POST['Intra-Oral6'])
&& (
(
isset($_POST['X-Ray1'])
&& isset($_POST['X-Ray3'])
)
|| isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
the isset() allows you to check one variable isset($var) or multiple variables at a time isset($var,$var1,$var2) this will return true is all exists and false if one or more are not set.
Your code is only checking one and then doing well I am not sure what
Try
if ( isset(
$_POST['Facial1'], $_POST['Facial2'], $_POST['Facial3'],
$_POST['Facial4'], $_POST['Intra-Oral1'], $_POST['Intra-Oral2']
$_POST['Intra-Oral3'], $_POST['Intra-Oral4'],
$_POST['Intra-Oral5'], $_POST['Intra-Oral6']
)
&&
(
isset($_POST['X-Ray1'], $_POST['X-Ray3'])
||
isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
I hope this is what you intended, its not totally obvious from your code.

Yahtzee Php Code 3 of a Kind

if($_SESSION['valueofdie1'] != 0 && $_SESSION['valueofdie2'] != 0 && $_SESSION['valueofdie3'] != 0 && $_SESSION['valueofdie4'] != 0 && $_SESSION['valueofdie5'] != 0)
{
if((($_SESSION['valueofdie1'] == $_SESSION['valueofdie2']) && ($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']||$_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5']))
|| (($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie2'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5']))
|| (($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5'])))
{
if($_POST['choose'] == 'choose 3oaK')
{
$_SESSION['g'] = 5;
$_SESSION['scoretkind'] = $_SESSION['valueofdie1'] + $_SESSION['valueofdie2'] + $_SESSION['valueofdie3'] + $_SESSION['valueofdie4'] + $_SESSION['valueofdie5'];
unset($_SESSION['3oaKBut']);
echo '<input type="hidden" name="choose" value="Clear" onLoad="form.submit();">';
$_POST['sub'] = 'reset';
$_POST['choose'] = '';
}
if(empty($_SESSION['g']))
{
$_SESSION['3oaKBut'] = '<input type="submit" name="choose" value="choose 3oaK">';
echo $_SESSION['3oaKBut'];
}
}
}
if($_SESSION['g'] == 5)
{
echo $_SESSION['scoretkind'];
}
So here is the code we have. We are trying to check if 3 of the 5 die values are equal. If they are equal we echo out a button that allows the user to choose to score his 3 of a kind, which is the total of all of the dice. Everything works except in some cases the 3 of a kind button would echo out when there isnt a 3 of a kind. Halp PLS
I'm sorry I didn't answer your question by actually solving your bug, but I think your code is hard to read and your approach makes it cumbersome to program all the rules.
General advice: Put $_SESSION['valueofdie1'] and the other dice into an array of values. That's much easier to work with. After that, it should be pretty easy to check how many times each value occurs. Even when you keep your approach, you could make variables like $die1, which is already a lot shorter and more readable than $_SESSION['valueofdie1'].
But with an array, you could roughly start like this:
// Put all dice into an array.
$dice = array(
$_SESSION['valueofdie1'],
$_SESSION['valueofdie2'],
etc.... );
// Count how many times each die is rolled.
$diceCount = array();
foreach($dice as $die) {
$count = 0;
if (isset($diceCount[$die])) {
$count = $diceCount[$die];
}
$diceCount[$die] = $count + 1;
}
// Check possible results simply by looking at those counts.
// If one die value is rolled 5 times, it's Yahtzee...
if (array_search(5, $diceCount) !== false) {
echo 'Yahtzee!';
}
if (array_search(4, $diceCount) !== false) {
echo 'Four of a kind';
}
// Full house takes two types.
if (array_search(3, $diceCount) !== false && array_search(2, $diceCount) !== false) {
echo 'Full house';
}
for ($diceCount as $die => $count) {
echo "$count times $die";
}
... etc ...
You'll need to expand this list, and take some other rules into account. After all, a Yahtzee could also count as a Four of a Kind. But by checking all those rules, you can generate a new array of possible combinations, which you can check against the previously chosen options. And the outcome of that determines which options the player can choose.

php syntax for if statement with multiple conditions ( `||` as well as `&&` )

Context: On our website, we calculate whether an item/order meets the criteria for free shipping using an if statement evaluating if a value - 'ff' - is true. Depending on conditions met it sets the shipping appropriately. We now require the need to evaluate if an item is free shipping with the order of _. To top it off, for other purposes such as external feeds, another condition must apply (weight must = 0 or have no value), for fw to be free freight. Here is the code, that I can't figure out why it is NOT working:
if($r['freeFreight'] == 'ff' || ($r['freeFreight'] == 'fw' && ($r['weight'] == 0 || $r['weight'] == '') ) ) {
$r['shippingStandard'] = 0;
}
Are the conditions overly segregated in the if statement with the sets of ()? Or, are they just incorrectly placed. For example, should it be:
if(($r['freeFreight'] == 'ff') || ($r['freeFreight'] == 'fw' && $r['weight'] == 0 || $r['weight'] == '') ) {
$r['shippingStandard'] = 0;
}
The second of the two seems more logical to me because: if ($r['freeFreight'] == 'ff') - then regardless of the following conditions the statement should return true and set the variable to 0. Is my logic correct? Sorry for this newb question...
Thanks for any help you can offer.
So I think perhaps, based on the answers so far (thanks everybody that has chimed in to help an amateur) - I think I am going to do a trial with:
if( ($r['freeFreight'] == 'ff') || ( $r['freeFreight'] == 'fw' ) && empty( $r['weight'] ) ) {
$r['shippingStandard'] = 0;
}
Planning to run trial with this, if it is fundamentally wrong, please advise.
Try this out?
if( $r['freeFreight'] == 'ff' || ( $r[ 'freeFreight' ] == 'fw' && empty( $r['weight'] ) ) ) {
$r['shippingStandard'] = 0;
}
using empty might be a little cleaner too
if you really want to break it out more, you could do this
if( $r[ 'freeFreight' ] == 'ff' ) {
$r[ 'shippingStandard' ] = 0;
} elseif( $r[ 'freeFreight' ] == 'fw' && empty( $r[ 'weight' ] ) ) {
$r[ 'shippingStandard' ] = 0;
} else {
// Everything else
}
If you want to shorten the variables, and will be using them later:
$freight = $r[ 'freeFreight' ];
$weight = isset( $r[ 'weight' ] ) ? $r[ 'weight' ] : null;
$shipping = $r[ 'shippingStandard' ]; // Or whatever you want the default value to be...
if( $freight == 'ff' || ( $freight == 'fw' && empty( $weight ) ) ) {
$shipping = 0;
}
// ... Later down the file
$r = array(
'freeFreight' => $freight,
'weight' => $weight,
'shippingStandard' => $shipping
);
I really cant tell how the rest of the file looks, like if this is in a function to get shipping, you could simply just return $shipping. Hope this helps. You should be able to move the concepts around to get what you want
your first option should should be identical to the following:
if ($r['freeFreight'] == 'ff') {
$r['shippingStandard'] = 0;
}
elseif ($r['freeFreight'] == 'fw') {
// the dual check for an empty value here is kind of redundant and unneccessary.
// I'd probably just use "if ($r['weight'])" and be done with it
if ($r['weight'] == 0) {
$r['shippingStandard'] = 0;
}
elseif ($r['weight'] == '') {
$r['shippingStandard'] = 0;
}
else {
// $r['shippingStandard'] stays whatever it was before
}
}
else {
// $r['shippingStandard'] stays whatever it was before
}
if that logic looks right to you, then you should be right on and I'd print_r($r) to make sure it's holding what you expect it to be holding. Your parenthesis in the first example is exactly how I would do it.

Categories