i have to calcuate BMI for a user by taking Users_height & users_weight from the different two tables.
Here is the two tables(http://i.stack.imgur.com/eOQs4.gif) & (http://i.stack.imgur.com/liqhH.gif)
DESIRED OUTPUT
weight_pounds height_cm
121.25 130
132.28 160
154.32 221
176.37 434
user_weight Table
user_height table
I tried this function, but its showing error:
function get_all_user_bmi($uid)
{
$this->load->database();
$this->db->select('w.weight_pounds','h.height_cm');
$res = $this->db->order_by('w.uweight_id', 'ASC')->order_by('h.height_id', 'ASC')
->get_where('user_weight w',array('w.creater_id'=>$uid))
->get_where('user_height h',array('w.creater_id'=>$uid))
;
$ret = array();
foreach ($res->result_array() as $row) {
$weight=$row['w.weight_pounds']* 4.88;
$height=($row['h.height_cm']*0.032808)*($row['h.height_cm']*0.032808);
$bmi=$weight/$height;
$ret[] = $bmi; //final bmi formuala calculated
}
return $ret;
}
Error is:
A Database Error Occurred
Error Number: 1054
Unknown column 'h.height_id' in 'order clause'
SELECT `w`.`weight_pounds` FROM (`user_weight` w) WHERE `w`.`creater_id` = '3235' ORDER BY `w`.`uweight_id` ASC, `h`.`height_id` ASC
Filename: D:\xampp\htdocs\webapp\system\database\DB_driver.php
Line Number: 330
Hope someone can help me here...
UPDATE
Added extra column same_id
in both the column to meet the requirements and added $this->db->join('user_height h' ,'w.same_id = h.same_id');
this piece of code, now its working fine
With reference to @dianuj Answer, and my little effort and searched , i found out a solution :
To distinguish between two table i use an extra column same_id of every same input . like
Finally my code looks like below, and solved my problem
Try this with
join
you haven't joined your tables, if you need the user details like height , weight etc then in your tables you should save the user information along with the user id in both tables as there is one - to- relation , then join your tables on the basis of that user id to calculate the bmi for userOR
And in loop just use the column name
Reference Active record