1

Good day all. I'm new to codeigniter and I'm trying to pass a variable from one view page to another. But I get error and after a lot of tries still couldn't figure it out.

This is the Model code:

function get_post($postID){
    $this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
    $query=$this->db->get();
    return $query->result_array();
}

function update_post($postID, $data)
{
    $this->where('post_id', $postID);
    $this->db->update('khanposts', $data);
}

This is the Controller code:

function editpost($postID)
{
    $data['success']=0;
    if($_POST){
        $data_post=array(
            'fullname'=>$_POST['fullname'],
            'dob'=>$_POST['dob'],
            'blood'=>$_POST['blood'],
            'village'=>$_POST['village'],
            'occupation'=>$_POST['occupation'],
            'company'=>$_POST['company'],
            'email'=>$_POST['email'],
            'contact'=>$_POST['contact'],
            'password'=>$_POST['pass'],
            'marry'=>$_POST['marry']);
        $this->khanpost->update_post($postID, $data);
        $data['success']=1;
    }
    $data['post']=$this->khanpost->get_post($postID);
    $this->load->view('edit_post', $data);
}

This is the code of View page which passes the value to edit_post view page:

foreach ($posts as $row){
<tr><td><a href="' .base_url(). 'khanposts/editpost/' .$row['post_id']. '" target="_blank"><i>Edit</i></a></td></tr>';
}

This is code of edit_post view page where it must get the value of $row['post_id']:

echo form_open(base_url().'khanposts/editpost/'.$row['post_id']);
echo '<b>Full Name: </b>';
$data_form=array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value'=>$row['fullname'] );
echo form_input($data_form);

How do I assign the passsed variable($row['post_id']) in form_open()? Any solution will be really helpful. Tnx.

1
  • you can use form_open(base_url().'khanposts/editpost/?id='.$row['post_id']) and in controller check for $this->input->get('id') Commented Jul 4, 2014 at 8:01

1 Answer 1

3

As far as I understand the problem;

Pass PostID to view in editpost function:

function editpost($postID)
{
   ...
   $data['postID'] = $postID;
}

Get it in view page like:

echo form_open(base_url('khanposts/editpost/'.$postID));

You should load form helper:

$this->load->helper('form');

And your data_form input:

$data_form = array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value' => $post['fullname']);

You should use row_array instead of result_array, because of you get single post.

function get_post($postID)
{
    $this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
    $query=$this->db->get();
    return $query->row_array();
}
Sign up to request clarification or add additional context in comments.

12 Comments

You write $data['postID'] = $postID; before the `$this->load->view``line, right?
Did you look form action url, is it empty? And try this variable in view page echo form_open(base_url('khanposts/editpost/'.$post['post_id']));
@SinOscuras And you should load form helper. Updated answer
@SinOscuras Hmm.. Could you dump it in view page? var_dump($post); you will see..
@SinOscuras Answer is updated. You should use row_array instead of result_array, because of you get single post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.