[Help]Can't get the data from Database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: [Help]Can't get the data from Database (/showthread.php?tid=49773) |
[Help]Can't get the data from Database - El Forum - 03-02-2012 [eluser]iamleeloo[/eluser] I copied and wrote some code yesterday. But the result was not satisfied. I created 2 tables in a database named "test". Table1: named "test" and there is 2 rows. "id" and "letters" Data in table1: 3groups, (1,restaurant), (2,repas), (3,resto) Table2: named "relation" and there is 3 rows. "relation_id" and "mot_1" and "mot_2" Data in table2: 2groups, (1,restaurant,repas), (2,restaurant,resto) [Help]Can't get the data from Database - El Forum - 03-02-2012 [eluser]iamleeloo[/eluser] Here are the codes: words_model.php <?php class Words_model extends CI_Model { public function __construct() { $this->load->database(); } public function get_words($zimu) { $query = $this->db->query("SELECT * FROM test WHERE letters = '$zimu';"); return $query->result_array(); } public function get_relation_mots($zimu) { $query = $this->db->query("SELECT mot_2 FROM relation WHERE mot_1 = '$zimu';"); return $query->result_array(); } } [Help]Can't get the data from Database - El Forum - 03-02-2012 [eluser]iamleeloo[/eluser] words_db.php <?php class Words_db extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('words_model'); } public function index() { $data['words'] = $this->words_model->get_words('restaurant'); //Get the value of "id" and "letters" of "restaurant" in table "test" $data['title'] = 'Words'; $data['relation_mots'] = $this->words_model->get_relation_mots('restaurant'); //Get the values of "mot_2" of "mot_1=restaurant" in table "relation". They are"repas" and "resto". $counter = 0; foreach ($data['relation_mots'] as $relation_mots_item): { $data['relation_mots_id2'][$counter] = $this->words_model->get_words($relation_mots_item['mot_2']); //Get the value of "id" and "letters" of "repas" and "resto" in table "test" $counter = $counter+1; } endforeach; $data['counter'] = $counter; $this->load->view('templates/header', $data); $this->load->view('words_db/index', $data); $this->load->view('templates/footer'); } } [Help]Can't get the data from Database - El Forum - 03-02-2012 [eluser]iamleeloo[/eluser] I don't know why I can't post full codes in index.php. The key code are: <?php for ($relation_mots_id_counter=0; $relation_mots_id_counter<$counter; $relation_mots_id_counter++) { echo $relation_mots_id2[$relation_mots_id_counter]['id']; //Output the value of "id" of "repas" and "resto" in table "test" } ?> But on the final page, these words are displayed: "A PHP Error was encountered Severity: Notice Message: Undefined index: id Filename: words_db/index.php Line Number: 26" What should I do? Thanks. |