views:

622

answers:

3

value of FORM INPUT Help!!

//this is just a refrence of $nm and $fid from test_model//

  $data['fid']['value'] = 0;
  $data['nm'] = array('name'=>'fname',
                      'id'=>'id');

say i have one form_view with

<?=form_label('Insert Your Name :')?>
<?=form_input($nm)?>

and a function to get single row

 function get($id){
    $query = $this->db->getwhere('test',array('id'=>$id));
    return $query->row_array();
}

then in controller.. index($id = 0)

and somewhere in index

 if((int)$id > 0)
        {
            $q = $this->test_model->get($id);
            $data['fid']['value'] = $q['id'];
            $data['nm']['value'] = $q['name'];
        }

and mysql table has something like 1. victor, 2. visible etc. as a name value

but here its not taking the value of name and id from form_input and not showing it again in form_view in same input box as victor etc so to update and post it back to database...

anyone please help!! and please be easy as I am new to CI!!

A: 

It's hard to see the problem from your snippets of code, please try and give a little more information as to the structure of your app and where these code samples are placed.

Presume in the last code listing ('somewhere in index') you are getting $id from the form, but you define the ID of the form input box as 'id' array('name'=>'fname','id'=>'id') rather than an integer value so maybe this is where the problem lies.

Where does the $data array get passed to in the third code listing?

jackbot
i am not able to understand? can u give me one small example where i take value in input box from a user then submit it and it goes to mysql table(test) and then again i have one view where i show data with an edit anchor beside the data... and when i click that edit anchor it should go back to the form with same value in input box taken from same mysql table(test) to edit and update again and post it back to table!! remember i took $id as key to catch every single data row!!
A: 

From your question I think you want to display a form to edit a person record in the database.

Controller code

// Normally data object is retrieved from the database
// This is just to simplify the code
$person = array('id' => 1, 'name' => 'stephenc');

// Pass to the view
$this->load->view('my_view_name', array('person' => $person));

View code

<?php echo form_label('Your name: ', 'name'); ?>
<?php echo form_input(array('name' => 'name', 'value' => $person['name'])); ?>

Don't forget to echo what is returned from form_label and form_input. This could be where you are going wrong.

Stephen Curran
Simply tell how can I fetch a record from a mysql table to a form_input box to EDIT and send it back to table after UPDATE e.g | id | Name |Before : 1. Vipul //inserted dynamically by user.After : 1. Vikas // steps. a.)First User click edit anchor. b.)User is redirected to a page where the entry can be edited. c.)After editing user click submit and entry gets updated Note: if user want to update third entry as example it should get redirected to third entry in table to update.With the help of some form_input field.That's it
I've added another answer with a Controller, Model and View to show how to update such a user entry.
Stephen Curran
can u poin ou where i am wrong. this is the model.function main_m(){ $data['nm'] = array('name'=>'fname', 'id'=>'fname'); $data['id']['value'] = 0; return $data; } function insert_entry(){ $data = array('name'=>$this->input->post('fname')); $this->db->insert('test',$data); } function get_all(){ $query = $this->db->get('test'); return $query->result(); } function get_single($id){ $q = $this->db->getwhere('test',array('id'=>$id)); return $q->row_array(); }
+1  A: 

Based on your comment to my first answer, here is a sample of a Controller, Model and View to update a user entry pulled from a table in a database.

Controller

class Users extends Controller
{
    function Users()
    {
        parent::Controller();
    }

    function browse()
    {
    }

    function edit($id)
    {
        // Fetch user by id
        $user = $this->user_model->get_user($id);

        // Form validation
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Name', 'required');

        if ($this->form_validation->run())
        {
            // Update user
            $user['name'] = $this->input->post('name', true);
            $this->user_model->update_user($user);

            // Redirect to some other page
            redirect('users/browse');
        }
        else
        {
            // Load edit view
            $this->load->view('users/edit', array('user' => $user));
        }
    }        
}

Model

class User_model extends Model
{
    function User_model()
    {
        parent::Model();
    }

    function get_user($user_id)
    {
        $sql = 'select * from users where user_id=?';
        $query = $this->db->query($sql, array($user_id));
        return $query->row();
    }

    function update_user($user)
    {
        $this->db->where(array('user_id' => $user['user_id']));
        $this->db->update('users', $user);
    }
}

View

<?php echo form_open('users/edit/' . $user['user_id']); ?>
<div>
    <label for="name">Name:</label>
    <input type="text" name="name" value="<?php echo set_value('name', $user['name']); ?>" />
</div>
<div>
    <input type="submit" value="Update" />
</div>
<?php echo form_close(); ?>
Stephen Curran
now totally confused with this last snippet please a simple example i am new to CI. You are not using active records and my snippet is using only active records. Is CI that much difficult to understand....
To retrieve the record I decided to just write the sql statement myself rather than use the CI database get_where method. Its just out of habit since this is what I normally do. It doesn't really matter. You could use the get_where method instead.The point is the general pattern of the code :1. Controller action is called and passed ID of user to update.2. User object is retrieved from the model and passed to the view.3. The view renders a form where the name can be edited.4. The form is posted back to the same controller method and the model is called to update the user.
Stephen Curran
thanks for the help I got the answer!! Thank You very much Stephenc
@vipul-verma You should mark that answer as the right answer then !
Julien N