我是新來的CodeIgniter和我建立一個簡單的I/O網站。我只有一個數據庫,說test
只有一個表results
,看起來像這樣:插入一行使用不同的型號/頁面 - CodeIgniter
截圖表的「結果」
我有兩個觀點personal.php
和songs.php
。在第一個中,我收集要插入到字段2,3和4中的數據值,其餘值將在第二個視圖中收集。然後將它們通過相關型號personal_model
和songs_model
插入表格中。 現在很明顯,他們將被插入到兩個不同的行,這是不是我想要的。這裏有什麼竅門?我應該如何管理它?到目前爲止,我已經想過得到最後一個ID,但我不知道該怎麼做。提前致謝!
personal.php
(第一視圖)
<?php echo validation_errors(); ?>
<?php echo form_open('data/personal'); ?> //passes the data to the controller that loads the personal_model.php
"some input fields"
<button type="submit" name="submit">Submit Data</button>
songs.php
(第二視圖)
<?php echo validation_errors(); ?>
<?php echo form_open('data/songs'); ?> //passes the data to the controller that loads the songs_model.php
"some input fields"
<button type="submit" name="submit">Submit Rating</button>
personal_model.php
(第一模型)
<?php
class Personal_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function insert_personal()
{
$this->load->helper('url');
$data = array(
'age' => $this->input->post('user_age'),
'education' => $this->input->post('user_edu'),
'twitter' => $this->input->post('user_twitter'),
'facebook' => $this->input->post('user_facebook')
);
return $this->db->insert('results', $data);
}
}
songs_model.php
(第二模型)
<?php
class Ratings_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function insert_ratings()
{
$this->load->helper('url');
#$this->load->database();
$data = array(
'score' => $this->input->post('rating'),
'song1' => $this->input->post('rnd1'),
'song2' => $this->input->post('rnd2')
);
return $this->db->insert('results', $data);
}
}
這是一些像親子一樣的事情。這意味着根據你的問題'personal.php'是父母,'songs.php'是孩子。因此,插入第一個父數據並保存,然後使用父代的ID加載它。希望它的幫助 –
你不需要有兩個模型,因爲你在這兩個文件中做同樣的事情(插入行)。你最好將數據從你的控制器傳遞給你的模型,在你的模型中有'public function insert_data($ data)'並且在你的控制器中構建你的數組,而不是在你的模型中。 – Dan
@AbdullaNilam非常感謝!它給了我一些見解。 –