How to dynamically write reports ?On the Fly?
This is a simple way of querying your database for specific instances and only having to write one SQL query that does it all. This example is something I wrote for an Inventory Management System at my job.
Here is the Results Page with the SQL Statement
<cfquery name="viewreports"
datasource="#application.dsn#">
Select columnname1, columnname2?.
from table
where 1=1
#urldecode(preservesinglequotes(url.whatever))#
<!--- (url.whatever means you can name this url.whatever you want)
--->
</cfquery>
<table>
<th>Service Tag</th>
<th>Machine Name</th>
<th>Owner</th>
<th>Make</th>
<th>Mode</th>
<th>Speed</th>
<th>Memory</th>
<cfoutput
query="viewreports">
<!--- (This basically checks the data and if the field is blank, returns a ?not found? message in the report or returns the data ?trimmed? with no spaces in front (just in case there is)
--->
<tr>
<td>#iif(len(trim(servicetag)) eq 0, de("no service
tag"), de(trim(servicetag)))#</td>
<td>#iif(len(trim(machinename)) eq 0,
de("no machine name"),
de(trim(machinename)))#</td>
<td>#iif(len(trim(owner)) eq 0,
de("no owner name"), de(trim(owner)))#</td>
<td>#iif(len(trim(make)) eq 0,
de("no make"), de(trim(make)))#</td>
<td>#iif(len(trim(model)) eq 0,
de("no model"), de(trim(model)))#</td>
<td>#iif(len(trim(processorspeed)) eq 0,
de("no speed"), de(trim(processorspeed)))#</td>
<td>#iif(len(trim(memory)) eq 0,
de("no memory"), de(trim(memory)))#</td>
</tr>
</cfoutput>
</table>
This basically is a text page with URL?s leading to the necessary reports.
<cfoutput>
<table width="40%"
border="2"
bordercolor="E8E8E8">
<tr bgcolor="ffe4b5">
<td>All Reports</td>
</tr>
<tr>
<td><a href="viewreports.cfm?whatever=#urlencodedformat("and
column1='Desktop'")#">All Desktop Users</a></td>
<tr>
<td><a href="viewreports.cfm?dsql=#urlencodedformat("and column2='Dell' and
column1='Desktop'")#">All Dell Desktop
Users</a></td>
<!--- (example of multiple selections ? it searches for Dell and Desktop and returns all users who have a Dell Desktop)
--->
</tr>
</table>
</cfoutput>
Everyone has had their bosses say ?I need a report with this, this, and this and I need it
now?. Using this code, you can simply copy and paste the code, change the data that you are looking for and then have a report ? without having to write multiple SQL statements.