Welcome Guest, Not a member yet? Register   Sign In
Problem with edit data in db with form
#1

I'm creating an admin cp and at the time I values in db I want to print in a form and then modify them. I used this method to print:

Model
PHP Code:
public function get_info() {
        
$db $this->load->database('default'TRUE);
        
$db    ->select('name, title, description, value')
                        ->
from('setting');
        
$query_get_info $db->get();
                
$query_info $query_get_info->result_array();
                return 
$query_info;
    } 
Controller
PHP Code:
class Setting extends MY_Controller {
    public function 
__construct() {
        
parent::__construct();
        
$this->load->helper(array('form''url'));
        
$this->load->library('form_validation');
        
$this->load->model('acp/settings');
    }
    public function 
edit() {
        
$rules = array(
                array(
                    
'field'=>'email_admin',
                    
'label'=>'Email admin',
                    
'rules'=>'trim|required|valid_email'
                
),

            );
        
$this->form_validation->set_rules($rules);
        if(
$this->form_validation->run() == FALSE) {
            
$data['setting_data'] = $this->settings->get_info();
            
$this->smarty->view('acp/settings.tpl'$data);
        } else {
            
$this->settings->edit_info();
            
$this->smarty->view('acp/settingsSuccess.tpl');
        }
    }


and view:

PHP Code:
{form url="acp/setting/edit" type=""}
            {if 
validation_errors() != ""}<div class="errorMessage">{validation_errors}</div>{/if}
                <
fieldset class="form">
                    {foreach 
$setting_data as $sd}
                        <
dl>
                            <
dt>
                                <
label for="{$sd.title}">{$sd.title}</label>
                            </
dt>
                            <
dd>
                                <
input type="text" name="{$sd.name}value="{$sd.value}"/>
                                <
small>{$sd.description}</small>
                            </
dd>
                        </
dl>
                    {/foreach}
                    <
div class="formSubmit">
                        <
input type="submit" id="SubmitButton" name="submitButton" value="Salva" />
                    </
div>
                </
fieldset>

            {
form

But now I would like to make sure you edit the data, and I'm using something like this

Model

PHP Code:
public function edit_info() {
        
$site_name $this->input->post('site_name');
        
$email_admin $this->input->post('email_admin');
        
$keywords $this->input->post('keywords');
        
$description $this->input->post('description');
        
$publickey $this->input->post('publickey');
        
$privatekey $this->input->post('privatekey');
        
$db $this->load->database('default'TRUE);
        
$info $db->get('setting');

        foreach (
$info->result() as $row) {
            
$name $row->name;
            if (
$name == 'site_name') {
                
$value $row->value;
                if(
$value != $site_name) {
                           ...
  }  
            }
        }

    } 


I realize that having to specify an if for each individual field is very uncomfortable. There is a way to simplify everything as I did for the creation of the form?

Maybe an array? that one side contains the name of the field and on the other the value changed? I do not know, give me a hand? Smile
Reply


Messages In This Thread
Problem with edit data in db with form - by StratoKyke - 06-10-2015, 01:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB