I am having trouble with some if statements. To cut a long story short, when a certain statement is true, the code stops and doesn't move on to the next loop/series of if statements.
Here is the first statement that once reached, doesn't move on.
else if ((($pteam_score[$i] == $popposition_score[$i]) && ($pteam_score[$i] != 0) && ($popposition_score[$i] != 0))) {
$team_points[$i]-=2;
$opposition_points[$i]-=2;
$team_win[$i]-=0;
$team_draw[$i]-=1;
$team_loss[$i]-=0;
$team_extra[$i]-=0;
$opp_win[$i]-=0;
$opp_draw[$i]-=1;
$opp_loss[$i]-=0;
$opp_extra[$i]-=0;
$played[$i]-=1;
echo "hey";
$query9=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
And here is the second. Bare in mind there are about 20 of these ifs/else ifs/ and when any of the others are hit, the points are decucted (as they are with the two bad loops also) butit seems it doesn't then move on to a separate section of the code. Perhaps coincidentally, both bad loops involve a draw result!
else if ((($pteam_score[$i] == $popposition_score[$i]) && ($pteam_score[$i] != 0) && ($popposition_score[$i] != 0))) {
$team_points[$i]-=2;
$opposition_points[$i]-=2;
$team_win[$i]-=0;
$team_draw[$i]-=1;
$team_loss[$i]-=0;
$team_extra[$i]-=0;
$opp_win[$i]-=0;
$opp_draw[$i]-=1;
$opp_loss[$i]-=0;
$opp_extra[$i]-=0;
$played[$i]-=1;
echo "what?";
$query9=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
Can anybody see any reason the code would stop dead with the above two statements? The echo stuff is just to see which option is hit. As I said, these two loops are hit at the correct times, but they are the only two that seem to stop the process.
EDIT - A SECTION THAT WORKS FINE
if (($team_score[$i] == $pteam_score[$i]) && ($opposition_score[$i] == $popposition_score[$i])) {
$team_points[$i]+=0;
$opposition_points[$i]+=0;
$team_win[$i]+=0;
$team_draw[$i]+=0;
$team_loss[$i]+=0;
$team_extra[$i]+=0;
$opp_win[$i]+=0;
$opp_draw[$i]+=0;
$opp_loss[$i]+=0;
$opp_extra[$i]+=0;
$played[$i]+=0;
echo "0";
$query11=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
else if (($pteam_score[$i] == 0) && ($popposition_score[$i] == 0)) {
$team_points[$i]+=0;
$opposition_points[$i]+=0;
$team_win[$i]+=0;
$team_draw[$i]+=0;
$team_loss[$i]+=0;
$team_extra[$i]+=0;
$opp_win[$i]+=0;
$opp_draw[$i]+=0;
$opp_loss[$i]+=0;
$opp_extra[$i]+=0;
$played[$i]+=0;
echo "bla";
}
if (($pteam_score[$i] != $team_score[$i]) && ($popposition_score[$i] == $opposition_score[$i])) {
if (($pteam_score[$i] > $popposition_score[$i]) && ($pteam_bonus[$i] > $popposition_score[$i])) {
$team_points[$i]-=3;
$opposition_points[$i]-=0;
$team_win[$i]-=1;
$team_draw[$i]-=0;
$team_loss[$i]-=0;
$team_extra[$i]-=0;
$opp_win[$i]-=0;
$opp_draw[$i]-=0;
$opp_loss[$i]-=1;
$opp_extra[$i]-=0;
$played[$i]-=1;
echo "6";
$query5=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
Thanks in advance
Related
I want to check if the reaction count is 0 then go in the if statement. The part without && ($app->count_reactie($topic['id']) == 0) does work but when I add this it won't work.
In the line above this code I use: implode($app->count_reactie($topic['id']))and it works so it is not that this code doesnt work. This code will return the number of reactions.
But this code down below is what I am trying to get to work.
if(isset($actiefboardid)){
$toppic = $app->get_topics($actiefboardid);
foreach($toppic as $topic){
if(isset($_SESSION['klant_id']) && ($_SESSION['klant_id'] == $topic['klant_id']) && ($app->count_reactie($topic['id']) == 0)){
}}}
Hopefully someone can help me.
Try this:
$count = $app->count_reactie($topic['id']);
if(isset($_SESSION['klant_id']) && ($_SESSION['klant_id'] == $topic['klant_id'])
&& ($count['COUNT(reactie)'] == 0)){
I made the script to do what is expected, so it work ok but there must be a more elegant way to achieve the same result. I know that using switch will make it look nicer but not sure if the result will be the same as the 'default:' behavior:
This is the section of the script i want to refactor:
foreach ($free_slots as $val) { // here i am looping through some time slots
$slot_out = $free_slots[$x][1];
$slot_in = $free_slots[$x][0];
$slot_hours = $slot_out - $slot_in;
// tasks
if ($slot_out != '00:00:00') {
// Here i call a function that do a mysql query and
// return the user active tasks
$result = tasks($deadline,$user);
$row_task = mysql_fetch_array($result);
// HERE IS THE UGLY PART <<<<<----------------
// the array will return a list of tasks where this current
// users involved, in some cases it may show active tasks
// for other users as the same task may be divided between
// users, like i start the task and you continue it, so for
// the records, user 1 and 2 are involved in the same task.
// The elseif conditions are to extract the info related
// to the current $user so if no condition apply i need
// to change function to return only unnasigned tasks.
// so the i need the first section of the elseif with the
// same conditions of the second section, that is where i
// actually take actions, just to be able to change of
// change of function in case no condition apply and insert
// tasks that are unassigned.
if ($row_task['condition1'] == 1 && etc...) {
} else if ($row_task['condition2'] == 1 && etc...) {
} else if ($row_task['condition3'] == 1 && etc...) {
} else if ($row_task['condition4'] == 1 && etc...) {
} else {
// in case no condition found i change function
// and overwrite the variables
$result = tasks($deadline,'');
$row_task = mysql_fetch_array($result);
}
if ($row_task['condition1'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition2'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition3'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition4'] == 1 && etc...) {
} else {
echo 'nothing to insert</br>';
}
}
}
Basically i run the else if block twice just to be able to change of function in case nothing is found in the first loop and be able to allocate records unassigned.
I haven't changed the functionality of your code, but this is definitely a lot cleaner.
The main problem was that your logic for your if/else statements was confused. When you're writing:
if($a == 1){ } else if($b == 1){ } else if($c == 1){ }else{ //do something }
You're saying If a is 1 do nothing, if b is 1 do nothing, if c is 1 do nothing, but if all of those did nothing, do something when you can just say if a is not 1 and b is not 1 and c is not 1, do something.
I wasn't too sure on your second if statements, but generally it's not good to have an if else with no body within it. However, if the "insert into database" comment does the same thing, you can merge the 3 if statements that do the same code.
I hope i've cleared a few things up for you.
Here's what I ended up with:
foreach ($free_slots as $val) { // here i am looping through some time slots
$slot_out = $free_slots[$x][1];
$slot_in = $free_slots[$x][0];
$slot_hours = $slot_out - $slot_in;
// tasks
if ($slot_out != '00:00:00') {
$result = tasks($deadline, $user);
$row_task = mysql_fetch_array($result);
if (!($row_task['condition1'] == 1 || $row_task['condition2'] == 1 || $row_task['condition3'] == 1 || $row_task['condition4'] == 1)) {
$result = tasks($deadline,'');
$row_task = mysql_fetch_array($result);
}
if ($row_task['condition1'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition2'] == 1) {
// insert into database
} else if ($row_task['condition3'] == 1) {
// insert into database
} else if ($row_task['condition4'] == 1) {
} else {
echo 'nothing to insert</br>';
}
}
}
So, I'm currently using a elseif() in PHP but I'm having a problem with the ||.
My code is as follows:
elseif($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('Please enter a valid token!');</script>";
}
The problem is, it does not check either $token_24_check == 0 or $token_48_check == 0 but it checks both of them. So if one of them is false it still returns false.
Does anyone know how to fix this?
UPDATE
Here's the full code from the first line (if()) to the last line (else)
if(empty($token))
{
echo "<script>alert('Please enter a token!');</script>";
}
elseif($user_check != 0)
{
echo '<script>alert("You can only redeem 1 token.\nPlease wait until your other token has expired!");</script>';
}
elseif($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('Please enter a valid token!');</script>";
}
elseif($token_check != 0)
{
echo "<script>alert('Token already used! Please use a new token');</script>";
}
else
echo "<script>alert('Token successfully redeemed!');</script>";
}
As your code is written you are using else if which should be elseif
However...
as the code is written you are making a comparison with else in there try:
if($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('Please enter a valid token!');</script>";
}
try reversing the if/elseif
$token_24_check = 0; $token_48_check = 1; # this
# or this
$token_24_check = 1; $token_48_check = 0; # this fires the ==0
if($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('== 0');</script>";
}
elseif($token_24_check >= 1 || $token_48_check >= 1)
{
echo "<script>alert('>= 1');</script>";
}
Fixed it by using a different tag.
Instead of using || I used && which I should've using in the first place.
Now it checks if both conditions are 0 and will then echo the string.
Thanks everyone for your help and tips. Much appreciated!
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.
What's wrong with this? It's not working out
// define
$prv=0;
if(isset($_GET['prv'])) {
$prv = intval($_GET['prv']);
}
// security
if($prv != 0 OR $prv != 2) {
die("<p>Error</p>");
}
This always goes through the die() part, even when prv is undefined or is defined as 2 in the url (and is 2)
And this does works:
// security
if($prv == 0 OR $prv == 2) { } else {
die("<p>Error</p>");
}
Change your if statement to:
// security
if($prv != 0 AND $prv != 2) {
die("<p>Error</p>");
}
With the OR it will always evaluate as true because when $prv == 0 it does not equal 2. Makes sense?
Whatever the value of "prv" is, it certainly cannot be both 0 and 2 at the same time, so one of the conditions is always true; that's why you get in that clause.
Break it down:
if $prv is not 0, the first condition passes and the OR succeeds
otherwise, $pev must be 0. Therefore it must be "not 2", so the OR succeeds.
Try && instead.
== replace !=
if($prv != 0 AND $prv != 2) {
die("<p>Error</p>");
}