Okay, so here's what I have.

User makes changes to a form and the data gets sent to a page. This data is an update of data already in a mysql table row.

So, I can make an array of previous data using mysql_fetch_array(), and I have the array of the $_POST data.

What I want to do is compare the data made by mysql_fetch_array() and $_POST to generate an email highlighting changes that were made. The end result would equate to a whole bunch of these, but basically with one loop:

if ($previous_array[$field] !== $_POST[$field]) {
echo "<font color="red">This value changed</font>";
} else {
echo "Original data outputs here";
}

I realize there's probably a bunch of different ways of doing this, but I'm looking for the easiest most concise way of doing it so I can basically move this loop around in other programs for an easy fix.

Thanks for any help with this!

    look at [man]array_diff[/man]. it will return an array of values that are not in the first argument.

      devinemke wrote:

      look at [man]array_diff[/man]. it will return an array of values that are not in the first argument.

      Well, There will be values in both arrays, because the data in this form can't be submitted unless all fields are filled out.

      So when comparing a mysql_fetch_array array and the $_POST array, each array will have the same number of values. What I need is to compare the two arrays and find differing array values.

      like:

      if ($previous_array['name'] !== $_POST['name']) {
      echo "Name has changed to: ".$_POST['name.']."<br>\n";
      } else {
      echo $_POST['name'];
      }
      
        Kinsbane wrote:

        What I need is to compare the two arrays and find differing array values.

        that is precisely what [man]array_diff[/man] does. you have two arrays with identical counts and presumably identical keys. what you wish to know is which values are different. [man]array_diff[/man] will return a new array with the differing values but it preserves the keys. here is an example:

        $array1 = array(
        'first_name' => 'Joe',
        'last_name' => 'Smith',
        'email' => 'joe@smith.com'
        );
        
        $array2 = array(
        'first_name' => 'Joe',
        'last_name' => 'Smith',
        'email' => 'joe@smith.net'
        );
        
        $diff = array_diff($array1, $array2);
        if ($diff)
        {
        	echo 'the following value(s) in array2 differ from array1:<br><br>';
        	foreach ($diff as $key => $value) {echo $key . ' = ' . $value . '<br>';}
        }
        

        as you already said, there are many to do this. you could just do your own foreach loop and compare each value but [man]array_diff[/man] does this for you.

          devinemke wrote:

          that is precisely what [man]array_diff[/man] does. you have two arrays with identical counts and presumably identical keys. what you wish to know is which values are different. [man]array_diff[/man] will return a new array with the differing values but it preserves the keys. here is an example:

          $array1 = array(
          'first_name' => 'Joe',
          'last_name' => 'Smith',
          'email' => 'joe@smith.com'
          );
          
          $array2 = array(
          'first_name' => 'Joe',
          'last_name' => 'Smith',
          'email' => 'joe@smith.net'
          );
          
          $diff = array_diff($array1, $array2);
          if ($diff)
          {
          	echo 'the following value(s) in array2 differ from array1:<br><br>';
          	foreach ($diff as $key => $value) {echo $key . ' = ' . $value . '<br>';}
          }
          

          as you already said, there are many to do this. you could just do your own foreach loop and compare each value but [man]array_diff[/man] does this for you.

          Damn, for some reason i was thinking of using array_diff as checking for presence of keys.. i also found array_diff_assoc = could that be used in this situation as well?

            Kinsbane wrote:

            i also found array_diff_assoc = could that be used in this situation as well?

            in the example i posted above [man]array_diff_assoc[/man] would not be of any use since all of the keys are identical. if you are dealing with two arrays that you know have identical keys then use [man]array_diff[/man]. i am going on the assumption that your form fields share the same names as your database fields.

              devinemke wrote:

              in the example i posted above [man]array_diff_assoc[/man] would not be of any use since all of the keys are identical. if you are dealing with two arrays that you know have identical keys then use [man]array_diff[/man]. i am going on the assumption that your form fields share the same names as your database fields.

              yes, that's correct. i set it up that way so i can just run a foreach loop on $_POST in order to build my query. 🙂

                Write a Reply...