skemi90 Posted May 13, 2015 Share Posted May 13, 2015 Hello, I am new to php and I am stuck with this problem. I have in my mysql base a table (table1) with three columns A, B and C (for easy explanation). id A B C 1 60 200 4 2 40 100 3 ... ... ... ... Table has more than 500 combinations. I created two input fields on my index page for column A and B. C column is a result. Problem is to code next: When someone enters 60 and 200 in those two input fields I want to generate an automatic result 4, for 40 and 100 result 3 and so on. I am using codeigniter framework. If it is needed you can split above table to three tables A, B and C. Thanks in advance and sorry for my bad english, please don't hesitate to ask questions. Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/ Share on other sites More sharing options...
Barand Posted May 13, 2015 Share Posted May 13, 2015 It looks like you just need to run a simple query SELECT C FROM table1 WHERE A = $input1 AND B = $input2 Add an INDEX on (A,B) to the table Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511691 Share on other sites More sharing options...
rwhite35 Posted May 13, 2015 Share Posted May 13, 2015 Alternatively, if the values of A and B are not variable, you can run something like: SELECT C_fld FROM test WHERE A_fld + B_fld=260; Where 260 is the sum of A and B. Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511699 Share on other sites More sharing options...
skemi90 Posted May 13, 2015 Author Share Posted May 13, 2015 Model public function GET($input1, $input2){ $this->db->select('c'); $this->db->from('table1'); $this->db->where('A', $input1); $this->db->where('B', $input2); $query = $this->db->get(); return $query; } Controller public function get_result() { //validation rules $this->form_validation->set_rules('a', 'A', 'trim|required'); $this->form_validation->set_rules('b', 'B', 'trim|required'); if ($this->form_validation->run() == FALSE) { $this->load->view('admin/dashboard/inc/header'); $this->load->view('admin/procena/index'); $this->load->view('admin/dashboard/inc/footer'); } else { $input1 = $this->input->post('A'); $input2 = $this->input->post('B'); $result = $this->ModelABC_model->GET($input1,$input2); echo $result; /*redirect('admin/index');*/ } } At least now I'm going in a certain direction, I think I understand your logic but now I have a new problem. I get an error "could not be converted to string" Thank you for your rapid response. Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511710 Share on other sites More sharing options...
Muddy_Funster Posted May 13, 2015 Share Posted May 13, 2015 I would expect $result to be an array, try either echo $result[0] for a single value or foreach($result as $row=>$value){ echo $value; } for multiple lines Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511715 Share on other sites More sharing options...
skemi90 Posted May 13, 2015 Author Share Posted May 13, 2015 Guys, thank you very much!!! I managed to solve the problem. The function is now working. The problem was the lack of foreach loop. (Thanks Muddy_Funster) Thank you again! Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511719 Share on other sites More sharing options...
Muddy_Funster Posted May 13, 2015 Share Posted May 13, 2015 most welcome. could you please mark as solved? Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511742 Share on other sites More sharing options...
skemi90 Posted May 14, 2015 Author Share Posted May 14, 2015 This topic is SOLVED! I was loking a way to mark it by myself, but without luck. Please, can any moderator mark this topic as SOLVED? Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511886 Share on other sites More sharing options...
CroNiX Posted May 14, 2015 Share Posted May 14, 2015 This topic is SOLVED! I was loking a way to mark it by myself, but without luck. Please, can any moderator mark this topic as SOLVED? There is a "Mark Solved" button on the lower-right of each post. Click that next to the post that was most helpful. Link to comment https://forums.phpfreaks.com/topic/296279-mysql-base-help-codeigniter/#findComment-1511888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.