Calculate price from two different table + using php and mysql -
i have 2 tables in database, 1 table has trip/user related information: such trip_id
, username
, leave_date
, return_date
, flight_estimate
, registration_fee
etc, , second table expense table trip_id
fk, , expenses such hotelcost, bf,lunch each date... each trip_ip
has more 1 record in table.
now, i'm trying create summary table total price using php user , when used following code, gives records of trip 1 date right, if has multiple dates, if creates 2 different records.. want 1 record (since i'm calculating grand total of particular trip)
if(isset($user)) { $sql = " select trip.trip_id new_tripid" . " , destination" . " , leave_date" . " , return_date" . " , date(last_updatedate) updateddate" . " , flight_estimate" . " , registration_fee" . " , hotel_cost" . " , breakfast_cost" . " , lunch_cost" . " , dinner_cost" . " , parking_cost" . " , taxi_cost" . " , rental_car_cost" . " , mileage" . " , fuel_cost" . " , other_cost" . " trip" . " , expense" . " trip.username='" . $user ."'" . " , trip.trip_id = expense.trip_id" ; $result = mysql_query($sql); $num_rows = mysql_num_rows($result); $o = '<table id="mytable1" border="1" style="border:1px solid orange; background-color: #f3f3f3;"><thead><tr><th>trip id</th><th>destination</th><th>leave date</th><th>return date</th><th>total</th><th>submit date</th><th>status</th></tr></thead><tbody>'; } else { echo('not valid user'); } if (!$result) { die(mysql_error()); } if ($num_rows<=0){ echo('not valid user'); } elseif ($num_rows>0) { while($row = mysql_fetch_array($result)) { $grandtotoal = $row['flight_estimate'] + $row['registration_fee'] + $row['hotel_cost']+$row['breakfast_cost']+$row['lunch_cost']+ $row['dinner_cost']+$row['parking_cost']+$row['taxi_cost']+ $row['rental_car_cost'] +$row['mileage']+$row['fuel_cost']+$row['other_cost']; $o .= '<tr><td align = "center" style="width:absolute; height:30px;">'. $row['new_tripid'].'</td><td align = "center" style="width:absolute;height:30px;">'. $row['destination']. '</td><td align = "center" style="width:absolute height:30px;">'. $row['leave_date']. '</td><td align = "center" style="width:absolute; height:30px;">'.$row['return_date']. '</td><td align = "center" style="width:absolute; height:30px;">'. $grandtotoal.'</td><td align = "center" style="width:absolute; height:30px;">'. $row['updateddate'].'</td></tr>'; } $o .= '</tbody></table>'; echo $o; }
select trip.trip_id new_tripid,destination, leave_date, return_date, date(last_updatedate) updateddate, flight_estimate , registration_fee, sum(hotel_cost, breakfast_cost, lunch_cost, dinner_cost, parking_cost, taxi_cost, rental_car_cost, mileage, fuel_cost, other_cost) total_cost trip, expense trip.username='$user' , trip.trip_id = expense.trip_id group trip.username
Comments
Post a Comment