Welcome Guest, Not a member yet? Register   Sign In
Module delete/insert function
#11

[eluser]René Tuin[/eluser]
It is really weird.
I have use all different combination's.

example:

I have 2 checkboxs checked and I want the third one also be checked.
I get the following result out of my arrays.

$diff_module = Array ( )
$new_modules = Array ( [0] => 1 [1] => 2 [2] => 3 )
$old_modules = Array ( [0] => 1 [1] => 2 )

But there is absolute a different between the arrays.

I also try this

example:

None of the checkboxs are checked and I select one of them.
I get the following result out of my arrays.


$diff_module = Array ( )
$new_modules = Array ( [0] => 1 )
$old_modules = Array ( )

I don't get it.
Why?

Greetz
#12

[eluser]TheFuzzy0ne[/eluser]
Try array_diff($new_modules, $old_modules). array_diff only returns the entries that are present in the first array, but not in the second.
#13

[eluser]adamp1[/eluser]
Thats fine, it will return all modules he needs to insert, but I think your going to have to do it the other way as well to get all the modules you need to remove.
#14

[eluser]René Tuin[/eluser]
Now $diff_modules is getting a value, only I get an error in my foreach loop in the else statement

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: cms/module_model.php

Line Number: 78


Code:
foreach($diff_module as $module)
            {
                if(in_array($module,$old_modules))
                {
                    // Module not in new so it needs deleting
                    $this->db->delete('Pages_Module', array('Pages_id_Pages' => $id, 'Module_id_Module' => $module));
                }
                else
                {
                    // Module not in old so needs adding
                    foreach($module as $module_newdata=> $module_id)
                    {
                        $module_data = array(
                                      'Pages_id_Pages'=>$id,
                                      'Module_id_Module'=>$module_id
                                      );
                        
                        $this->db->insert('Pages_Module',$module_data);
                    }                
                }
            }
#15

[eluser]adamp1[/eluser]
Thasts because $module isn't an array. Its an ID
#16

[eluser]TheFuzzy0ne[/eluser]
I agree. You will need to run array_diff twice (once one way, and then again with the parameters reversed), in order to get an array of modules to insert and then another array of modules to remove.
#17

[eluser]adamp1[/eluser]
Code:
$new_modules = array();
$old_modules = array();

$new_modules = // Populate an array with ID's from POST
$old_modules = // Populate an array with ID's from QUEYRY

$insert_modules = array_diff($new_modules, $old_modules);
$delete_modules = array_diff($old_modules, $new_modules);

// Delete & Insert as required
#18

[eluser]René Tuin[/eluser]
Like this.

Code:
// This will return unique modules in either the new/old arrays
        // I.E these modules either need inserting or deleting
        $diff_module = array_diff($new_modules, $old_modules);
        
        if($diff_module != ''){
            foreach($diff_module as $module)
            {
                // Module not in old so needs adding
                $module_data = array(
                                      'Pages_id_Pages'=>$id,
                                      'Module_id_Module'=>$module
                                     );
                        
                $this->db->insert('Pages_Module',$module_data);
            }
        }
        
        $diff_module2 = array_diff($old_modules, $new_modules);
        
        if($diff_module2 != ''){
            foreach($diff_module2 as $module2)
            {
                if(in_array($module2,$old_modules))
                {
                    // Module not in new so it needs deleting
                    $this->db->delete('Pages_Module', array('Pages_id_Pages' => $id, 'Module_id_Module' => $module2));
                }
            }
        }

RIGHT!!

Greetz
#19

[eluser]adamp1[/eluser]
Yes but you won't need the extra if statement checking for in_array inside the foreach loops. Also the if statement checking if $diff_modules/$diff_modules2 == '' isn't need, it won't ever return "", it will return an empty array. But this doesn't need to be caught since the foreach just won't loop over an empty array.

I would also advise some better variable names.
#20

[eluser]René Tuin[/eluser]
Guys, thanks for the help, it finally works how i want it to work.

Greetz




Theme © iAndrew 2016 - Forum software by © MyBB