Jump to content

Add counter to if's?


unemployment

Recommended Posts

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);
}

Link to comment
Share on other sites

<?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
}

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.