0

This return of print_r($query->result()); would be:

Array ( [0] => stdClass Object ( [guest_name] => Test Name [guest_gender] => Male [guest_nic_pp_dl] => 123456789 ) )

What I need is to pass those values into input text boxes, radio buttons and dropdowns in the view respectilvely.

For example, I need 'guest_name' to be in an input, 'guest_gender' value to be selected on the view, and dropdown value corresponding to 'guest_nic_pp_dl' to be selected on a dropdown (HTML select).

Controller:

function get_customer_details() {
    $guest_name = $this->input->post('guest_name');
    $this->banquet_model->talk_to_new_guest_table($guest_name);
    $this->load->view('/main/banquet_view');
}

Model:

function talk_to_new_guest_table($guest_name) {
    $query = $this->db->query(" SELECT guest_name, guest_gender, guest_nic_pp_dl
                                FROM new_guest 
                                WHERE guest_name LIKE '$guest_name%'
                                LIMIT 1 ");
    if($query->num_rows()>0) {
        return $query->result();
    }
    else {
        return 0;
    }
}

View:

<div class="control-group">   
<label for="guest_name" class="control-label"><i class="icon-user"></i> Name: </label>
    <div class="controls">
        <div class="input-append">
        <input type="text" id="appendedInputButtons" class="span2" name="guest_name" value="<?php echo set_value('guest_name'); ?>">
        <input class="btn" type="submit" name="searchGuest" value="Search">
    </div>
<?php echo form_error('guest_name'); ?>
</div>

2
  • 1
    Where are you returning the result? Commented Jul 12, 2013 at 4:36
  • I haven't yet. I tried to echo in the view but it didn't work. I need to populate simple text boxes and they are not shown in the above code. For example, <input type="text" id="appendedInputButtons" class="span2" name="sth" > Commented Jul 12, 2013 at 4:38

3 Answers 3

2

make some changes in

Controller :

$guest=$this->banquet_model->talk_to_new_guest_table($guest_name);
 //creating data array from returned result
 $data['guest_name']=$guest->guest_name;
 $data['guest_gender']=$guest->guest_gender;
 $data['guest_nic_pp_dl']=$guest->guest_nic_pp_dl;
 //loading view with data
 $this->load->view('/main/banquet_view',$data);

more important all these data array element will be available as variable on view page like

$data['guest_gender'] as $guest_gender
Sign up to request clarification or add additional context in comments.

6 Comments

Oh, wait... Now it returns 3 "Trying to get property of non-object" messages
@saucecord on where ? are you using like this <input type="text" id="appendedInputButtons" class="span2" name="guest_name" value="<?php echo $guest_name ?>">
@saucecord what is returning 3 and where you are getting "Trying to get property of non-object"...
As soon as I submit, it returns backs to index(); (...which I expected) and gives the error.
@saucecord cant help with this info .it is possible that result is returning nothing .
|
1

The answers from Rajeev Ranjan and Prasanth are ok but on the line return $query->result(); you can do thisreturn $query->row(); the reason is because the result() returns an array of objects which needs to be iterated while the row() object returns a single object which you can reference without iterating with a loop. I hope this will help

Comments

1

Try something on the lines of:

Controller:

function get_customer_details() {
    $guest_name = $this->input->post('guest_name');
    $data = $this->banquet_model->talk_to_new_guest_table($guest_name);
    if ($data != 0) {
        $this->load->view('/main/banquet_view', $data);
    }
}

Model:

function talk_to_new_guest_table($guest_name) {
    $query = $this->db->query(" SELECT guest_name, guest_gender, guest_nic_pp_dl
                                FROM new_guest 
                                WHERE guest_name LIKE '$guest_name%'
                                LIMIT 1 ");
    if($query->num_rows()>0) {
        return $query->result();
    }
    else {
        return 0;
    }
}

View:

<div class="control-group">   
<label for="guest_name" class="control-label"><i class="icon-user"></i> Name: </label>
    <div class="controls">
        <div class="input-append">
        <input type="text" id="appendedInputButtons" class="span2" name="guest_name" value="<?php echo $guest_name; ?>">
        <input class="btn" type="submit" name="searchGuest" value="Search">
    </div>
<?php echo form_error('guest_name'); ?>
</div>

Comments

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.