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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Eliminating last comma in my dynamic output 4

Status
Not open for further replies.

evergreean43

Technical User
Joined
May 25, 2006
Messages
165
Location
US
My current output of dynamic names in my variable (called dynNames) has a comma at the end.

I need to eliminate the last comma:
Jones, Smith, Baker, Johnson,

to look like this:

Jones, Smith, Baker, Johnson


My attempt to eliminate the last comma doesnt work because it eliminates all the commas:
Code:
<cfset myNames = REReplace(dynNames, ",$" "", "All")>

<cfoutput>#myNames</cfoutput>

Output from my bad attempt:
Jones Smith Baker Johnson

Please advise.

 
Remove the "ALL" statement.
<cfset myNames = REReplace(dynNames, ",$", "")>

This was my test:
Code:
<cfset one = "Jones, Smith, Baker, Johnson,">
<cfset myNames = REReplace(one, ",$", "")>
<cfoutput>
#one#<br>
#myNames#
</cfoutput>

The display comes as you want.

____________________________________
Just Imagine.
 
Thanks,

It still gets rid of all my commas because
it actually outputs from a nested output.
I have the names output that shows like this from the database:
Jones Carson Richards

But I put in a comma to show better. The problem is I dont want the comma to show after the last name.

Code:
<cfoutput query="otherquery" group="id">
#id#<br>
<cfoutput>
<cfset dynNames = "#names#,">
<cfset myNames = REReplace(dynNames, ",$", "")>
#myNames#
</cfoutput>
<cfoutput>
 
Thanks,

I think my problem is this is in a nested cfoutput.

I am taking the data which is in variable names that looks like this as an output:
Jones Rivers Baker Johnson

and putting comma delimeters on them like this:
Code:
<cfset dynNames = "#names#,">
so my output will have comma delimeters. The problem is I dont want a comma at the end which is what is happening:
Jones, Rivers, Baker, Johnson,

I tried all suggestions in this thread and all of them knock off all the commas but I only need the last comma knocked off. I assume I have an output issue.
Should I do this another way? If so please advise.
 
if this is in a loop/output query you can also do

<cfset dynNames = ''>

<cfloop ...> OR <cfoutput query="..">
<cfset dynNames = dynNames&','&#names#>
</cfloop> / </cfoutput>


hope it helps...

 
Thanks, this is close:

Code:
<cfset dynNames = ''>

<cfoutput query="..">
   <cfset dynNames = dynNames&','&#names#>
</cfoutput>

It outputs:
,Jones, Rogers, Baker

How do I get rid of first comma?
 
oops. my bad
<cfset dynNames = ''>

<cfoutput query="..">
<cfif ListLen(dynNames) EQ 0>
<cfset dynNames = dynNames&#names#>
<Cfelse>
<cfset dynNames = dynNames&','&#names#>
</cfif>
</cfoutput>


 
Thanks, the output still eliminates all my commas giving me this: Jones Carson Baker Smith
It must be something for how I have my nested output which I need.
Code:
<cfoutput query="otherquery" group="id">
#id#<br>
<cfoutput>
<cfset dynNames = ''>
   <cfif ListLen(dynNames) EQ 0>
       <cfset dynNames = dynNames&#names#>
   <Cfelse>
       <cfset dynNames = dynNames&','&#names#>
   </cfif>
</cfoutput>
<cfoutput>
 
ValueList(myquery.dynNames) outputs invalid data names in my output:


Code:
<cfoutput query="otherquery" group="id">
#id#<br>
<cfoutput>
<cfset dynNames = ValueList(otherquery.names)>
#dynNames#
</cfoutput>
</cfoutput>
 
ValueList operates on the entire column, so you should not put in inside a CFOUTPUT loop

do this instead --

<cfset dynNames = ValueList(otherquery.names)>
<cfoutput>
#dynNames#
</cfoutput>


r937.com | rudy.ca
 
Thanks I tried that and it still gives invalid data where it duplicates the names.

Code:
<!--- Also tried putting it here:  <cfset dynNames = ValueList(otherquery.names)>  --->
<cfoutput query="otherquery" group="id">
#id#<br>
<cfset dynNames = ValueList(otherquery.names)>
<cfoutput>
#dynNames#
</cfoutput>
</cfoutput>
 
invalid data? duplicates names?

what do you get when you run this --
Code:
<cfquery name="otherquery" datasource="#foo#">
 select [b]names[/b] from  ...
</cfquery>

<cfset dynNames = ValueList(otherquery.names)>  

<cfoutput>
#dynNames#
</cfoutput>
you don't by any chance have comma-delimited names stored in the names column, do you?

r937.com | rudy.ca
 
If I run your example it will show different data so I assume it is something unique to my query.

No I dont have comma delimited names in the names column.

I appreciate all the input and I assume I will have to make my output with just spaces in between the names to solve my problem?

Code:
<cfoutput query="otherquery" group="id">
#id#<br>
<cfoutput>
<cfset dynNames = "#names# ">
#dynNames#
</cfoutput>
</cfoutput>

 
okay, well, i don't understand what you're doing, so good luck with your "other data"

r937.com | rudy.ca
 
can you do a favor and try this...

<cfoutput query="otherquery" group="id">
#id#<br>
<!--- this used to be reseting to '' after every name, it was in the loop of the nested cfoutput
<cfset dynNames = ''>

<cfoutput>
<cfif ListLen(dynNames) EQ 0>
<!--- we know cynNames is '', so no reason for dynNames&names --->
<cfset dynNames = names>
<cfelse>
<!--- working with a list, use a list function --->
<cfset dynNames = listAppend(dynNames,names)>
</cfif>
</cfoutput>

<cfoutput>

valuelist() won't work because of the CFOUTPUT with group... he wants a list of names PER id. valuelist give the ENTIRE list of names to EVERY id.



Kevin

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!
 
valuelist() won't work because of the CFOUTPUT with group... he wants a list of names PER id. valuelist give the ENTIRE list of names to EVERY id.
aha, okay, my bad, i guess i should've figured that out, but i didn't, sorry


r937.com | rudy.ca
 
:)

yeah, it's been a rough month for me

old computer died, had to get a new one

had to upgrade myself on all sorts of software

on the plus side, i was finally able to install cf on my own machine, and actually hook it up to a local mysql database so that i can test cf queries...

(i had never succeeded in installing cf in 8 years of using it -- i had always just uploaded my .cfm files to my host)

also, being forced to set up a new machine for my own development purposes inevitably meant that i had to find some new software to replace things i had been using that you can't get anymore, such as the free version of mysql-front, which i very joyfully was able to replace with Heidi SQL (a spinoff of mysql-front) -- if you haven't tried it, it's way better than phpmyadmin)

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top