Adding Select All to dropdown - sql

This is my query:
<cfquery name="qry" datasource="#variables.staffDs#">
SELECT
firstname + ' ' + surname name, userid, mobileno, extension
FROM
currentstaff
WHERE
(mobileno IS NOT NULL OR
Left(LTrim(extension), 2) = '07')
</cfquery>
This is my ColdFusion dropdown box:
<select name="to" id="to">
<option value=""> -- Select -- </option>
<option style="font-weight: bold" value="">Send to All</option>
<cfoutput query="people">
<option value="#qry.userid#">#qry.name#</option>
</cfoutput>
</select>
The names of all people are displayed in the dropdown box list.
What I'd like to do is add an option at the top of the dropdown box to 'Sent to All' to select all users and I'm not sure how to do this.
I'd like to incorporate any extra SQL I might need into my current query, if this is necessary.

you just need to change like this if possible , just add
Select 'Select All'
union
to your query , final code like
Select 'Select All','','',''
union
SELECT
firstname + ' ' + surname name, userid, mobileno, extension
FROM
currentstaff
WHERE
(mobileno IS NOT NULL OR
Left(LTrim(extension), 2) = '07')

Select 'Select All', '','',''
union
SELECT
firstname + ' ' + surname name, userid, mobileno, extension
FROM
currentstaff
WHERE
(mobileno IS NOT NULL OR
Left(LTrim(extension), 2) = '07')

Is there a reason to do this in SQL, rather than in HTML, like your example shows? I've worked on systems where a 'Select All' or 'Please choose' is unioned into the query and the downside is that you cannot re-use that query for other purposes, because the query is now generating something that is only useful in an HTML dropdown.
To my mind, getting a list of people is one concern and building a UI with those people and a 'Select All' option is a different concern.

I wouldn't add the option to query, I'd add an extra option row into the HTML and when SELECT ALL is chosen I would update my query to not filter.
<select name="to" id="to">
<option value=""> -- Select -- </option>
<option value="ALL">SELECT ALL</option>
<option style="font-weight: bold" value="">Send to All</option>
<cfoutput query="people">
<option value="#qry.userid#">#qry.name#</option>
</cfoutput>
</select>

Related

WTForms / SQLAlchemy - Can you concatenate display values in SelectField or QuerySelectField

Is is possible to have a WTForm in which a drop down selection box (SelectField or QuerySelectField) displays a concatentated display value?
For example, your database may contain:
ID=1, FirstName=John, LastName=Smith
ID=2, FirstName=Kim, LastName=Johnson
so the generated HTML code would be something like:
<select name="userid">
<option value="1">John Smith</option>
<option value="2">Kim Johnson</option>
</select>
The display value is the concatenated value of FirstName + LastName. The unique identifier in the database will be the ID.
You need a dynamically set SelectField:
form.userid.choices = [(item.id, item.firstname+' '+item.lastname) for item in
session.query(ModelName).all()]

Generate xml fragments from a static set of rows

I have a static list of values I want to create a list of XML elements from. The values are: one, two, and three. I'm trying to avoid creating a new table just for this one special use case. The goal is to get rows with the following output from this list:
<option value="one"/>
<option value="two"/>
<option value="three"/>
I've tried the following SQL query in PostgreSQL which gets close:
SELECT xmlelement(name option, xmlattributes(s as value))
FROM (values('one'), ('two')) AS s;
However, I end up with the following output:
<option value="(one)"/>
<option value="(two)"/>
<option value="(three)"/>
I've also tried the following query with the same results:
SELECT xmlelement(name option, xmlattributes(s as value))
FROM (SELECT unnest('{one,two}'::varchar[])) AS s;
I believe I just need to find a way to refer to that column. I tried using ?column? but it didn't work. I'm also not sure if there is a cleaner way to write what I'm attempting.
May be it's me but there should be better ways of rendering HTML select options than database... like i.e. php, ruby, node.js etc.
In any case you can simply do
SELECT format('<option value="%s"/>', value) AS option
FROM (VALUES ('one'), ('two'), ('three')) options(value);
or
SELECT xmlelement(name "option", xmlattributes(value AS "value")) AS option
FROM (VALUES ('one'), ('two'), ('three')) options(value);
Output:
option
-------------------------
<option value="one"/>
<option value="two"/>
<option value="three"/>
Here is a dbfiddle demo

Creating a survey using Coldfusion: Stuck on the table data insert

I'm trying to create a survey tool in Coldfusion and I'm stuck on one part.
My tables are:
t_forms (id, name, desc)
t_questions (id, question, type, formid, order)
t_cdata (id, email)
t_cqdata (formid, questionid, customerid, answergiven)
The form fields are dynamically built using a url variable and look like this, for example:
<cfquery name="gs">
select * from t_forms where id = #url.sid#
</cfquery>
<cfquery name="gq">
select * from t_questions where fid = #gs.id# ORDER BY order ASC
</cfquery>
<cfform name="survey" method="post" action="">
<cfloop query="gq">
<cfinput type="text" name="q#gq.id#">
</cfloop>
<cfinput type="text" name="email">
<cfinput type="hidden" name="fid" value="#url.fid#">
<cfinput type="submit" name="submit" value="Save">
</cfform>
However, I'm having trouble when I need to put the value of the answer into the t_cqdata table, as the form element input needs to go into the table as well.
If anyone could help or point out where I am going wrong, that would be appreciated .
There is more than one way to have form fields associated with database identifier values. This is the one I find easiest to comprehend.
On the form page.
<cfoutput query="somequery">
<input name="field1#databaseID#">
<input name="field2#databaseID#">
etc
On the processing page.
<cfloop list="#form.fieldnames#" index="ThisElement">
<cfif left (ThisElement, 6) is "field1">
<cfset ThisID = RemoveChars(ThisElement, 1, 6)>
<cfset ThisField1Value = form[ThisElement]>
<cfset ThisField2Value = form['field2' & ThisID]>
Continue to set variables and then do something with them. In fact, in this example, once you've set ThisID, setting more variables is optional. You can simply use the form variables directly using the synax shown.
You will need to use an insert into cfquery.
You would use something like:
INSERT INTO t_cqdata
(q1
,q2
,q3
,q4
,q5
,q6
,q7)
VALUES
(
,
,
,
,
,)

ColdFusion cfselect display two query columns

I'm trying to display the results of a query, but I'm having a hard time combining two values that the query produces... Here's the query I have
<cffunction name="getStudentData" returntype="query">
<cfargument name="nameVar" type="string" required="yes">
<cfargument name="timeframe" type="numeric" required="yes">
<cfquery datasource="#Application.hds#" name="gsd">
select (s.lastname + ', ' + s.firstname) as StudData,
('[' + r.hallname + ' ' + r.roomnumber + ']') as roomdata,
s.studentnumber
from tblstudents s left join
(select h.hallname, ra.roomnumber, studentid
from tblroomassignments ra, tblhalls h
where ra.TimeFrame = #Arguments.timeframe#
and ra.hallid = h.hallid) r
on s.studentid = r.studentid
where s.lastname like '#Arguments.nameVar#%'
</cfquery>
<cfreturn #gsd#>
</cffunction>
What I'm trying to figure out is how to display StudData+' '+roomdata IF they both exist together since some students won't have a room assigned to them. (I'm just trying to produce a list of students that have a dorm/room assigned to them. In my cfselect...
<cfselect name="RecipientName"
query="studentdata"
display="StudData+' '+roomdata"???????
value="studentnumber">
</cfselect>
I don't know how to get StudData and roomdata in the display attribute without the page giving me an query column error. I'm very new to coldfusion and it's my understanding that you can only display one variable? Is there a way to combine StudData and roomdata into a variable and then display that variable?
Anyone have any ideas? Could this be simplified?
Hope this all makes sense!
I wouldn't use a cfselect at all.
<select name="RecipientName">
<cfoutput query="studentdata">
<option value="#studentnumber#">#StudData# #roomdata#</option>
</cfoutput>
</select>
If you really want to use cfselect, then I'd concatenate the columns in your query.
StudData + roomdata AS expanded_student_data
...
<cfselect name="RecipientName"
query="studentdata"
display="expanded_student_data"
value="studentnumber"
>
</cfselect>

Build <options> tags with FOR XML Path in SQL. Remove unwanted Attributes

Probably a really simple question to answer but I can't for the life of me find it anywhere.
I'm looking to build select list options from the database using FOR XML Path. So far I have this:
SELECT ID AS 'option/#value', Name AS [option]
FROM MyTable
FOR XML Path('')
Which gets me this:
<option value="1">Item 1</option><option value="2">Item 2</option>... and so on
Yep pretty simple stuff but now I want to set which option is selected where ID is lets say 1. So this should only ever occur once. So I add this:
SELECT
ID AS 'option/#value', Name AS [option],
CASE WHEN ID=1 THEN 'selected' ELSE '' END AS 'option/#selected',
FROM MyTable
FOR XML Path('')
Output:
<option value="1" selected="selected">Item1</option><option value="2" selected="">Item2</option>
Although this does technically set the right attribute I only need the attribute #selected on options where the case is true.
Any help or point in the right direction would be greatly received.
Instead of using a empty string in your case statement return a Null
SELECT
ID AS 'option/#value',
CASE WHEN ID=1 THEN 'selected' ELSE null END AS 'option/#selected',
Name AS [option]
FROM MyTable
FOR XML Path('')