Skip to main content
Known Participant
March 22, 2010
Question

mixing next n records with cfswitch order by problem

  • March 22, 2010
  • 2 replies
  • 1166 views

Hello;

I'm trying to make 2 types of page navigation work together. both work fine independently, but when I put my next n record code together with my change order by code, when you hit the next button, it goes back to the set default for the order by. I know I need to change some code logic in my scripts that run the next n records, but I'm not sure what I need to change in it to make it all work together properly. I am attaching the code, in it's rawest form, but all code is here for both features.

<cfparam name="sort" default="1">

<cfquery name="getMerch" datasource="#APPLICATION.dataSource#">
SELECT merchID, MerchName, MerchDescription, MerchPrice, MYFile, CategoryID
FROM Merchandise
WHERE CategoryID = 2
ORDER BY
<cfswitch expression="#sort#">
<cfcase value="1">MerchName</cfcase>
<cfcase value="2">MerchPrice</cfcase>
</cfswitch>
</cfquery>
<cfset rowsPerPage = 3>
<cfparam name="URL.startRow" default="1" type="numeric">
<cfset totalRows = getMerch.recordCount>
<cfset endRow = min(URL.startRow + rowsPerPage - 1, totalRows)>
<cfset startRowNext = endRow + 1>
<cfset startRowBack = URL.startRow - rowsPerPage>

<cfoutput>

<!--- changes the order by --->

<cfif sort is 1>

<a href="#CGI.script_name#?sort=#IIF(sort is 1, '2', '1')#" class="pre2Nav">Order By Price</a>

<cfelse>

<a href="#CGI.script_name#?sort=#IIF(sort is 2, '1', '2')#" class="pre2Nav">Order By Product</a>

</cfif>

<!--- my next n buttons --->

<cfif startRowBack GT 0>

<a href="#CGI.script_name#?startRow=#startRowBack#" class="pre2Nav">< Back</a>

</cfif>

<cfif startRowNext lte totalRows>
<a href="#CGI.script_name#?startRow=#startRowNext#" class="pre2Nav">More ></a>

</cfif>

</cfoutput>

<cfloop query="getMerch" startRow="#URL.startRow#" endRow="#endRow#">

<cfoutput>

All my output goes here

</cfoutput></cfloop>

Can anyone help me get this to work together? is it possible? and more importantly, how hard is it going to be to do this?

Thank you.

This topic has been closed for replies.

2 replies

Inspiring
March 22, 2010

The problem is either that the variable is not what you expect, or that your switch case code is wrong.  Try this

<cfdump var = "isDefined('sort')>

<br>

<cfparam name="sort" default="1">

<cfdump var = "sort is  #sort#">

<br>

<cfswitch expression="#sort#">
<cfcase value="1">MerchName</cfcase>
<cfcase value="2">MerchPrice</cfcase>
</cfswitch>

Do you get the expected results?

Known Participant
March 22, 2010

It works fine, I get the proper results, accept when I hit next, then it goes back to the default #sort# that is 1 for the next set of records.

I believe the next n code needs to have stipulations in it for the #sort# setting, but I'm not sure how to approach that.

Inspiring
March 22, 2010

What did this give you when you hit next?

<cfdump var = "isDefined('sort')>

Inspiring
March 22, 2010

What is the value of sort when you look for the next n records?

Known Participant
March 22, 2010

the value is changed to 2 if you click the link, that's when next n record fail. when it's set to the default, which is 1, it's fine.