0

这个 print_r($query->result()); 的返回 将会:

数组([0] => stdClass 对象([guest_name] => 测试名称 [guest_gender] => 男性 [guest_nic_pp_dl] => 123456789))

我需要将这些值分别传递到视图中的输入文本框、单选按钮和下拉列表中。

例如,我需要在输入中输入“guest_name”,在视图中选择“guest_gender”值,并在下拉列表中选择与“guest_nic_pp_dl”对应的下拉值(HTML 选择)。

控制器:

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');
}

模型:

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;
    }
}

看法:

<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>

4

3 回答 3

2

做一些改变

控制器 :

$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);

更重要的是,所有这些数据数组元素都将在视图页面上作为变量提供,例如

$data['guest_gender'] as $guest_gender
于 2013-07-12T04:42:30.027 回答
1

Rajeev Ranjan 和 Prasanth 的答案是好的,但是return $query->result();你可以这样做return $query->row();,原因是 result() 返回一个需要迭代的对象数组,而 row() 对象返回一个你可以引用的对象而不用循环迭代。我希望这个能帮上忙

于 2013-07-12T11:30:22.150 回答
1

尝试以下内容:

控制器:

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);
    }
}

模型:

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;
    }
}

看法:

<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>
于 2013-07-12T04:41:56.487 回答