I'm creating an internal program for our sales department. It's for tracking trailer sales in each state for our company as well as 5 other different companys that we track.
I have created a table and created a table header(<th>) that consists of the different companys, now what I'm trying to do is if a company sold trailers in that state I list how many, but if a company didn't sell any trailers for a state I want to put in a 0.
I can not get it to come out the way I would like it to. Right now I'm only using one state for testing.
The code I'm using is as follows:
$sql = "SELECT COUNT(polk_data.state),
COUNT(polk_data.make),
polk_data.make,
polk_data.state,
polk_data.body,
states.state,
states.abv_state
FROM polk_data, states
WHERE polk_data.body = 'livestock'
AND states.state = 'Wyoming'
AND polk_data.state = states.abv_state
GROUP BY(states.abv_state),(polk_data.make)
ORDER BY states.abv_state DESC, polk_data.make ASC";
$result = mysql_query($sql,$cn) or die($sql.mysql_error());
$sub_total_state = '';
$total = 0;
print "<table border='1' width='89%' align='left'>";
print "<tr>";
while ($row = mysql_fetch_assoc($result2)) {
if($total == 0) {
$sub_total_state = $row['state'];
}
$total += $row['COUNT(polk_data.make)'];
if($row['state'] != $sub_total_state) {
if($sub_total_state != '')
print "<td align='center'>Total<br>".$sub_total."</td>";
$sub_total_state = $row['state'];
$sub_total = $row['COUNT(polk_data.make)'];
}
else {
$sub_total += $row['COUNT(polk_data.make)'];
}
if($row['make'] == 'BARRETT TRAILER') {
print "<td>".$row['COUNT(polk_data.make)']."</td>";
}
else {
print "<td>0</td>";
}
if($row['make'] == 'M H EBY') {
print "<td>".$row['COUNT(polk_data.make)']."</td>";
}
else {
print "<td>0</td>";
}
if($row['make'] == 'GUTHRIE') {
print "<td>".$row['COUNT(polk_data.make)']."</td>";
}
else {
print "<td>0</td>";
}
if($row['make'] == 'MERRITT EQPT') {
print "<td>".$row['COUNT(polk_data.make)']."</td>";
}
else {
print "<td>0</td>";
}
if($row['make'] == 'M & W TRAILERS') {
print "<td>".$row['COUNT(polk_data.make)']."</td>";
}
else {
print "<td>0</td>";
}
if($row['make'] == 'WILSON TRAILER') {
print "<td>".$row['COUNT(polk_data.make)']."</td>";
}
else {
print "<td>0</td>";
}
}
Right now I'm using the if statement but I have tried a if/elseif/else and a switch statement, but cannot get it to come out the way I would like.
The data output looks like:
Barret trailer - 1
Merritt Eqpt - 1
Wilson trailer - 6
But I woukld like to see:
Barrett Trailer -1
M H EBY - 0
M & W trailers - 0
Guthrie - 0
Merritt Eqpt - 1
Wilson trailer - 6
All of this is in a table row(<tr> and <td>)
Does anybody have any ideas on how I can accomplish this, any help would be greatly appreciated