I have two tables in my database, one table has trip/user related information: such as trip_id
, username
, leave_date
, return_date
, flight_estimate
, registration_fee
etc, and the second table is an expense table trip_id
as my FK, and all the expenses such as hotelcost, bf,lunch for each date... so each trip_ip
has more than one record in this table.
Now, I'm trying to create a summary table with total price using php for the user and when I used the following code, it gives the records of the trip with one date right, but if it has multiple dates, if creates two different records.. but I want it as one record (since I'm calculating grand total of that particular trip)
if(isset($user))
{
$sql =
" SELECT Trip.trip_id as new_tripid"
. " , destination"
. " , leave_date"
. " , return_date"
. " , date(last_updatedate) as 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"
. " FROM Trip"
. " , Expense"
. " WHERE Trip.username='" . $user ."'"
. " AND 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 as new_tripid,destination, leave_date, return_date,
date(last_updatedate) as 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) as total_cost
FROM Trip, Expense
WHERE Trip.username='$user' AND Trip.trip_id = Expense.trip_id
GROUP BY Trip.username
You can use GRUOP BY
of MySQL:
SELECT Trip.trip_id as new_tripid,destination, leave_date, return_date,
date(last_updatedate) as 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
FROM Trip, Expense
WHERE Trip.username='$user' AND Trip.trip_id = Expense.trip_id
GROUP BY Trip.username