• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
PHP Code:
$data = array(
    
'site_name'   $this->input->post('site_name'TRUE),
    
'email_admin' $this->input->post('email_admin'TRUE),
    
'keywords'    $this->input->post('keywords'TRUE),
    
'description' $this->input->post('description'TRUE),
    
'publickey'   $this->input->post('publickey'TRUE),
    
'privatekey'  $this->input->post('privatekey'TRUE),
); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
In this way I can use $data['site_name'] for example to include the $this->input->post('site_name', TRUE). It is true?

But in my table db i have a field that contains the name then site_name etc. and in another the value, that is the content of input. So you'll need both variables so as I say, where is the name site_name go to change the value.
Reply

#4
If I understand what you're trying to do, you could do something like this in your base model:

PHP Code:
<?php

class MY_Model extends CI_Model
{
 
   /** @var string The name of the primary db table used by this model. */
 
   protected $tableName '';

 
   /** @var string|array The primary key of the table. */
 
   protected $key 'id';
 
   
    
// ...

 
   /**
     * Metadata for the model's database fields.
     * @var array
     *
     * This could also be hard-coded to avoid a database call when using 
     * $this->prepData() and/or $this->getFieldInfo().
     *
     * @see http://www.codeigniter.com/user_guide/database/metadata.html#retrieve-field-metadata
     *
     * Each field definition should be an array containing the following keys:
     * 'name' => the name of the field in the database table
     * 'type' => the data type of the field
     * 'default' => (optional) the default value
     * 'max_length' => (optional) the maximum length of the value
     * 'primary_key' => (optional) 1 if the column is a primary key
     */
 
   protected $fieldInfo = array();

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

 
       // ...

 
       if (! empty($this->fieldInfo)) {
 
           foreach ($this->fieldInfo as $key => $field) {
 
               $this->fieldInfo[$key] = (object) $field;
 
           }
 
       }

 
       // ...
 
   }

 
   /**
     * Get the metadata for the model's database fields.
     * 
     * Retrieves the metadata from the database if $this->fieldInfo is empty.
     *
     * @return array The database field metadata for this model.
     */
 
   public function getFieldInfo()
 
   {
 
       if (empty($this->fieldInfo)) {
 
           $this->fieldInfo $this->db->field_data($this->tableName);
 
       }

 
       return $this->fieldInfo;
 
   }

 
   public function prepData($postData)
 
   {
 
       // Populate $skippedFields with any fields which may be auto-populated by
 
       // your model, like created/modified/soft-delete fields.
 
       $skippedFields = array();
 
       // for example:
 
       $skippedFields[] = $this->get_created_field();
 
       $skippedFields[] = $this->get_created_by_field();
 
       $skippedFields[] = $this->get_deleted_field();
 
       $skippedFields[] = $this->get_deleted_by_field();
 
       $skippedFields[] = $this->get_modified_field();
 
       $skippedFields[] = $this->get_modified_by_field();

 
       // $this->key could be an array or a string...
 
       $skippedFields array_merge($skippedFields, (array) $this->key);

 
       $data = array();

 
       // Loop through the table's fields.
 
       foreach ($this->getFieldInfo() as $field) {
 
           // If the field is the primary key, one of the skipped fields, or has 
 
           // not been set in $postData, skip it.
 
           if (isset($field->primary_key) && $field->primary_key
                
|| in_array($field->name$skippedFields)
 
               || ! isset($postData[$field->name])
 
           ) {
 
               continue;
 
           }

 
           // Add the field to the returned $data.
 
           $data[$field->name] = $postData[$field->name];
 
       }

 
       return $data;
 
   }


Then, in your individual models, you would set the properties accordingly, and override the prepData() method (calling the parent's prepData() method) to add any additional prep you need to perform on specific fields. The primary purpose of this is that you can pass the entire output of $this->input->post() into prepData() and it will only return the fields for the table specified in the model's $tableName property (minus any fields you wish to skip).

For example, I might have some special handling in a model like this:


PHP Code:
public function prepData($postData)
{
 
   $data parent::prepData($postData);

 
   // Checkbox.
 
   $data['example_closed'] = empty($data['example_closed']) ? 1;

 
   // Fields which default to null.
 
   $data['example_due_date'      = empty($data['example_due_date']) ? null $data['example_due_date'];
 
   $data['example_billable_hours'] = empty($data['example_billable_hours']) ? null $data['example_billable_hours'];

 
   // Fields which default to empty strings.
 
   $data['example_assigned_to'] = empty($data['example_assigned_to']) ? '' $data['example_assigned_to'];
 
   $data['example_notes'      = empty($data['example_notes']) ? '' $data['example_notes'];

 
   return $data;


Then my controller might do something like this:


PHP Code:
// Assuming the model has a getValidationRules() method which returns an array 
// compatible with the form_validation library's set_rules() method.
$this->form_validation->set_rules($this->example_model->getValidationRules());
if (
$this->form_validation->run() === false) {
 
   return false;
}

// Only pass in the desired fields.
$data $this->example_model->prep_data($this->input->post());

// If the controller needs to do something special with the data, do it here...

// Send the data back to the model for insert/update in the database. 
Reply

#5
Explain better my problem:

I have several fields in the form that already contain within them the value of the corresponding tightening in the db table for example:

In the db I have the name of input and the value.

<input type="text" name="name of value" value="value of the db"/>


This repeated for all values of the table.

Now, what I want to achieve is that changing any of these inputs, or more than one. The value is updated in the table row correct.

What I wonder is there a way to take from the input not only the value but also the name? That way I could make a query with a where and have solved the problem. No?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.