Jump to content

[SOLVED] Creating a table on the fly with mySQL results


Recommended Posts

Ok I will explain this the best I can and I hope someone understands and can help.

 

I have 3 tables

 

tbl_Vehicles

-REG

-STATUS_ID

-LOCATION_ID

 

tbl_status

-ID

-NAME

 

tbl_location

-ID

-NAME

 

I want to create a table like this:

 

/                \ LOCATION1 - LOCATION2 - LOCATION3

STATUS1            1                4                    2

STATUS2            3                7                    9

STATUS3            9                2                    7

 

I want the data in the table to be generated on the fly by reading the names of the locations and status' for the header row and the left column, then I want to automatically fill the values in as well, by reading for the vehicle table the number of vehicles at location1 with a status of status1.

 

I cant show you the code I have so far, or rather I can but it just isnt even close to getting it right.

 

I know the code for the selecting the data from the tables, but I am stuck on how to put the results in the right places.

lol as always, after asking I sorted it :)

 

$status_query = "SELECT ID,NAME FROM `tbl_status`";
$location_query = "SELECT ID,NAME FROM `tbl_locations`";

$status_result = mysql_query($status_query);
$location_result = mysql_query($location_query);

echo "<table><tr><th> </th>";
while ($row = mysql_fetch_array($location_result)) {
	echo "<th>{$row['NAME']}</th>";
}
echo "<th>Totals</th></tr>";
while ($status= mysql_fetch_array($status_result)) {
	echo "<tr><td>{$status['NAME']} </td>";
	$total = 0;
	$location_result = mysql_query($location_query);

	while ($location = mysql_fetch_array($location_result)) {
		$query = "SELECT * from `tbl_vehicles` WHERE `STATUS_ID` = '{$status['ID']}' AND `LOCATION_ID` = '{$location['ID']}'";
		$result = mysql_query($query) or die(mysql_error());
		$count = mysql_num_rows($result);
		$total = $total + $count;
		echo "<td><a href='http://intranet/dev/temp/remarketing/reporting.php?LOCATION_ID={$location['ID']}&STATUS_ID={$status['ID']}&submit=Search'>$count</a></td>";

	}
	echo "<td>$total</td></tr>";
}
echo "</table>";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.