Skip to main content
Inspiring
August 30, 2007
Question

building dynamic query

  • August 30, 2007
  • 2 replies
  • 482 views
hi Folks,

I display my query using a table and I want to sort the results by clicking the header of each column, and building the order by statement in the query. I want the user to be able to select various combinations so they can filter the dta accordingly.

Can anyone help?

My code is below, but it doesnt work at the moment.

Thankyou

------------

<!--- <cfquery name="name" datasource="weblog" username="#dsn_username#" password="#dsn_password#">
select *
from staff
where staffnum = '#session.userid#'
</cfquery> --->
<style>
tr.green{background-color:#99ff99;}
tr.red{background-color:#FFCCCC;}
tr.orange{background-color:#FFCC66;}

</style>
<cfif isdefined("staffname")>

ORDER BY staffid ASC;
</cfif>
<cfquery name="get" datasource="weblog" username="#dsn_username#" password="#dsn_password#">
SELECT *
FROM table1
<!--- where staffid = #name.staffid# --->
<cfif isdefined("sort")>
ORDER BY
<cfif sort eq "staffname">
staffid
</cfif>
<cfif sort eq "taskname">
, priority
</cfif>
<cfif sort eq "dateset">
, dateset
</cfif>
<cfif sort eq "completiondate">
, completiondate
</cfif>
<cfif sort eq "isdone">
, isdone
</cfif>;
</cfif>
</cfquery>
<cfquery name="sortByDate" datasource="weblog" username="#dsn_username#" password="#dsn_password#">
SELECT *
FROM table1
ORDER BY dateset DESC;
</cfquery>
<img src="../images/addfolder.gif" width="15" height="15" border="0"> <a href="add-article.cfm">ADD
JOB</a><br>
<cfoutput>
<table width="100%" >
<tr>
<td ><a href="?area=weblog/index
&sort=staffname">Name</a> </td>
<td><a href="?area=weblog/index
&sort=taskname">Task name</a> </td>
<td><a href="?area=weblog/index
&sort=dateset
">Task name</a> </td>
<td><a href="?area=weblog/index
&sort=completiondate">Completed</a> </td>
<td><a href="?area=weblog/index
&sort=dateset">Completed</a> </td>
</tr>
</cfoutput>
<cfoutput query="get">
<tr
<cfif priority eq 3>
class="green"
<cfelseif priority eq 2>
class="orange"
<cfelseif priority eq 1>
class="red"
</cfif>


>
<td><cfif #staffid# eq '1'>
Matt
<cfelseif #staffid# eq '2'>
Hayley
<cfelseif #staffid# eq '3'>
David
</cfif>
</td>
<td><a href="view-job.cfm?jobid=#jobid#">#Taskname#</a></td>
<td width="10%">#DateFormat(dateset, "dd/mm/yyyy")#</td>
<td width="10%">#DateFormat(CompletionDate, "dd/mm/yyyy")#</td>
<td width="10%">
<cfif #isdone# eq 1>
<img src="../images/tick.jpg" width="20" height="20" border="0" alt="Been Completed" >
<cfelse>
<img src="../images/cross.gif" width="20" height="20" border="0" alt="Not Completed" >
</cfif>
</td>
</tr>
</cfoutput>
</table>
    This topic has been closed for replies.

    2 replies

    Inspiring
    August 30, 2007
    If you put your query results into a flash grid, the users will be able to click on a column header and sort the results.
    Legend
    August 30, 2007
    Your whole ORDER BY section can be simplified using the following - and it's much easier to read & debug (I also removed the comma's, they were most likely the cause of your error):
    MattasticAuthor
    Inspiring
    August 31, 2007
    Thanks for your help
    Known Participant
    August 30, 2007
    is there an error?

    I am thinking the problem is with the comma in front of some of the order by fields....