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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Solution Muddy_Funster Posted May 13, 2015 Solution 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 Quote Link to comment 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! Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.