CodeIgniter Forums

Full Version: [Solved] What is the best way to update existing serialized data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have this code that I am working on but for some reason when I want to update my existing serialized data it updates it but does not remove old data.

How can I update existing serialized properly so will remove the previous serialized data.

As you can see in image below because previous data not been removed shows with no width or height

[Image: 3XMSMEvM6hrS.png]


Controller

PHP Code:
<?php

class Slideshow extends MX_Controller {

    public function 
__construct()
    {
        
parent::__construct();
        
$this->lang->load('admin/module/slideshow''english');
        
$this->load->library('form_validation');
        
$this->load->model('admin/setting/admin_model_setting');
    }

    public function 
index()
    {
        if (
$this->input->server('REQUEST_METHOD') == 'POST') {
            if (
$this->admin_model_setting->validate_settingdata('slideshow')) {
                
$this->admin_model_setting->update_setting('slideshow'$this->input->post());
                
redirect(base_url('admin/extension/module'));
            } else {
                
$this->admin_model_setting->insert_setting('slideshow'$this->input->post());
                
redirect(base_url('admin/extension/module'));
            }
        }

        
$data['heading_title'] = $this->lang->line('heading_title');
        
        
$data['text_edit'] = $this->lang->line('text_edit');
        
$data['text_enabled'] = $this->lang->line('text_enabled');
        
$data['text_disabled'] = $this->lang->line('text_disabled');
        
        
$data['entry_banner'] = $this->lang->line('entry_banner');
        
$data['entry_dimension'] = $this->lang->line('entry_dimension');
        
$data['entry_width'] = $this->lang->line('entry_width');
        
$data['entry_height'] = $this->lang->line('entry_height');
        
$data['entry_status'] = $this->lang->line('entry_status');

        
$data['button_save'] = $this->lang->line('button_save');
        
$data['button_cancel'] = $this->lang->line('button_cancel');
        
$data['button_module_add'] = $this->lang->line('button_module_add');
        
$data['button_remove'] = $this->lang->line('button_remove');

        if (
$this->input->post('slideshow_status')) {
            
$data['slideshow_status'] = $this->input->post('slideshow_status');
        } else {
            
$data['slideshow_status'] = $this->site->get('slideshow_status');
        }

        if (
$this->input->post('slideshow_module')) {
            
$modules $this->input->post('slideshow_module');
        } elseif (
$this->site->has('slideshow_module')) {
            
// unserialize string
            
$modules $this->site->get('slideshow_module');
        } else {
            
$modules = array();
        }

        
$data['slideshow_modules'] = array();
        
        if (
$modules) {
            foreach (
$modules as $key => $module) {
                
$data['slideshow_modules'][] = array(
                    
'key' => $key,
                    
'banner_id' => $module['banner_id'],
                    
'width' => $module['width'],
                    
'height' => $module['height']
                );
            }
        }

        
$this->load->model('admin/design/admin_model_banner');

        
$data['banners'] = $this->admin_model_banner->get_banners();

        
$data['header'] = Modules::run('admin/common/header/index');
        
$data['footer'] = Modules::run('admin/common/footer/index');

        
$this->load->view('module/slideshow_view'$data);
    }



Setting Model

PHP Code:
<?php

class Admin_model_setting extends CI_Model {

    public function 
__construct()
    {
        
parent::__construct();
    }

    public function 
validate_settingdata($type) {
        
$this->db->where('type'$type);
        
$query $this->db->get($this->db->dbprefix 'setting');

        if (
$query->num_rows() > 0) {
            return 
TRUE;
        } else {
            return 
FALSE;
        }
    }

    public function 
insert_setting($type$data = array())
    {
        foreach (
$data as $key => $value) {
            
            
// Make sure only keys belonging to this group are used
            
if (substr($key0strlen($type)) == $type) {

                if (!
is_array($value)) {

                    
$insert = array(
                        
'type' => $type,
                        
'item' => $key,
                        
'value' => $value,
                        
'serialized' => '0'
                    
);

                    
$this->db->set($insert);
                    
$this->db->insert('setting');

                } else {

                    
$insert = array(
                        
'type' => $type,
                        
'item' => $key,
                        
'value' => serialize($value),
                        
'serialized' => '1'
                    
);

                    
$this->db->set($insert);
                    
$this->db->insert('setting');
                }
            }

        }        
    }

    public function 
update_setting($type$data = array())
    {
        foreach (
$data as $key => $value) {
            
            if (
substr($key0strlen($type)) == $type) {

                if (!
is_array($value)) {

                    
$update = array(
                        
'type' => $type,
                        
'item' => $key,
                        
'value' => $value,
                        
'serialized' => '0'
                    
);

                    
$this->db->set($update);
                    
$this->db->where('item'$key);
                    
$this->db->where('type'$type);
                    
$this->db->update('setting');

                } else {

                    
$update = array(
                        
'type' => $type,
                        
'item' => $key,
                        
'value' => serialize($value),
                        
'serialized' => '1'
                    
);

                    
$this->db->set($update);
                    
$this->db->where('item'$key);
                    
$this->db->where('type'$type);
                    
$this->db->update('setting');
                }
            }

        }            
    }    

Found the issue I had it like

PHP Code:
 "slideshow_module['" $slideshow_module['key'] . "'][banner_id]" 


PHP Code:
<?php 

$option 
= array();

foreach (
$banners as $key => $banner) { 
    
$option[$banner['banner_id']] = $banner['name'];
}

echo 
form_dropdown("slideshow_module['" $slideshow_module['key'] . "'][banner_id]"$option$slideshow_module['banner_id'], array('class' => 'form-control'));

?>

It should of been 

PHP Code:
"slideshow_module[" $slideshow_module['key'] . "][banner_id]"