有没有办法在加入后获取 table PK id 值而不是外键值?代码点火器
Is there a way to fetch table PK id value instead of foreign key value after Join? Codeigniter
我需要获取我的 PK 值。
现在,我遇到的问题是我正在获取的值是外键。如何获取 PK id 值?
对于视觉表示:看看我的问题
我的 UI 和问题:
所以所有的值都是1
,这是外键的值。如何将其更改为 1
、 2
等,这是 PK id 值?
我的模型有连接查询
public function get_questions($id){
$this->db->select('*');
$this->db->from('answers');
$this->db->join('questions', 'answers.question_id = questions.id');
$this->db->where('questions.id', $id);
$query = $this->db->get();
return $result = $query->result_array();
}
控制器
public function edit($id)
{
$data['questions'] = $this->post_model->get_questions($id);
$this->load->view('templates/header');
$this->load->view('teachers/edit', $data);
$this->load->view('templates/footer');
}
查看我的 table 和我应该获取的 ID:
查看
<?php echo form_open('posts/update'); ?>
<!-- <input type="hidden" name="id[]" value="<?php echo $posts['id']; ?>" /> -->
Question:
<input type="text" name="question" class="form-control" value="<?php echo $posts['question']; ?>" /><hr>
Answers:
<?php foreach($questions as $question): ?>
<input type="text" name="id[]" value="<?php echo $question['id']; ?>" /><hr>
<input type="text" name="answer[]" class="form-control" value="<?php echo $question['answer']; ?>" /><hr>
<?php endforeach; ?>
<hr>
<input type="submit" class="btn btn-success" value="Save Changes">
</form>
尝试使用
$this->db->select('answers.id,answers.answer,answers.question_id,answers.correct,answers.type_id');
$this->db->from('answers');
$this->db->join('questions', 'answers.question_id = questions.id');
$this->db->where('questions.id', $id);
$query = $this->db->get();
return $result = $query->result_array();
根据查询获得结果
我需要获取我的 PK 值。
现在,我遇到的问题是我正在获取的值是外键。如何获取 PK id 值?
对于视觉表示:看看我的问题
我的 UI 和问题:
所以所有的值都是1
,这是外键的值。如何将其更改为 1
、 2
等,这是 PK id 值?
我的模型有连接查询
public function get_questions($id){
$this->db->select('*');
$this->db->from('answers');
$this->db->join('questions', 'answers.question_id = questions.id');
$this->db->where('questions.id', $id);
$query = $this->db->get();
return $result = $query->result_array();
}
控制器
public function edit($id)
{
$data['questions'] = $this->post_model->get_questions($id);
$this->load->view('templates/header');
$this->load->view('teachers/edit', $data);
$this->load->view('templates/footer');
}
查看我的 table 和我应该获取的 ID:
查看
<?php echo form_open('posts/update'); ?>
<!-- <input type="hidden" name="id[]" value="<?php echo $posts['id']; ?>" /> -->
Question:
<input type="text" name="question" class="form-control" value="<?php echo $posts['question']; ?>" /><hr>
Answers:
<?php foreach($questions as $question): ?>
<input type="text" name="id[]" value="<?php echo $question['id']; ?>" /><hr>
<input type="text" name="answer[]" class="form-control" value="<?php echo $question['answer']; ?>" /><hr>
<?php endforeach; ?>
<hr>
<input type="submit" class="btn btn-success" value="Save Changes">
</form>
尝试使用
$this->db->select('answers.id,answers.answer,answers.question_id,answers.correct,answers.type_id');
$this->db->from('answers');
$this->db->join('questions', 'answers.question_id = questions.id');
$this->db->where('questions.id', $id);
$query = $this->db->get();
return $result = $query->result_array();
根据查询获得结果