Moving Categories Up and Down

Moving Categories Up and Down

Page 1 of 1

4 Replies - 3141 Views - Last Post: 31 May 2005 - 03:29 AM Rate Topic: -----

#1 carend  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-May 05

Moving Categories Up and Down

Posted 04 May 2005 - 01:44 PM

Greetings all! We have a concept that I am not sure how to implement but here goes.

We have a web store that is based on categories. The information is all pulled from SQL Server and using cfquery you can only sort the categories by name or by primary key id or date created.

What they want to be able to do is have a little arrow on an admin tool next to the category name where you can move it up or down and have the categories ordered by them using this tool.

I am assuming I will add a column to the table the categories are pulled from to populate it by "weight" of the object. This has been done already in phpnuke codes but how should I start to do it for coldfusion?

Thank you in advance for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Moving Categories Up and Down

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Moving Categories Up and Down

Posted 05 May 2005 - 03:37 PM

I had some code all setup for you, but I just lost it when my computer restarted. Anyway, you're gonna wanna setup a 'sort' field in the database, make it numeric. On your page you'll add Up and Down links which have the primary key ID of the category, and sort + 1 for up and sort - 1 for down. You'll also want to pass a variable for 'up' or 'down' so you know whether to update the record above or below.

I would suggest creating a function that accepts unique ID, direction, and position. Then you'll run 2 update commands against your database, update the unique ID record to reflect the new sort position, then update the record that was occupying that position with it's new sort position, basically a swap of the sort values. Let me know if you want me to code it out, we use the same concept for the customize layout page here at dream.in.code (it's in PHP).

The other way you could do this is to store the sort order in an array and just store the array as a list in the database or a persistent variable. You would then use ArraySwap() to swap the position of the unique IDs in the array and loop through the array when you display your categories...

Hope that helps!!! Lemme know if you have any other questions... and welcome to dream.in.code!
Was This Post Helpful? 0
  • +
  • -

#3 carend  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-May 05

Re: Moving Categories Up and Down

Post icon  Posted 06 May 2005 - 07:13 AM

Thanks for the quick reply! That would be awesome if you could code it out for me. I was going towards that idea of the +1 and -1 but wasnt sure where to start. I will start setting up the database.

Thank you very much for the code!@!

//Now my favorite forum//
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Moving Categories Up and Down

Posted 06 May 2005 - 11:09 AM

No problem, normally we don't provide code, and this is just the swap function, but since this doesn't appear to be for a homework assignment... here ya go:

	<!--- Change sort order by swapping --->
	<cffunction name="SwapSort" access="public" output="false">
  <!--- Pass category id, direction (up/down), and new sort position --->
  <cfargument name="catid" type="numeric" required="true">
  <cfargument name="direction" type="string" required="true">
  <cfargument name="newposition" type="numeric" required="true">
  
  <!--- Decide what the new position of the category we are swapping with will be --->
  <cfif arguments.direction IS "up">
 	 <cfset swap = arguments.newposition - 1>
  <cfelseif arguments.direction IS "down">
 	 <cfset swap = arguments.newposition + 1>
  </cfif>
  
  <!--- Here's the swap --->
  <cftransaction>
 	 <!--- Move the category that's currently in that spot up or down --->
 	 <cfquery name="qSwap1" datasource="YOURDATASOURCE">
 	 UPDATE categories SET sort = '#swap#' WHERE sort = #arguments.newposition#
 	 </cfquery>
	
 	 <!--- Move the category that was clicked to it's new sort position  --->
 	 <cfquery name="qSwap2" datasource="YOURDATASOURCE">
 	 UPDATE categories SET sort = '#arguments.newposition#' WHERE id = #arguments.catid#
 	 </cfquery>
  </cftransaction>
  
	</cffunction>


Let me know if you need help implementing further.
Was This Post Helpful? 0
  • +
  • -

#5 maxibou69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-May 05

Re: Moving Categories Up and Down

Posted 31 May 2005 - 03:29 AM

Hello,
I'm new on this forum but I'm also new in Coldfusion development. I've the same problem as "carend" but I the answer given do not work for me.
I'm sute it's because I don't understand everything....
So my question is : what is the relation between the code you gave and the form that display the value from the database ?
I've made two files : one for the form and one for the action (I put your code in this last file). Here is my code :

FILE 1 :
--------------------------------------------------------------------
<script src="../resources/moveselected.js"></script>
<cfquery name="qGetfam" datasource="case">
SELECT fam_id, fam_name, fam_order
FROM family
ORDER BY fam_order ASC
</cfquery>

<form name="form" action="<cfoutput>modify_order_action.cfm</cfoutput>" method="post">

<select name="selectName" size="12">
<cfoutput query="qgetfam">
<option value="#qGetfam.fam_order#">#urldecode(qGetfam.fam_name)#
</option>
</cfoutput>
</select>

<br>

<input type="button" value="up" onclick="moveSelected(this.form.selectName, false);" />

<input type="button" value="down" onclick="moveSelected(this.form.selectName, true);" />

<input type="submit" name="Submit" value="Update">

</form>

FILE 2 :
--------------------------------------------------------------------
<!--- Change sort order by swapping --->
<cffunction name="SwapSort" access="public" output="false">
<!--- Pass category id, direction (up/down), and new sort position --->
<cfargument name="fam_order" type="numeric" required="true">
<cfargument name="direction" type="string" required="true">
<cfargument name="selectName" type="numeric" required="true">

<!--- Decide what the new position of the category we are swapping with will be --->
<cfif arguments.direction IS "up">
<cfset swap = arguments.selectName - 1>
<cfelseif arguments.direction IS "down">
<cfset swap = arguments.selectName + 1>
</cfif>

<!--- Here's the swap --->
<cftransaction>
<!--- Move the category that's currently in that spot up or down --->
<cfquery name="qSwap1" datasource="case">
UPDATE family SET fam_order = '#swap#' WHERE sort = #arguments.selectName#
</cfquery>

<!--- Move the category that was clicked to it's new sort position --->
<cfquery name="qSwap2" datasource="case">
UPDATE family SET fam_order = '#arguments.selectName#' WHERE fam_order = #arguments.fam_order#
</cfquery>
</cftransaction>

</cffunction>
--------------------------------------------------------------------


Thank you very much for help,
MB
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1