Welcome Guest, Not a member yet? Register   Sign In
using update function to display data within form, want to use same function to update on form post
#1

[eluser]Brad K Morse[/eluser]
In the admin controller I have the update function

Code:
function update() {
    $this->load->model('data_model');
    $data['volunteers'] = $this->data_model->getVolunteer();
    $this->load->view('update', $data);
}

In the model

Code:
function getVolunteer() {
    $q = $this->db->where('id',$this->uri->segment(3))->get('data');
    
    if($q->num_rows() > 0) {
        foreach($q->result() as $row) {
            $data[] = $row;
        }
    return $data;
    //in theory
     if($_POST) {
        get values
        update database
      }
    }
}

URL is domain.com/index.php/admin/update/4

It takes segment(3) as the id = 4, to pull in the appropriate data into the form which is the view (update) and what I want to do, is use the same getVolunteer and update function (above) to update that record that id = 4, when the form posts to itself, the form code is below (update view)

Code:
<?php
foreach($volunteers as $r) { ?>
<?=form_open(uri_string());?>
    <p>First name: &lt;input type="text" value="&lt;?=$r-&gt;first_name?&gt;" name="first_name"></p>
    <p>Shirt Size: &lt;input type="text" value="&lt;?=$r-&gt;tshirt?&gt;" size="3" name="tshirt"></p>
    &lt;?=form_submit('submit', 'Update!');?&gt;
&lt;?=form_close();?&gt;
&lt;?php } ?&gt;

as you can tell, I am new to this, any help is appreciated.

p.s. I have url and form helper autoloaded, so any revising to shorten my code is appreciated, I have a feeling it can be shorten when I refer to a segment.

I edited the getVolunteer function to reflect what I am trying to do

Code:
function getVolunteer() {
    $q = $this->db->where('id',$this->uri->segment(3))->get('data');
    
    if($q->num_rows() > 0) {
        foreach($q->result() as $row) {
            $data[] = $row;
    }
    
    if($_POST) {
        $data[]=$_POST['first_name'];
        $data[]=$_POST['tshirt'];
      $this->db->where('id',$this->uri->segment(3))->update('data', $data);
    }
    
    return $data;
    }
}

Error message: http://cl.ly/8aaab4472f746ac2c8a7
#2

[eluser]Brad K Morse[/eluser]
I think I got it to work:

added a model function, updateVolunteer

Code:
function updateVolunteer($id) {
    /*$data['first_name']=$_POST['first_name'];
    $data['tshirt']=$_POST['tshirt'];*/
  $this->db->where('id',$id)->update('data', $data);
    redirect('/admin/update/'.$id);
}

then referenced it in the controller update function on post

Code:
function update($id, $data) {
    $this->load->model('data_model');
    $data['volunteers'] = $this->data_model->getVolunteer($id);
    
    if($_POST) {
        //$data['volunteers'] = $this->data_model->updateVolunteer($id);
        $data['volunteers'] = $this->data_model->updateVolunteer($id,$_POST);
    }
    
    $this->load->view('update', $data);
}

should I test for other things in the $_POST conditional? to make sure segment(3) is not empty? Let me know.
#3

[eluser]Dennis Rasmussen[/eluser]
Use this library: http://ellislab.com/codeigniter/user-gui...ation.html
#4

[eluser]Brad K Morse[/eluser]
Suggesting I use that, to properly accomplish the if($_POST) for running the updateVolunteer() function?




Theme © iAndrew 2016 - Forum software by © MyBB