az_wraith Posted December 17, 2007 Share Posted December 17, 2007 I need some help with the following code below. When it runs it only updates the first if statement but does not update the elseif's in the database. if ($red == 1 & $red_2 == 6 & $one > $two) {$aaa = "1";}elseif ($red == 1 & $red_2 == 10 & $three > $four) {$bbb = "1";}elseif ($red == 1 & $red_2 == 9 & $five > $six) {$ccc = "1";}elseif ($red == 1 & $red_2 == 8 & $seven > $eight) {$ddd = "1";}else {$aaa = "0" & $bbb = "0" & $ccc = "0" & $ddd = "0";} Link to comment Share on other sites More sharing options...
Synook Posted December 17, 2007 Share Posted December 17, 2007 Err... I don't quite understand. What does databasing have to do with that code? Perhaps the complete section posted would help. Link to comment Share on other sites More sharing options...
boen_robot Posted December 17, 2007 Share Posted December 17, 2007 When you use if elseif, only the first section that evaluates to true will be executed. So what exactly is your problem? If you want, you can have nested conditions if that is what you're asking i.e.: if ($red == 1 & $red_2 == 6 & $one > $two) { $aaa = "1"; if ($red == 1 & $red_2 == 10 & $three > $four) { $bbb = "1"; }else { if ($red == 1 & $red_2 == 9 & $five > $six) { $ccc = "1"; }else { if ($red == 1 & $red_2 == 8 & $seven > $eight) {$ddd = "1";} } }}else {$aaa = "0" & $bbb = "0" & $ccc = "0" & $ddd = "0";} but again, that depends a lot on what are you trying to accomplish.[edit]"}" added[/edit] Link to comment Share on other sites More sharing options...
az_wraith Posted December 17, 2007 Author Share Posted December 17, 2007 Ok let try rewording the question. I have a database that has colums aaa, bbb, ccc, ddd. I my php code I have$red$red_2$one$two$three$four$five$six$seven$eightOk now when $red = 1 and $red_2 = 6 then I would like it to add a 1 to the aaa coloum in the database, but also if $red = 1 and $red_2 = 10 then I would like it to add a 1 to bbb coloum in the database and so on. Right now it is adding the 1 to aaa but not bbb, ccc, ddd. I can make an input box and add the 1 to aaa, bbb, ccc, ddd so I know it can be added manualy.By the way boen_robot, you are missing a } in your code that you have. Link to comment Share on other sites More sharing options...
boen_robot Posted December 17, 2007 Share Posted December 17, 2007 I don't see how can $red_2 have more than one single string value at the same time...Hmm... now that I look at it a bit closer, the "and" operator is actually "&&", not "&". So, you need: if ($red == 1 & $red_2 == 6 && $one > $two) {$aaa = "1";}elseif ($red == 1 && $red_2 == 10 && $three > $four) {$bbb = "1";}elseif ($red == 1 && $red_2 == 9 && $five > $six) {$ccc = "1";}elseif ($red == 1 && $red_2 == 8 && $seven > $eight) {$ddd = "1";}else {$aaa = "0";$bbb = "0";$ccc = "0";$ddd = "0";} I think "&" is also a valid operator, but it's just not the one you really want. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 17, 2007 Share Posted December 17, 2007 Don't even use elses at all. Else means that one action isn't going to happen if the action before it happened. If you want each of those actions to be evaluated independently of the others then just remove the else statements and make it a series of if statements. Link to comment Share on other sites More sharing options...
az_wraith Posted December 18, 2007 Author Share Posted December 18, 2007 Don't even use elses at all. Else means that one action isn't going to happen if the action before it happened. If you want each of those actions to be evaluated independently of the others then just remove the else statements and make it a series of if statements.justsomeguy I used what you said and itworks and does not work. If I use the following codeit does not work and only does like before and only updates aaaif ($red == 1 && $red_2 == 6 && $one > $two) {$aaa = "1";}if ($red == 1 && $red_2 == 10 && $three > $four) {$bbb = "1";}if ($red == 1 && $red_2 == 9 && $five > $six) {$ccc = "1";}if ($red == 1 && $red_2 == 8 && $seven > $eight) {$ddd = "1";}else {$aaa = "0";$bbb = "0";$ccc = "0";$ddd = "0";} but if I use the following it updates all but then throws off what $red_2 is. if ($red == 1 && $red_2 = 6 && $one > $two) {$aaa = "1";}if ($red == 1 && $red_2 = 10 && $three > $four) {$bbb = "1";}if ($red == 1 && $red_2 = 9 && $five > $six) {$ccc = "1";}if ($red == 1 && $red_2 = 8 && $seven > $eight) {$ddd = "1";}else {$aaa = "0";$bbb = "0";$ccc = "0";$ddd = "0";} By the way red and red_2 are text boxes. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2007 Share Posted December 18, 2007 You still have an else in there. That means if the last condition fails, all the variables get reset no matter what happened before. Set the variables to their default values first, then change them. $aaa = "0";$bbb = "0";$ccc = "0";$ddd = "0";if ($red == 1 && $red_2 = 6 && $one > $two) {$aaa = "1";}if ($red == 1 && $red_2 = 10 && $three > $four) {$bbb = "1";}if ($red == 1 && $red_2 = 9 && $five > $six) {$ccc = "1";}if ($red == 1 && $red_2 = 8 && $seven > $eight) {$ddd = "1";} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.