print_r的返回($ query-> result());将是:
数组([0] => stdClass对象([guest_name] =>测试名称[guest_gender] =>男[guest_nic_pp_dl] => 123456789))
我需要的是将这些值传递到视图中的输入文本框,单选按钮和下拉列表中。
例如,我需要'guest_name'在输入中,要在视图上选择'guest_gender'值,并在下拉列表中选择与'guest_nic_pp_dl'相对应的下拉值(HTML select)。
控制器:
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>
答案 0 :(得分: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
答案 1 :(得分: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>
答案 2 :(得分:1)
Rajeev Ranjan和Prasanth的答案还可以,但在return $query->result();
行你可以这样做return $query->row();
原因是因为result()返回一个需要迭代的对象数组row()对象返回一个可以引用的对象,而不需要循环迭代。我希望这会有所帮助