1

In an edit form I have the possibility to add jobs and to match the reasons for the job (not necessarily). The first thing I do is to retrieve the current state, before the modification, so as to have an array like this from my query:

Array ( 
    [1] => Array ( 
        [0] => 3 
        [1] => 4 
    ) 
    [3] => Array ( 
        [0] => 11 
    ) 
) 

Where the numeric value of the key represents the id of the job and its value in array the id of any reasons. So in the example, work with id = 1 has the reasons with id 3 and 4, work with id = 3 has the reason with id 11 and so on

From the form I get an array with the ids of the jobs posted like this

Array ( 
    [0] => 1 
    [1] => 3 
) 

And for each job an array that contains its reasons like this

ids_reasons_1 = Array ( 
    [0] =>  3 
    [1] => 4 
)

ids_reasons_3 = Array ( [0] => 11 )

Now I should detect any changes: If I have added a job, if I have deleted a job and if I have added or removed some reasons from the pre-existing jobs

The first approach I tried is to use array_key_exists, to determine if a job was pre-existing or if it is new

if(array_key_exists(job_id, array_value_from_database):

If it returns true, it is a job that already exists, otherwise it is a new job, but I don't know how to determine if an existing job has been deleted

Obviously, within the hypothetical foreach I should also check if the reasons for each posted job have changed compared to those previously saved in the database

Could you show me the way to get any changes to what was previously saved both as regards the work and for its reasons

The need is to track who does what during these types of changes

7
  • 1
    "...but I don't know how to determine if an existing job has been deleted" - to determine that, you need to check if any of the IDs from the database are missing in your new array of job IDs. For both that and to check if any reasons have been added/removed, I recommend studying array_diff. Commented Apr 9, 2021 at 13:44
  • array_diff I can use it for the reasons which are two simple arrays and I have to do it in both directions, otherwise I have only one type of information, but for the jobs which are the key of the array taken from the query how can I do to identify what was removed ? Commented Apr 9, 2021 at 13:55
  • You can extract keys with array_keys. Then you can compare the difference between the keys of the database array and values of the new job array, just like you would any other two arrays. Commented Apr 9, 2021 at 13:57
  • 1
    Good idea, now I'll get to work and see if I get out, thank you, in case I'll come back to ask ;) Commented Apr 9, 2021 at 14:04
  • @Robert Say you detected, now what next? Commented Apr 9, 2021 at 14:29

1 Answer 1

1

My basic approach

// Retrieve the previous values ​​from the db (see example array in the first post)
$array_query = $this->get_work_mot_prec($this->input->post('id'));

// Retrieve only the keys representing the job id from the array retrieved from the query
$work_key = array_keys($array_query);

// I get the added jobs
$diff_work_add = array_diff($this->input->post('ids_work'), $work_key);

// I get the removed jobs
$diff_work_del = array_diff($work_key, $this->input->post('ids_work'));

var_dump($diff_work_add);

var_dump($diff_work_del);

In this way I have two arrays that contain the added jobs and the removed jobs and I can save the log events on job changes

Now for the reasons, if a job has been added as new, I don't need to save the log related to the reasons, because it is all new and the job takes everything with it, so I have to detect the changes only for the already existing jobs

// Cycle the job ids sent by the form
foreach($this->input->post('ids_work') as $work):
                    
    // If it's a new job don't check
    if( ! array_key_exists($work, $diff_work_add)):
                        
        echo '<br><br>Motivation for work added: '.$work.'<br>';
        // I collect any additional reasons
        $diff_mot_add = array_diff($this->input->post('ids_mot_'.$work), ( (isset($arr_work_mot_prec[$work])) ? $arr_work_mot_prec[$work] : [] ) );
        var_dump($diff_mot_add);
                        
        echo '<br><br>Motivation for work deleted: '.$work.'<br>';
        // I collect any removed reasons
        $diff_mot_del = array_diff( ( (isset($arr_work_mot_prec[$work])) ? $arr_work_mot_prec[$work] : [] ), $this->input->post('ids_mot_'.$work));
        var_dump($diff_mot_del);
                        
    endif;
                    
endforeach;

At this point I have the array of added jobs, the array of deleted jobs and for each pre-existing job an array for the added reasons and one for the deleted ones

This is currently my basic solution for my needs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.