I have the following code where I have a placeholder inside my saved content. I want to replace the placeholder value with an input field. I am able to do that successfully. However, I need to rename all of the input checkbox fields differently like 1,2,3,4, but without using the JSoup library because that is kind of overkill here. Is there anything else I can try?
<cfsavecontent variable="h">
<cfoutput>#mytable.h#</cfoutput>
</cfsavecontent>
<cfset h= Replace(h,"[Check Box]","<input type='checkbox' name='abc' id='abc'>","all")>
The above creates checkboxes with the same name, which I do not want.
This is ugly and kind of brute-force-ish, but it should give you an idea of one way you can approach this issue.
<!--- Whatever your h value comes from --->
<cfset h = "[Check Box] Some text. <br>
[Check Box] Some text. <br>
[Check Box] Some text. <br>
[Check Box] Some text. <br>
[Check Box] Some text. <br>
[Check Box] Some text. <br>
[Check Box] Some text. <br>
[Check Box] Some text. <br>
">
<!---
We replace the searched for text with a single character delimiter
so that we can count elements. We also append a nonsense character
to the string to make sure that if our searh string is the first
thing in the full string, it will still get replaced.
--->
<cfset changeDelims = replace('a' & h,"[Check Box]","|","All")>
<!---
Now we can count how many elements are in the main string. This
will tell us how many times we need to replace our substring.
--->
<cfset howMany = listLen(changeDelims,"|")-1>
<!---
Now loop through your string for the number of times we have that
substring and replace it.
--->
<cfloop from="1" to="#howMany#" index="i">
<cfset replaceStr = "<input type='checkbox' name='abc#i#' id='abc#i#'>")>
<cfset h = replace(h,"[Check Box]", replaceStr)>
</cfloop>
<!--- Which gives us.... ---->
<cfoutput>#h#</cfoutput>
Related
I need to create a list of country names within quotes and a comma at the end - except the last country name, like this:
(I'm using ColdFusion 10)
"Tuvalu",
"Uganda",
"Ukraine",
"United Arab Emirates",
"United Kingdom",
"Uruguay"
<cfquery name="query_names" datasource="MyDB">
select short_desc
from tbl_country
where NVL(short_desc,' ') <> ' '
order by short_desc
</cfquery>
<cfset TotalRec = "#query_names.Recordcount#">
<cfloop query="query_names">
<cfif query_names.Recordcount GT 271>
<cfoutput>
"#Trim(short_desc)#" & ","
</cfoutput>
<cfelse>
<cfoutput>
"#Trim(short_desc)#"
</cfoutput>
</cfif>
</cfloop>
This loop result in country names within quotes, but no comma. So my loop result in:
"Tuvalu"
"Uganda"
"Ukraine"
"United Arab Emirates"
"United Kingdom"
"Uruguay"
If you really need double quotes, it is probably simpler to append the quoted values to an array and convert it to a list at the end. The ArrayToList function automatically handles the commas for you:
<cfset names = []>
<cfloop query="query_names">
<cfset arrayAppend(names, '"'& short_desc & '"')>
</cfloop>
<cfoutput>#arrayToList(names)#</cfoutput>
Result:
"Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","Uruguay"
Side note, if single quotes, i.e., ' are acceptable, it is even simpler. Skip the looping and just use QuotedValueList():
<cfoutput>#quotedValueList(query_names.short_desc)#</cfoutput>
Result:
'Tuvalu','Uganda','Ukraine','United Arab Emirates','United Kingdom','Uruguay'
I hope listQualify() does the same in pretty easier way. isn't it???
<cfset myQry = queryNew("country","varchar",[["Tuvalu"],["Uganda"],["Ukraine"],["United Arab Emirates"],["United Kingdom"],["Uruguay"]])>
<cfdump var="#listQualify(valueList(myQry.country),'"')#" />
Also we can use quotedValueList() as Leigh mentioned, if we need single quoted list.
User1557856, you were so close. Your answer is actually good, but for one point. If you correct it, you will get what you want.
The reason why you obtained a list without commas is this:
<cfif query_names.Recordcount GT 271>
This condition is always false apparently. So only the <cfelse></cfif> part of the code is run. That is the part without commas, hence the result.
If you modify your code slightly, as follows, you will get the desired result:
<cfloop query="query_names">
<cfif query_names.currentRow LT query_names.Recordcount>
<cfoutput>
"#Trim(short_desc)#",
</cfoutput>
<cfelse>
<cfoutput>
"#Trim(short_desc)#"
</cfoutput>
</cfif>
</cfloop>
I have a word document with tables laid out to look like a form. I have placeholders like %firstName%, %lastName%, %birthdate%...etc.
When I use the replace() function, the %firstName%, %lastName%, %birthdate% and all of the other placeholder fields are replaced on the first and second page. After the second, nothing replaces. All the names of the placeholders on the 3rd and 4th pages are the same as the 1st and 2nd pages. I even copied and pasted the placeholder names and I've made sure there are no added spaced. Curious to know if anyone else has had this happen and can tell me what was done to fix it.
<cfset docPath = GetDirectoryFromPath(GetCurrentTemplatePath()) & "UserTemplate.rtf" />
<cflock name="UserTemp" type="exclusive" timeout="30">
<cfset rtf = FileRead(docPath) />
<cfquery name = "qUserFormData">
SELECT * FROM vUserFormData WHERE UserID = 3
</cfquery>
<cfset rtf = Replace(rtf,"%firstName%",#firstName#)/>
<cfset rtf = Replace(rtf,"%lastName%",#lastName#) />
<cfset rtf = Replace(rtf,"%birthday%",#birthday#) />
</cflock>
<cfheader name="content-disposition" value="filename=UserTemplate.doc" />
<cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>
There is a fourth (optional) parameter to the replace() method; scope.
Scope:
one: replaces the first occurrence (default)
all: replaces all occurrences
Notice that "one" is the default and that only replaces the first occurrence. Try adding that fourth parameter like this:
<cfset rtf = Replace(rtf,"%firstName%",firstName,"all") />
<cfset rtf = Replace(rtf,"%lastName%",lastName,"all") />
<cfset rtf = Replace(rtf,"%birthday%",birthday,"all") />
(The hash tags # are not necessary in this bit of code.)
Also be aware that the replace() method you are using is case sensitive.
I want to create database location records in mySQL. I have the following html string from a select box:
<cfset x='
<option value="1188">Aka Aka</option><option value="346">Ararimu</option><option value="293">Awhitu</option><option value="2851">Bombay</option><option value="865">Buckland</option>
'>
Rather than manually enter the records in the database, I'd like to strip out the html tags and end up with the following:
Aka Aka
Ararimu
Awhitu
Bombay
Buckland
Then I could do a simple loop based on line breaks and enter the data programatically. I can probably handle that part, but what I need to know is the simplest way to strip out the html to end up with the line break delimited list.
Here you go:
<cfset x='
<option value="1188">Aka Aka</option><option value="346">Ararimu</option><option value="293">Awhitu</option><option value="2851">Bombay</option><option value="865">Buckland</option>
'>
<cfset y = ListToArray(x, "</option>", "false", "true") />
<cfset z = ArrayNew(1) />
<cfloop array="#y#" index="name">
<cfif Trim(ListLast(name, ">")) is not "">
<cfset temp = ArrayAppend(z, ListLast(name, ">")) />
</cfif>
</cfloop>
<cfdump var="#z#" />
you have them in a 'z' array now, you can convert to list and add line break delimiters if you really want to.
I'm in Coldfusion 8. I have a table that is produced by a loop. Very complex code but I put some of it here:
<cfloop array = #qrep.getColumnList()# index = "col">
<cfset l = l + 1>
<cfif l EQ tindx - 1>
<cfset prevcol= col>
</cfif>
<cfif linefold GT 0>
<cfset lmod = i%linefold>
<cfelse>
<cfset lmod = 1>
</cfif>
<!--- printing detail --->
<cfif l LE m AND repdetail NEQ 'n'>
<td class = "repsubthead"> Subtotal:
<b>#qrep[col][currentrow]#</b></td>
</cfif>
<!--- printing totals only; row labels --->
<cfif repdetail EQ 'n' AND l EQ tindx >
<cfset frowarr[footrow] = qrep[col][currentrow]>
<cfset footrow_1 = footrow - 1>
<cfif footrow EQ 1>
<td style = "font-size: 13px" > #qrep[col][currentrow]#</td>
<cfelseif frowarr[footrow] NEQ frowarr[footrow_1] >
<td style = "font-size: 13px;"> #qrep[col]currentrow]#</td>
<cfelse>
<cfset testrow = footrow>
<td class = "repsubthead" style = "padding-top: 10px"> Total #qrep[prevcol] currentrow]# </td>
</cfif>
.... lots more before we get to end of loop
This part of the code prints out a row label for each row. Further in the program there is a similar loop to print out the value for the row. Everything is working fine except for one problem I can't trace. An extra row is being inserted in one spot, with no data in it. Part of the table is here:
State: CT
AVS 25.00
COMB 15.00
Email2010 15.00
REF 75.00
STRLST01 22.00
extra row inserted here, height much smaller than other rows
STRLST04 50.00
Total CT 202.00
I have copied this table to a Libre Office document and zoomed in on the bad row. It is definitely there, and it contains a blinking item that looks like this: '
I cannot delete this item from the row in Libre Office, although I am able to delete the entire row. The blinking thing disappears when I put my cursor in another row.
I have checked both STRLST01 and STRLST04 in my MySQL database, and they seem fine, with no anomalies. I cannot find anywhere in my code where I would be inserting an extra row (altho admittedly the code is very complicated).
Has anyone seen something like this? Does anyone have a clue what might be causing this?
Just a shot in the dark here... but try sanitizing your table content. For example:
#htmlEditFormat(qrep[col][currentrow])#
This would be to rule out that the TR in "STRLST01" isn't getting processed as <TR>. I've seen dynamic tables like this one go haywire because the content gets interpreted as HTML.
MC
What is the generated HTML? Looking at the actual generated output rather than the rendered output could help you find if it's bad data being dumped into another row or if it's a bug in your HTML generation routine.
If it's an odd special character in your data, the HTMLEditFormat() or XMLFormat() functions should find it and deal with it. Or at least make it easier to troubleshoot.
It's a bit of tricky question, however, my page for most rated bands displays the band logos in order of how high they have been rated. My only problem is i want to count through the records by using a cfloop from 1 to 10 and because it is split in to two columns, have one counting 1 through to 9 and the other 2 through to 10, each of them with steps of two.
Can anybody help me with this? If i've confused just mention it and ill try to clarify exactly what i mean.
<DIV class="community_middle">
<cfoutput query="top10MostRated">
<cfloop from="2" to="10" index="i" step="2">
<DIV class="communityContent">
#chr(i)#
<IMG src="logo/#top10MostRated.Logo#" alt="#top10MostRated.Name#" width="100%" height="100%"></IMG>
</DIV>
<BR/>
</cfloop>
</cfoutput>
</DIV>
If you're looking to do odd/even lists separately, then you can use the currentrow property of the query combined with the modulo operator (%) to work out if the row is odd or even:
<cfloop query="topBands>
<cfif topBands.currentRow % 2 = 1>
<!--- do your odd number output here --->
</cfif>
</cfloop>
<cfloop query="topBands>
<cfif topBands.currentRow % 2 = 0>
<!--- do your even number output here --->
</cfif>
</cfloop>
I think these answers address your side-by-side part of your question but does not explain the "same image" issue. Their code is written correctly but does not explain the reason.
Your code:
<IMG src="logo/#top10MostRated.Logo#"
alt="#top10MostRated.Name#"
width="100%" height="100%"></IMG>
... would be fine if you were only inside a <cfloop query = "top10MostRated"> or <cfoutput query = "top10MostRated"> block. The reason is because inside these types of blocks CF is smart enough to know you want the data for the current row. It would be the same as:
<IMG src="logo/#top10MostRated.Logo[top10MostRated.currentRow]#"
alt="#top10MostRated.Name[top10MostRated.currentRow]#"
width="100%" height="100%" />
Because you're nesting the to/from cfloop inside a <cfoutput query = ""> block, you are getting unexpected results. Your existing code is always asking for the record provided by your outer loop. Hence you see the same image 5 times. (using any of the fine examples provided will help you get out of this) but, you can remove the query from your cfoutput and simply ask CF to show you the value for the correct row in your loop using your index (you set your index to "i") so the below would show you the image that corresponds to your loop.
<IMG src="logo/#top10MostRated.Logo[i]#"
alt="#top10MostRated.Name[i]#"
width="100%" height="100%" />
It sounds like what you'd like to get is a collection of even-numbered records and a collection of odd-numbered records. In Coldfusion 10 or Railo 4, you can use groupBy() from Underscore.cfc to split up your query result into manageable sub-sets, like so:
_ = new Underscore();// instantiate the library
groupedBands = _.groupBy(topBands, function (val, index) {
return index % 2 ? "odd" : "even";
});
This returns a struct with two elements odd and even, each containing an array of records which are odd or even. Example result:
{
odd: [{name: "Band one"}, {name: "Band three"}],
even: [{name: "Band two"}, {name: "Band four"}]
}
Splitting your results into logical sub-sets makes the code more readable:
<cfoutput>
<cfloop from="1" to="5" index="i">
<div class="left">#groupedBands.odd[i].name#</div>
<div class="right">#groupedBands.even[i].name#</div>
</cfloop>
</cfoutput>
You'll also be able to use those sub-sets in other places on your page if you need to.
Note: I wrote Underscore.cfc
Ben Nadel has a post exactly for this. Link here
A breakdown for this is
<cfloop query="top10MostRated">
<cfif top10MostRated.CurrentRow MOD 2>
<!--- Add to the "odd list" --->
<cfelse>
<!--- Add the record to the "even list" --->
</cfif>
</cfloop>
Then you'll have 2 lists oddList and evenList. Then it's just a matter of displaying them.
I'd do it a different way. The objective is to have records 1 and 2 side by side and I don't see that in #barnyr's answer.
<cfoutput>
<cfloop from="2" to="topbands.recordcount + 1" index = "i" step="2">
#topbands.fieldname[i-1]#
<cfif i lte topbands.recordcount>
#topbands.fieldname[i]# <br />
</cfif>
</cfloop>
</cfoutput>