CodeIgniter Forums
Module delete/insert function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Module delete/insert function (/showthread.php?tid=19966)

Pages: 1 2


Module delete/insert function - El Forum - 06-24-2009

[eluser]René Tuin[/eluser]
Hi all,

I have the following thing.

I have build a kind of cms where the user can make a new page and select which kind of modules he want in that page.

But I have the following, I want to build in a function where the user can change the modules of a specific page. I already have a page where all modules are showing and if they are active they would be checked with a checkbox.

Now I must have a function which delete/insert after saving.
Only if I already selected the module this one must not be deleted.
So i must check which I already have in my db.

Can someone help me with this code.

As attachment I send some screenshot of my modulepage.

Greetz


Module delete/insert function - El Forum - 06-24-2009

[eluser]adamp1[/eluser]
What you could do is when they click save. Before saving and doing anything, get the current list of selected modules from the DB, say put that in a 'old' array. Then using the checkboxes build an array of 'new' modules.

Code:
$old_modules = array('module1','module2');
$new_modules = array('module1','module3');

// This will return unique modules in either the new/old arrays
// I.E these modules either need inserting or deleting
$diff_module = array_diff($old_modules, $new_modules);

foreach($diff_module as $module)
{
  if(in_array($module,$old_modules))
  {
    // Module not in new so it needs deleting
  }
  else
  {
    // Module not in old so needs adding
  }
}



Module delete/insert function - El Forum - 06-24-2009

[eluser]René Tuin[/eluser]
Hi,

I have implemented your code.
But it does nothing.

This is my function:

Code:
function update_module()
    {
        $this->load->database();
        
        $id = $this->input->post('id');
        
        $query = $this->db->query('SELECT Module_id_Module FROM Pages_Module
WHERE Pages_id_Pages = "'.$id.'"');
        
        foreach($query->result() as $row)
        {
            $module_id_old = $row->Module_id_Module;
            $old_modules = array($module_id_old);
        }
        foreach($_POST["module"] as $module_new=> $module_id_new)
        {
            $new_modules = array($module_id_new);
        }
        
        // This will return unique modules in either the new/old arrays
        // I.E these modules either need inserting or deleting
        $diff_module = array_diff($old_modules, $new_modules);
        
        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));
                return $module;
            }
            else
            {
                // Module not in old so needs adding
                $data = array(
                             'Pages_id_Pages' => $id,
                             'Module_id_Module' => $module
                             );
                
                $this->db->insert('Pages_Module', $data);
            }
        }
    }



Module delete/insert function - El Forum - 06-24-2009

[eluser]adamp1[/eluser]
You may want to have a look at this bit
Code:
foreach($query->result() as $row)
        {
            $module_id_old = $row->Module_id_Module;
            $old_modules = array($module_id_old);
        }
        foreach($_POST["module"] as $module_new=> $module_id_new)
        {
            $new_modules = array($module_id_new);
        }

Every time instead of appending the value to the array, you are overwriting it with a new array. Try the following:
Code:
$old_modules = array();
$new_modules = array();
foreach($query->result() as $row)
        {
            $old_modules[] = $row->Module_id_Module;
        }
        foreach($_POST["module"] as $module_new=> $module_id_new)
        {
            $new_modules[] = $module_id_new;
        }



Module delete/insert function - El Forum - 06-24-2009

[eluser]René Tuin[/eluser]
I get your point.

My post variable is already a array, because I say in my textfield name="module[]" so this is already a array.

But how do i set my result from my query in a array??
Can i do it with result_array() ?

Greetz


Module delete/insert function - El Forum - 06-24-2009

[eluser]adamp1[/eluser]
I'v just given you the code above. Your query will return objects, that's fine because all your doing is asking each object for its ID and then putting it in an array.


Module delete/insert function - El Forum - 06-24-2009

[eluser]René Tuin[/eluser]
Hi,

thx for the code.
The delete function works, only my insert doesn't ??
Any clue??

Greetz


Module delete/insert function - El Forum - 06-24-2009

[eluser]adamp1[/eluser]
The code looks fine. Put some logging in or at least output some values. IE, the contents of $new_modules/$old_modules, $diff_modules


Module delete/insert function - El Forum - 06-24-2009

[eluser]René Tuin[/eluser]
If i select one of the checkboxs my $new_module gives me a array back.
The rest is empty.

But if i use this variable, no data is inserted in db.

Greetz


Module delete/insert function - El Forum - 06-24-2009

[eluser]adamp1[/eluser]
well what seems to be happening is if one of the arrays fed into array_dif is empty then it returns an empty result rather than the contents of the other array. I would look into this a bit more, do some testing with what happens to the $dif_modules array when different input arrays are imputed to array_dif.