Fill spaces with dash in ColdFusion - coldfusion

I need to create a CFM page I that will have hyperlinks that are built from data from a database. For example, my database contains a column "Title" and a column "Subject. From "Title" and "Subject" I will create a hyperlink (because all of the pages follow the same scheme: This-is-the-title-this-is-the-subject.html
So the issue is that I need to enter "-" in the spaces. Because right now my CFOUTPUT is just this: This is the title this is the subject.html
Is there a simple way to convert this to This-is-the-title-this-is-the-subject.html?

i think this will solve your problem
<cfset pageName = "This is the title this is the subject.html" />
<cfset seoPageName = replace(pageName," ","-","all") />

I like to use listChangeDelims() for these tasks since you can efficiently add additional chars to change.
seo = listChangeDelims(original, '-', ' ,\' );

Related

Getting the Value of the Query to Populate Form Fields

I had a Query Where i am Getting 14 records and i need to Populate this records in 4 form fields, they are coming as
select id, name, value from mytable
check the attached Screenshot.
I cannot do cfoutput query as it will loop the form fields 14 times,
i tried something like
queryname.name[currentow], but it not worked, checked this URL of cflib http://cflib.org/udf/queryGetRow
but that did not seems to get work. need to populate the value of the name column in the form field
need to know what is the right way
From this comment only,
"honestl my friend, i think i got confused what to ask; I will try to explain again: Auume i have two columns in my table {name & Value}. It has four rows. Now i have to populate the form fields of the rows 1 to 4, which contains the the details from the value column of the table"
I would do this to build my form:
<cfquery name="data" datasource="whatever">
select name, value
from mytable
</cfquery>
<form action="somewhere.cfm" method="post">
<cfoutput query="data">
<input name="name_#name# value="#value#">
</cfoutput>
</form>
Processing the form is outside the scope of the question so I won't do it. However, I will mention some potential problems. If name is a text field, and it's values contain spaces, puncuation marks, etc, your page will crash due to an invalid variable name. If your table has an id field that is either uuid or numeric, that's a better field to use.
Ok, I did it, Here is if someone encounters this, i may not be able to correctly tell what is required. So here is my Solution:
<cfset rsSettings= initial.getSettings()>
<cfloop array="#rsSettings#" index="k">
<cfset settings[k['name']] = k['value']>
</cfloop>
My Query is returning me an array, Using ben's post
http://www.bennadel.com/blog/124-Ask-Ben-Converting-a-Query-to-an-Array.htm
so to fill the form i used like this and it worked
<input type="text" name="pri" class="text" value="<cfoutput>#settings['pri']#</cfoutput>" />
if you guys have better solution, please share

Is this a ColdFusion Bug in cfscript?

This is a valid query to return emails surrounded by single quotes from the users table.
SELECT '''' +email + '''' as email
FROM users
where fname = #fname
Yet when I try to do this in a cfscript (cf9) query like this :
var q = new Query(datasource="warewithal");
q.setSQL("SELECT '''' +email + '''' as email
FROM users where firstName= :firstName ");
q.addParam(name="firstName", value=trim(firstName), cfsqltype="cf_sql_varchar");
what I end up with is
Email
+email+
when I expected (and get by running the query in an analyzer)
Email
'bozo#clowns.com'
the cfscript is doing a perserveSingleQuote and not letting me add the single quote to the output.
Is this a bug or what am I doing wrong?
To answer the question in your headline: no, this is not a bug in CFScript. What you are demonstrating has nothing to do with CFScript at all.
However to answer the question you mean to be asking: yes, you have found a bug in Query.cfc.
Here's some code that demonstrates the bug (and demonstrates that it has nothing to do with CFScript, and everything to do with Query.cfc).
This code works fine:
<cfset query = new Query(datasource="scratch_mssql")>
<cfset query.setSql("
SELECT '''' + email + '''' as email
FROM users
WHERE firstName = '#firstName#'
")>
<cfset emailAddresses = query.execute().getResult()>
<cfdump var="#emailAddresses#">
Note that I have hard-coded the filter value into the SQL string. Yuck.
This code errors:
<cfset query = new Query(datasource="scratch_mssql")>
<cfset query.setSql("
SELECT '''' + email + '''' as email
FROM users
WHERE firstName = :firstname
")>
<cfset query.addParam(name="firstname", value=firstName, cfsqltype="CF_SQL_VARCHAR")>
<cfset emailAddresses = query.execute().getResult()>
<cfdump var="#emailAddresses#">
The error for me is:
[Macromedia][SQLServer JDBC Driver][SQLServer]An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements,
look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
FYI: The SQL that CF had passed to the DB was:
SELECT '' '' + email + '' '' as email FROM users WHERE firstName = (param 1)
To prove that it's caused by ColdFusion mishandling your single quotes, this works:
<cfset query = new Query(datasource="scratch_mssql")>
<cfset query.setSql("
SELECT email as email
FROM users
WHERE firstName = :firstname
")>
<cfset query.addParam(name="firstname", value=firstName, cfsqltype="CF_SQL_VARCHAR")>
<cfset emailAddresses = query.execute().getResult()>
<cfdump var="#emailAddresses#">
So I'd raise a bug for this if I was you. Note: it's the same on CF9.0.2 and CF10.0.7
But, equally... if I was you I'd not be putting those quotes in there anyhow. Unless there's a very good reason, stick 'em in when you're doing the display, not when you're doing the data processing. I presume they're there for display purposes?
For what it's worth, I just got bit by this issue was well. What seems to work is a simple fix to your Query.cfc object (/[YOUR-CF-INSTALL]/CustomTags/com/adobe/coldfusion/Query.cfc). In the replaceDelimsWithMarkers() function, update this line (LN 341 for me)
newSql = newSql & SINGLEQUOTE & sqlArray[i] & SINGLEQUOTE & " ";
by removing the quoted space at the end, so it is:
newSql = newSql & SINGLEQUOTE & sqlArray[i] & SINGLEQUOTE & "";
Then it works great. I was looking up users, and needed to find one where last name is "O'Neill". No matter what I did I was getting "O' 'Neill" which as you state fails.
Can you think of any cases where this change/fix would have a negative impact? I can't off the top of my head, but for now we are going to copy the query.cfc file, rename it and use it as a custom tag ONLY when we need to run queries where we suspect extra apostrophe's might be used. The only difference will be this one little change.

How to access query column with multiple words?

I'm using a cfspreadsheet read to read a sheet into a query object.
<cfspreadsheet action="read" src="TestExcel.xls" sheet="1" query="spreadsheetData" headerrow="1" excludeHeaderRow="true">
The problem is, some of the headers contain more than one word. So I end up with a query a bit like this:
ID Name Start Date End Date
3 Test 1/1/2009 1/1/2013
17 Test 2 11/11/2010 11/11/2012
If I try to access one of the columns that have a space in the column name, I get an error.
<cfoutput query="spreadsheetData">
#start date#
</cfoutput>
I've tried #[start date]# as well, but that didn't work. I cannot control the format of the excel sheet that I receive. Is there any way to access the multiple-worded-header columns?
When using bracket notation the contents must end up as a string, so:
<cfoutput query="spreadsheetData">
#spreadsheetData['start date'][CurrentRow]#
</cfoutput>
If you don't use quotes, you are passing in a variable, which is done like so:
<cfset ColumnName = 'start date' />
<cfoutput query="spreadsheetData">
#spreadsheetData[ColumnName][CurrentRow]#
</cfoutput>
Note that you must use the query name before the brackets - if you simply write [ColumnName] then this is inline array creation notation, not accessing the variable.
Also, if using this outside of a query loop (i.e. not within cfoutput/cfloop with query attribute), you also need to scope the CurrentRow variable, i.e.
spreadsheetData[ColumnName][spreadsheetData.CurrentRow]
(or provide your own explicit number/variable).
As Leigh notes below, for cfspreadsheet-specific behaviour, you can also specify the columnnames attribute, to rename the column to something directly accessible, e.g.
<cfspreadsheet query=".." columnNames="Foo,Bar,StartDate,Etcetera" ..>

cfspreadsheet escape comma in comma separated row insert

The function for adding a row to a coldfusion spreadsheet is SpreadsheetAddrow which accepts data as "A comma delimited list of cell entries, one per column."
Some of my data has commas within it. How do I escape the commas within my data without escaping the commas in the list?
I am currently creating an array with the contents of the row, then converting it to a list to add to the spreadsheet:
<cfset row = ArrayNew(1)>
<cfloop list="#structKeyList(setRecord.columns)#" index="key">
<cfset ArrayAppend(row, "#Evaluate(key)#")>
</cfloop>
<cfset spreadsheetAddRow(xlsObj, "#ArrayToList(row)#")>
Looks like the ability to specify a different delimiter is not supported yet. Since you are already looping, you may as well skip the array and use SpreadsheetSetCellValue instead. You should be able to eliminate the evaluate() as well.
<cfset cols = structKeyArray(yourStruct) >
<cfloop from="1" to="#arrayLen(cols)#" index="c">
<cfset SpreadsheetSetCellValue(xlsObj, yourStruct[ cols[c] ], lastRow, c)>
</cfloop>
<cfset lastRow++>
...
Update: However, if the base object is a query, not a structure, then it is more efficient to use CfSimplicity's suggestion of SpreadSheetAddRows.
If the data you want to add to the sheet is in a query object (recordset) then the simplest solution is to use SpreadSheetAddRows(), (as opposed to SpreadSheetAddRow - singular).
<cfset SpreadSheetAddRows( xlsObj,query )>
The sheet columns are mapped from the query columns, so commas in the data won't matter.
Even if the data is in another format or you are only adding a single row, converting it to a query object is an effective way of getting round the issue, see http://cfsimplicity.com/30/workaround-for-spreadsheetaddrow-limitation-when-column-values-contain-commas
You can replace the commas with the character "#130;" (see the source: https://www.petefreitag.com/cheatsheets/ascii-codes/), which looks the same as comma but doesn't create any problems.

How can I disable href link in cfgrid cells for blank rows?

I have the following cfgrid:
<cfgrid format="html" name="grid" pagesize="10" selectmode="row" striperows="yes"
bind="cfc:data.getData({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})">
<cfgridcolumn name="link" header="Link" href="link.cfm" hrefkey="link_id">
</cfgrid>
Since pagesize is 10, I'll have some blank rows if I have less than 10 rows in the grid. For those blank row I'd have a '-' sign in place of the usual data, and the hrefkey will be null instead of the usual link_id. Is there a way to disable the href for blank rows? Or if there's a way to capture the null value with javascript?
You can loop through the a hrefs within the table and find all the hrefs that contain the '-' and change this to something else or remove the href completely. This can be done in JS but ideally I would try and do this before hand (i.e in CF) as there will be an overhead using JS to do this.