Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-15-2009, 04:17 AM   PM User | #1
Hayyel
New Coder

 
Join Date: Jan 2009
Posts: 91
Thanks: 17
Thanked 1 Time in 1 Post
Hayyel is an unknown quantity at this point
Compare arrays in MySQL using php?

I have an array I am given on a irregular basis, say 3 - 5 times a week, and it is uploaded to a website via a form and inserted into a MySql table.

I am needing to be able to compare the array I am given today to the table in MySQL and output the newest array and the difference on the same webpage.

Example:

I upload an array of current employees on Mon.
On Wed. I am given the newest array to update the db with but I need to be able to distinguish which employees are in the db and not the newest array.. ie they were fired or quit. On Thurs. I need to do this again.

The unique key is the Employee ID number so I am using REPLACE INTO in my php code.

How can I accomplish this? I was thinking of a flag of some sort... but since it is a irregular update occurrence I cannot think of a way.
Hayyel is offline   Reply With Quote
Old 03-16-2009, 07:47 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
How about fetching the data from the table into an array, then comparing that array to the new array to create your report. Then proceed onto the table update using the new array as it's always been done. There's an army of array functions in PHP to make it easy to compare two arrays.

http://us.php.net/manual/en/function...diff-assoc.php
__________________
Fumigator is offline   Reply With Quote
Old 03-16-2009, 01:24 PM   PM User | #3
Hayyel
New Coder

 
Join Date: Jan 2009
Posts: 91
Thanks: 17
Thanked 1 Time in 1 Post
Hayyel is an unknown quantity at this point
I was thinking about this however how do I query the db so array is in the same form? The one being submitted is 2 dimensional.

Submitted array:

PHP Code:
Array (
            [
1] => Array
        (
                              [
note] => 
                              [
careerID] => 20
                              
[lastLogin] => 1231125665
                              
[bearerStatus] => 0
                              
[lastLoginNumberHour24] => 22
                              
[dept] => 0
                              
[lastLoginNumberDay] => 4
                              
[statusNumber] => 1
                              
[lastLoginNumberMonth] => 1
                              
[lastLoginNumberYear] => 2009
                              
[memberID] => 28178

         

When i query it I havent found anything on how to setup the numerical key for the outter array.

Last edited by Hayyel; 03-16-2009 at 02:38 PM..
Hayyel is offline   Reply With Quote
Old 03-16-2009, 04:17 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You'll have to get the two arrays to look the same somehow. Either convert the new array to use your unique primary key for the array index, or something.

If that's too much of a pain, another method that would work is fetch through the existing table and perform an array search for each fetch. You'll need to keep track of array elements that you do find (probably by adding an index that indicates it's been found) so you can go back through the array and determine the ones that are new and should be inserted.
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:17 PM.


Advertisement
Log in to turn off these ads.