Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort records???

Status
Not open for further replies.

johk02

Programmer
Aug 20, 2003
169
AU
I must be having a "brain fart"!!!
I output a list of records and want to be able to sort them.
What I basically am after is to be able to have an <input> (or even better have a <select> with 1 to "number of records") next to each record and be able to enter a number that sets the sort order. Where I am stuck is HOW do I "link" that <input> field to a record??????????

Thanks
Jonas
 
give example of the form field html

give example of query

give example of how you "output a list of records"

r937.com | rudy.ca
 
Why would you want the user to enter a number to sort the record? If you do it this way you'd also have to check to make the user did not enter (or select) the same number twice. And, that the user did not skip a number. Kind of seems more work then necessary.

Like r937 suggested, post some code or query so we know what you're doing. I did something similar to this for work, but instead I output the records in a select multi-box and let the user choose any record then allow them to push up or down for sorting.

____________________________________
Just Imagine.
 
Rudy and GUJUm0deL - thanks for responding,
Say for a example that you have a links section and you want to order/sort them other than by name.

I guess it is similar to have a "move up or down" function which is what I have now but "someone" is not happy with how it works as they on a regular basis are shifting the order of all the links. (to let everyone be at the top of the list).
My idea was to have a dropdown showing 1 - xx (xx being number of records) and then it is easy for them to select a new order of the link (record).
Please find some code below.
Thanks
jonas

Code:
<cfquery name="GetLinks" datasource="#request.SiteDSN#">
SELECT tbl.LinksName,
	tbl.LinksLive,
	tbl.LinksID, 
	tbl.LinksOrder
FROM tbl
ORDER BY tbl.LinksOrder, tbl.LinksName
</cfquery>
      
<table width="219" border="0" align="left" cellpadding="0" cellspacing="0">

  <form action="action_Link.cfm" method="post" name="SetOrder" id="SetOrder" >
    <tr>
      <td width="155"><h3>Name</h3></td>
      <td><h3 align="center">Order</h3></td>
    </tr>
    <cfoutput query="GetLinks">

    <tr style="line-height:11px;vertical-align:middle" BGCOLOR="###IIF(GetLinks.currentrow MOD 2, DE('E6E6E6'), DE('C0C0C0'))#">
      <td>#GetLinks.LinksName#</td>
      <td><div align="center">
        <input name="order" type="text" id="order" size="4" />
      </div></td>
    </tr>
	</cfoutput>
    <tr>
      <td valign="top">&nbsp;</td>
      <td width="64"><input name="Update Order" type="button" value="UpdateOrder" /></td>
    </tr>
  </form>
</table>
 
ah, the old "linkorder" column -- yes, that's a good technique, as it allows the users to set their own sequencing

i'm afraid what you need to do is read in the entire list from the form, then loop through them, updating the matching table rows one at a time

this means you need two form fields -- the primary key and (new) linkorder -- and they have to be repeated on the form for all rows

make sense?

by the way, you can "hide" this underneath your "move up / move down" interface, which is much better from a user perspective than forcing them to enter linkorder numbers (which can get mighty confusing if the user wants to re-arrange some entries in a non-trivial way)

r937.com | rudy.ca
 
I was hoping that I had missed something and didn't need to go to that extent :)


Cheers

Jonas
 
johk02, Hmmm, I still think the other way is better, but I guess I see the point in doing it this way.

Take a look at this, I did this a while back but it might help you. I recomplied this pretty quick, so there might be some typos or whatnot, but it has the logic you need.

Test this out and see what happens.

Code:
<cfquery name="GetLinks" datasource="#request.SiteDSN#">
	SELECT 	 tbl.LinksName, tbl.LinksLive, tbl.LinksID, tbl.LinksOrder
	FROM 	 tbl
	ORDER BY tbl.LinksOrder, tbl.LinksName
</cfquery>     
	 
<!--- CHECK TO SEE IF  'prmUpdateSortOrder' IS PRESENT IN THE FORM --->
<cfif IsDefined("Form.prmUpdateSortOrder")>
	<cfloop query="GetLinks">
		<!--- LOOP THROUGH THE  --->
		<cfquery datasource="#DB#">
			UPDATE tbl
			SET LinksOrder = #Evaluate("FORM.prmSortOrder#GetLinks.LinksID#")#
			WHERE intPrimaryID = #form.prmID# AND LinksID = #form.LinksID#
		</cfquery>
	</cfloop>
	<cfquery datasource="#DB#" name="GetLinks">
		RE-QUERY THE DATABASE TO GET BACK THE NEW ORDER
	</cfquery>
</cfif>


<table border=0 cellpadding=4 cellspacing=0 width="350">
	<tr>
		<td>Name</td>
		<td>Sort Order</td>
	</tr>
	
	<!--- FORM ACTION SHOULD SUBMIT TO ITSELF! --->
	<form action="OrderMe.cfm" method="post">
		<cfoutput query="GetLinks">
		<tr>
			<td>#GetLinks.LinksName#</td>
			<td>
				<input type="Text" name="prmSortOrder#GetLinks.LinksID#" value="#GetLinks.LinksOrder#" size="5" maxlength="5">
				<input type="Hidden" name="prmID" value="#prmID#">
			</td>
		</tr>
		</cfoutput>
		
		<tr><td colspan="3"><input type="Submit" name="Submit" value="Save Changes"></td></tr>
	</form>
</table>

____________________________________
Just Imagine.
 
Ooo, that was a typo.

It should be: isdefined("FORM.Submit")

This is to make sure the UPDATE statement only runs IF the submit buuton is defined (as in clicked), :)

/It's Wednesday but why does it feel like Monday?

____________________________________
Just Imagine.
 
/It's Wednesday but why does it feel like Monday?

Your at least the fourth person that has siad that today! friday will be a nice early suprise then, I hope!

Kevin

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!
 
Kevin, not sure where you are but i'm in NYC and I just got back from a 4-day weekend (Tuesday was 4th of July - US's independance day) so for me it feels like a Monday, lol. Can't wait till Friday...woo-hoo

____________________________________
Just Imagine.
 
Thanks guys for all your input.
I will try to get on top of this next week.
Will let you know how it goes.

Cheers
Jonas
 
I don't know why I didn't think of this before but I can use CFGIRD. It will make it pretty easy for updating the sort order.
I haven't played around with much so I will let you know how it goes.

GUJUm0deL:
I will have a look at your example later on as it will be good practise.


Jonas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top