unemployment Posted May 1, 2011 Share Posted May 1, 2011 How can I say, if more than one of these if's happens do this.... if ((!isset($_POST['state'])) && (($_POST['country'] != $r['country']) || ($_POST['city'] != $r['city']))) { $details = $_POST['city'].', '.$_POST['country']; update_user_actions(1, $details); } else if ((isset($_POST['state'], $_POST['city'])) && (($_POST['state'] != $r['state']) || ($_POST['city'] != $r['city']))) { $details = $_POST['city'].', '.$_POST['state']; update_user_actions(1, $details); } if ((isset($_POST['credentials'])) && ($_POST['credentials'] != $r['credentials'])) { $details = ''; update_user_actions(2, $details); } if ((isset($_POST['specialties'])) && ($_POST['specialties'] != $r['specialties'])) { $details = ''; update_user_actions(3, $details); } if ((isset($_POST['personalweb'])) && ($_POST['personalweb'] != $r['personalweb'])) { $details = $_POST['personalweb']; update_user_actions(4, $details); } Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/ Share on other sites More sharing options...
teddyb Posted May 1, 2011 Share Posted May 1, 2011 <?php $counter = 0; if ((!isset($_POST['state'])) && (($_POST['country'] != $r['country']) || ($_POST['city'] != $r['city']))) { $details = $_POST['city'].', '.$_POST['country']; update_user_actions(1, $details); $counter++; } else if ((isset($_POST['state'], $_POST['city'])) && (($_POST['state'] != $r['state']) || ($_POST['city'] != $r['city']))) { $details = $_POST['city'].', '.$_POST['state']; update_user_actions(1, $details); $counter++; } if ((isset($_POST['credentials'])) && ($_POST['credentials'] != $r['credentials'])) { $details = ''; update_user_actions(2, $details); $counter++; } if ((isset($_POST['specialties'])) && ($_POST['specialties'] != $r['specialties'])) { $details = ''; update_user_actions(3, $details); $counter++; } if ((isset($_POST['personalweb'])) && ($_POST['personalweb'] != $r['personalweb'])) { $details = $_POST['personalweb']; update_user_actions(4, $details); $counter++; } if ($counter >= 2) //ie more than one { //do something } Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208888 Share on other sites More sharing options...
unemployment Posted May 1, 2011 Author Share Posted May 1, 2011 ok that works, but I forgot to say that if the counter is > 1 then stop running the previous ifs and only run the new one. Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208890 Share on other sites More sharing options...
gizmola Posted May 1, 2011 Share Posted May 1, 2011 ok that works, but I forgot to say that if the counter is > 1 then stop running the previous ifs and only run the new one. In other words, you would like to have spaghetti code. I'm not sure what you are aiming for or why, but I can tell you without a doubt that the approach you are taking is the wrong one. Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208893 Share on other sites More sharing options...
unemployment Posted May 1, 2011 Author Share Posted May 1, 2011 All I want to do is if more then two data sets are posted... or maybe better said, if more than two ifs successfully run then post something else instead of running the two ifs. If not just post the regular if. Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208894 Share on other sites More sharing options...
Fadion Posted May 1, 2011 Share Posted May 1, 2011 If you're less than 80 years old, there's no excuse to open a thread, forget it and after a few days open an identical one. Anyways, your approach is quite nonsense. If more then two if() run successfully, post the regular if()? Who's the regular if()? Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208897 Share on other sites More sharing options...
unemployment Posted May 1, 2011 Author Share Posted May 1, 2011 If you're less than 80 years old, there's no excuse to open a thread, forget it and after a few days open an identical one. Anyways, your approach is quite nonsense. If more then two if() run successfully, post the regular if()? Who's the regular if()? For a little back story what I am trying to do in context outside of the code is...I have a news feed and if someone update their location it will say, location updated. If someone updates their date of birth it will say, date of birth updated, but if they update both things at the same time I only want it to say, profile updated. Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208898 Share on other sites More sharing options...
teddyb Posted May 1, 2011 Share Posted May 1, 2011 what does update_user_actions(); do? does it echo what the users just updated or does it just make db modifications? Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208907 Share on other sites More sharing options...
unemployment Posted May 1, 2011 Author Share Posted May 1, 2011 what does update_user_actions(); do? does it echo what the users just updated or does it just make db modifications? It just makes database modifications. Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1208992 Share on other sites More sharing options...
teddyb Posted May 1, 2011 Share Posted May 1, 2011 You could do this <?php $output = array(); if ((!isset($_POST['state'])) && (($_POST['country'] != $r['country']) || ($_POST['city'] != $r['city']))) { $details = $_POST['city'].', '.$_POST['country']; update_user_actions(1, $details); $output[] = "City and Country Changed"; } else if ((isset($_POST['state'], $_POST['city'])) && (($_POST['state'] != $r['state']) || ($_POST['city'] != $r['city']))) { $details = $_POST['city'].', '.$_POST['state']; update_user_actions(1, $details); $output[] = "City and State Changed"; } if ((isset($_POST['credentials'])) && ($_POST['credentials'] != $r['credentials'])) { $details = ''; update_user_actions(2, $details); $output[] = "Credentials Changed"; } if ((isset($_POST['specialties'])) && ($_POST['specialties'] != $r['specialties'])) { $details = ''; update_user_actions(3, $details); $counter++; $output[] = "Specialties Changed"; } if ((isset($_POST['personalweb'])) && ($_POST['personalweb'] != $r['personalweb'])) { $details = $_POST['personalweb']; update_user_actions(4, $details); $counter++; $output[] = "Personal Web Changed"; } if (empty($output)) { echo "No modifications made"; } elseif(count($output) == 1) { echo $output[0]; } else { echo "Profile Changed"; } But its not the best way of doing it, but it does mean you dont have to restructure your entire script Quote Link to comment https://forums.phpfreaks.com/topic/235239-add-counter-to-ifs/#findComment-1209008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.