【发布时间】:2020-03-15 04:15:43
【问题描述】:
我需要获取我的 PK 值。
现在,我遇到的问题是我要获取的值是外键。如何获取 PK id 值?
对于视觉表示:看看我的问题
所以所有值都是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');
}
查看我的表格和我应该获取的 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>
【问题讨论】:
-
打印 $data['questions']; 的结果是什么?
-
@SandeepModak - 你好,它会打印出
Array();你能帮我解决这个问题吗? -
@SandeepModak 正是它打印出来的内容
Array ( [0] => Array ( [id] => 1 [answer] => Yes [answer_id] => 1 [question_id] => 1 [correct] => 1 [type_id] => 0 [question] => Apple is color Red? ) [1] => Array ( [id] => 1 [answer] => No [answer_id] => 2 [question_id] => 1 [correct] => 0 [type_id] => 0 [question] => Apple is color Red? ) ) -
@SandeepModak 是您需要的吗?
-
在你的结果中来自问题表的所有列是什么
标签: php mysql codeigniter codeigniter-3