Sorting values by property from cfoutput - sorting

This question for cold fusion programmers, and will be probably asked by me wrongly, because it is open question and actually can't be answered, coz u and me will be in a lack of inf about it :) But still all i need is just a hint or guess about it, so that i can understand and move on to achieve my aim.
So here comes the question:
I have the coldfusion output script
<cfquery datasource="#dsn2#">SELECT * FROM PRODUCT WHERE PRODUCT_ID = #PRODUCT_ID#</cfquery>
where some products are displayed, and all i need is to sort them by the property for example is_purchase whose values can be 0 or 1, plus i have a checkbox:
<input type="checkbox" name="is_purchase_stock" value="1" <cfif isdefined("attributes.is_purchase_stock")>checked</cfif> onClick="sayfalama.submit();">
There are actually functions smth like this(is_saleable_stock), u can see it from the all the script of the whole page with products:
http://vteam.net.ru/_fr/4/list_prices.cfm
Thank you everyone!

You want ORDER BY, something like this:
<cfquery datasource="#dsn2#">
SELECT * FROM PRODUCT
WHERE PRODUCT_ID = #PRODUCT_ID#
ORDER BY is_purchase <cfif StructKeyExists(attributes, "is_purchase_stock")>ASC<cfselse>DESC</cfif>
</cfquery>
EDIT. This is a reply to the question in a comment:
<cfquery datasource="#dsn2#">
SELECT * FROM PRODUCT
WHERE PRODUCT_ID = #PRODUCT_ID#
AND is_purchase = <cfif StructKeyExists(attributes, "is_purchase_stock")>1<cfselse>0</cfif>
</cfquery>

Related

Query with sum many columns from same table with laravel query

This seems very easy query but can't translate it into laravel query. I have table orders there are two columns total_usd and total_gbp. I want to sum each column to get total of usd and total of gbp on the page.
This query is working in phpmyadmin and I got correct result
SELECT sum(order_total_usd) as usd, sum(order_total_gbp) as gbp FROM `orders`
In laravel I've tried this
$sumOrders = Order::select('sum(order_total_gbp) as gbp, sum(order_total_usd) as usd');
when I dd($sumOrders) I've one really huge output which almost cause browser to freeze.
Where is my mistake here?
You can try something like this
$sumOrders = Order::select( \DB::raw("sum(order_total_gbp) as gbp"), \DB::raw("sum(order_total_usd) as usd"))->get();
You can use selectRaw
$sumOrders = Order::selectRaw('sum(order_total_gbp) as gbp, sum(order_total_usd) as usd');
Except for one missed word, your code is OK. Add "Raw" after "select" as shown:
$sumOrders =
Order::selectRaw(
'sum(order_total_gbp) as gbp,
sum(order_total_usd) as usd'
);
Just replace "Order::select(...)" with Order::selectRaw(...).
Have a great day!

passing 2 values from cfselect based on selection from dropdown?

This is only my second question on SO, and I'm brand new to web coding of all kinds (anything I've done I've picked up within the last 3 weeks so I don't really know what I'm doing) but here's some background and my issue:
Background info:
I'm updating some existing pages that previously had, no joke, 42 different dropdown menus to show data maps for different water bodies and sampling dates. So I'm condensing that down to a slightly more elegant (though still way dated) approach of all of that in 2 dropdown menus where users can choose whatever combo of tributaries and sampling dates they want to see data for.
I have 2 dynamic related dropdowns using a CFC and CFM (those are working great, thanks to help from SO). THE CFC has the functions, queries, arguments to get the data from the database. The CFM displays the first dropdown menu (a list of tributaries), and when a user selects from that, a second dropdown is populated with dates that the tributary/ies were sampled. The 'display' of this 2nd dropdown is tributary name appended to the beginning of the date so they displays as a 'tribdate', like "Back River: Apr 07, 2014". When a user selects what they want from the second dropdown (named 'link') and hits submit, the sent value(s) are path strings ('File_html') which, on the target page, result in maps of data being displayed for those tributaries on those dates. In the target page the 'form.link' is defined, loops, and shows all the data map images (ie. 'File_html') correctly.
My issue:
Above each image that is displayed I want to have the "tributary" name and the "sampling date" but I don't know how to pass this information. I am passing the 'File_html' but I would like to pass 'tribdate' (the display value) or pass 'tributary' or 'cruisedate' separately. Or even use the alt text from the image, but I don't know how to do that either.
What I've tried:
(1) I can't pull a usable tributary name out of the value that is passed (many are partial names), but I can pull a usable tributary date.
(2) If I pull the 'tributary' name from the 1st dropdown where tributary is selected and try to use that to title the maps it only works for one map (which makes sense bc only 1 tributary is selected). So if a user selects 2 dates from 'Back River' only one map can be displayed with the title of 'Back River' and there's an error for the second. If there's a way to replicate this tributary value to be reused when needed, that would be great.
(3) I tried making a 3rd dropdown which just shows the tributary name for the dates selected and then on the mapdisplay using the name of that dropdown menu for the title name, which works, but I can't really have people select those, I want them automatically passed.
Can I call tributary from the CFC? Can I send 'tribdate' to the mapdisplay page? Any help, hints, ideas would be great, thank you.
I saw this question which is essentially what I'm asking but I don't really understand where to put the answer that was given: Using CFQUERY and CFSELECT to pull multiple values based on selection
The CFC:
<cfcomponent displayname="GetStuff" hint="Getting data on cruises from database">
<!---Get list of Tributaries--->
<cffunction name="getData"
access="remote"
returntype="query"
hint="Get data for tributary dropdown">
<!---define variables--->
<CFSET VAR data="">
<!---Run the query for tributaries--->
<cfquery name="data" datasource="mydatasource">
SELECT DISTINCT Tributary
FROM df_cruises
ORDER BY Tributary
</cfquery>
<!---and return it--->
<cfreturn data>
</cffunction>
<!---Get Dates by Tributary--->
<cffunction name="getDates"
access="remote"
returntype="query"
hint="Get cruise dates by tributary for select dropdown">
<cfargument name="Tributary" type="any" required="no" multiple="yes">
<!---define variables--->
<cfset var data="">
<!---Run query to get Date Data and add Trib name to front, and get File_html to send to MapDisplay--->
<cfquery name="data" datasource="mydatasource">
SELECT File_html, Tributary, (Tributary + ':' + SPACE(2)) + CONVERT(varchar(12), CruiseDate, 107) AS tribdate
FROM df_cruises
WHERE Tributary IN (<cfqueryparam cfsqltype='cf_sql_varchar' list="yes" value='#ARGUMENTS.Tributary#'>)
ORDER BY tribdate
</cfquery>
<!---And return it--->
<cfreturn data>
</cffunction>
</cfcomponent>
CFM which has the dropdowns/form:
<cfform name="CruiseChoose" action="mapdisplayv5.cfm" method="post">
<td align="center">
<strong>Tributary/Water Body:</strong><br />
<cfselect name="Tributary"
size="7" class="dynamicdropdown"
multiple="yes"
value="Tributary"
display="Tributary"
bind="cfc:UsingBook.getData()"
bindonload="yes"/>
</td>
<td align="center"><br />
<strong>Sampling Dates:</strong><br />
<cfselect name="link"
size="7" class="dynamicdropdown"
multiple="yes"
value="File_html"
display="tribdate"
bind="cfc:UsingBook.getDates({Tributary})"
bindonload="false"
type="any"/>
</td>
The target CFM page which displays the data maps:
<!---mapdisplayv5.cfm--->
<tr>
<td>
<p><p><cfoutput>
<cfif IsDefined("form.link")>
<br>
<cfset cnt= 1>
<cfloop index="listing" list="#form.link#" delimiters=",">
<cfset Tributary=#listGetAt(form.link, cnt)#>
<div align="center">
<font size="4"><b>#Tributary#<cfset cnt=cnt> - <cfset tribdate=#listGetAt(form.link, cnt)#> #right(tribdate, 6)# </b></font>
<cfset wqmimage=#listGetAt(form.link, cnt)#>
<table width="85%" border="1" bordercolor="##000000">
<tr>
<td><cfinclude template="images/#Ltrim(wqmimage)#.html"></td>
</tr>
</table>
Click here for Enlarged Map (WARNING-Large File Size)<Br>
<cfif listlen(form.link)Less Than or Equal to #cnt#>
<cfbreak>
</cfif>
<cfset cnt=cnt+1>
<br><br><hr>
</cfloop>
<cfelse>
<b>
<p align="center"><br><br><br><br>No Selection Was Made.<br>Please Return to the Previous Page and Make Your
Selection</p></b><Br>
<br><br><br>
</cfif>
</p>
</cfoutput></p>
UPDATE:
On the CFM page that has the form I am now passing the PK "ID" to the mapdisplayv5.cfm, below is the new code for that. It now relates the correct title and shows the image that I want and will show multiple, but each multiple is a copy of whatever the first selection is from the second dropdown. So if you choose 2 different dates, it shows 2 copies of the first date you chose.
Snapshot of the change to the 2nd dropdown on the CFM with the form, note that I am now passing ID:
<td align="center"><br />
<strong>Sampling Dates:</strong><br />
<cfselect name="link"
size="7" class="dynamicdropdown"
multiple="yes"
value="ID"
display="tribdate"
bind="cfc:UsingBook.getDates({Tributary})"
bindonload="false"
type="any"/>
</td>
Change to the mapdisplay.cfm page:
<!---mapdisplayv5.cfm--->
<tr>
<td>
<p><cfoutput>
<cfif IsDefined("form.link")>
<cfquery name="retrieve" datasource="mydatasource">
SELECT File_html, Tributary, CruiseDate, (Tributary + ':' + SPACE(2)) + CONVERT(varchar(12), CruiseDate, 107) AS tribdate
FROM df_cruises
WHERE ID IN (<CFQUERYPARAM VALUE='#FORM.link#' list='yes' CFSQLTYPE='CF_SQL_INTEGER'>)
</cfquery>
<cfset cnt= 1>
<cfloop index="listing" list="#form.link#" delimiters=",">
<br>
<cfset wqmimage=#retrieve.File_html#>
<cfset tributary=#retrieve.Tributary#>
<cfset tribdate=#retrieve.tribdate#>
<div align="center">
<font size="4"><b>#tribdate#</b></font>
<cfinclude template="images/#Ltrim(wqmimage)#.html">
Click here for Enlarged Map (WARNING-Large File Size)<Br><br>
<cfif listlen(form.link)Less Than or Equal to #cnt#>
<cfbreak>
</cfif>
<cfset cnt=cnt+1>
</cfloop>
<cfelse>
<br>
<p align="center"><br><br><br><br>No Selection Was Made.<br>Please Return to the Previous Page and Make Your
Selection</p></b><Br>
<br><br><br>
</cfif>
</cfoutput>
UPDATE 2:The corrected, working version of the mapdisplayv5.cfm
(THANK YOU luke, Dan, and Leigh!)
<!---mapdisplayv5.cfm--->
<tr>
<td>
<p>
<cfif IsDefined("form.link")>
<cfquery name="retrieve" datasource="tideRO">
SELECT File_html, Tributary, CruiseDate, (Tributary + ':' + SPACE(2)) + CONVERT(varchar(12), CruiseDate, 107) AS tribdate
FROM df_cruises
WHERE ID IN (<CFQUERYPARAM VALUE='#FORM.link#' list='yes' CFSQLTYPE='CF_SQL_INTEGER'>)
</cfquery>
<br><cfoutput query="retrieve">
<cfset wqmimage=#retrieve.File_html#>
<cfset tributary=#retrieve.Tributary#>
<cfset tribdate=#retrieve.tribdate#>
<div align="center">
<font size="4"><b>#tribdate#</b></font>
<cfinclude template="images/#Ltrim(wqmimage)#.html">
Click here for Enlarged Map (WARNING-Large File Size)<Br><br></cfoutput>

Dynamic rowspan in a Table Coldfusion

Just to give some background. We have users in different parts of the region. Our application sends out reports in emails which can be accessed via a URL. This way we keep a track on who accessed the report and various other attributes.
Now, as part of the statistics, I am trying to display the attributes in a HTML table.
I have the query which contains the details about the "Region Name", "UserID", "ReportName", "AccessCount", "ViewDate" etc.
The requirement is to span the "Region Name" across all its rows.
For ex. 10 different users from Melbourne region have access a report XYZ.
My table should have Melbourne with rowspan = "10" and each row having each users' details.
I don't want Melbourne to repeat 10 times in the table.
I've tried using the <cfoutput group="RegionName" tag along with the HTML table but the table is not well-formed.
How can I achieve this?
You should be able to achieve this and you were along the right direction by looking at the groupby attribute (actually the attribute is group=""). The cfml won't look very pretty though (I prefer cfscript and likely would do the following with a few functions). Something like the following should render a well-formed table with the RegionName cell spanning multiple rows in a well-formed manner, just adjust with classes / formatting etc. as you see fit!
<!---
Make sure that myQuery is ordered by
RegionName ASC before anything else to
ensure the group by works as intended
--->
<table>
<thead>
<tr>
<th>Region</th>
<th>User</th>
</tr>
</thead>
<tbody>
<cfoutput query="myQuery" group="RegionName">
<!--- set up an array to hold the users for this region --->
<cfset arrOfUsers = ArrayNew(1)>
<cfoutput>
<cfset ArrayAppend(arrOfUsers,'<td>'&myQuery.UserName&'</td>)>
</cfoutput>
<!--- render time, use the array just generated so we know how many users are in this group --->
<cfloop from="1" to="#ArrayLen(arrOfUsers)#" index="i">
<tr>
<cfif i EQ 1>
<td rowspan="#ArrayLen(arrOfUsers)#">#myQuery.RegionName#</td>
</cfif>
#arrOfUsers[i]#
</tr>
</cfloop>
</cfoutput>
</tbody>
</table>

Facebook API: Getting photos that have a comment containing a a string

I'm implementing a pseudo hash-tagging system for the company I work at with their customer's facebook photos. A customer can upload a photo to the page, and a page admin can tag it with the hashtag for a product.
What I am trying to do is get all photos from the page that have a certain tag in the comments (for instance, get all photos from the company page with a comment containing only '#bluepants').
I am trying to make sure the Facebook API handles the heavy lifting (we'll cache the results), so I'd like to use FQL or the Graphs API, but I can't seem to get it working (my SQL is quite rusty after relying on an ORM for so long). I would prefer if it outputs as many results as possible, but I'm not sure if FB lets you do more than 25 at once.
This is going to be implemented in a sinatra site (I am currently playing around with the Koala gem, so bonus points if I can query using it)
Could anyone give me some guidance?
Thanks!
I've got something like this working in FQL/PHP. Here is my multiquery.
{'activity':
"SELECT post_id, created_time FROM stream WHERE source_id = PAGE_ID AND
attachment.fb_object_type = 'photo' AND created_time > 1338834720
AND comments.count > 0 LIMIT 0, 500",
'commented':
"SELECT post_id, text, fromid FROM comment WHERE post_id IN
(SELECT post_id FROM #activity) AND AND (strpos(upper(text), '#HASHTAG') >= 0",
'accepted':
"SELECT post_id, actor_id, message, attachment, place, created_time, likes
FROM stream WHERE post_id IN (SELECT post_id FROM #commented)
ORDER BY likes.count DESC",
'images':
"SELECT pid, src, src_big, src_small, src_width, src_height FROM photo
WHERE pid IN (SELECT attachment.media.photo.pid FROM #accepted)",
'users':
"SELECT name, uid, current_location, locale FROM user WHERE uid IN
(SELECT actor_id FROM #accepted)",
'pages':
"SELECT name, page_id FROM page WHERE page_id IN (SELECT actor_id FROM #accepted)",
'places':
"SELECT name, page_id, description, display_subtext, latitude, longitude
FROM place WHERE page_id IN (SELECT place FROM #accepted)"
}
To break this down:
#activity gets all stream objects created after the start date of my campaign that are photos and have a non-zero comment count. Using a LIMIT of 500 seems to return the maximum number of posts. Higher or lower values return fewer.
#commented finds the posts that have #HASHTAG in the text of one of their comments. Note, I'm not looking for a #, which is a reserved character in FQL. Using it may cause you problems.
#accepted gets the full details of the posts found in #commented.
#images gets all the details of the images in those posts. I have it on my todos to refactor this to use object_id instead of pid and try using the new real_width specification to make my layout easier.
#users and #pages get the details of the actor who originally posted the item. I now know I could have used the profile table to get this in one query.
#places gets the location details for geo-tagged posts.
You can see this in action here: http://getwellgabby.org/show-us-a-sign

How to reorder struct columns?

I'm trying to display my results from a CFQuery in a specific order. The order is to be maintained in the database so that it can be manipulated, and there are an unknown number of columns per table. The final row in the table is "ColumnOrder": each column has a number to specify it's sort order, 0 means "don't display". I'm trying to sort by looping say, "y" from 1 to maxCols:
0) do y = 1 to maxCols
1) in the sortColumn result set, use y to lookup the corresponding KEY
2) in the products result set, find the value from the corresponding KEY
3) insert said value into tempStruct[y]
4) loop.
I'm running into a wall trying to use structFindKey(). Here's my code:
<CFQUERY name="qParts" datasource="Pascal">
SELECT * FROM Turbos WHERE PartNumber LIKE <cfqueryparam cfsqltype="cf_sql_char" maxlength="30" value="%#mfr#%"> ORDER BY #sort# ASC
</CFQUERY>
<cfquery name="qPartsOrder" datasource="Pascal">
SELECT * FROM Turbos WHERE PartNumber = 'ColumnOrder'
</cfquery>
<cfset tempStruct=structnew()>
<cfloop index="columnOrder2" from="1" to="#ListLen(qPartsOrder.ColumnList, ',')#">
<cfdump var="#StructFindKey(qPartsOrder, columnOrder2)#">
<cfset tempStruct[columnOrder2] = StructFindKey(#qPartsOrder#, "#columnOrder2#")>
<cfset currentCol = "#ListGetAt(qParts.columnList, columnOrder2, ',')#">
<cfoutput>#qParts[currentCol][qParts.currentrow]# <br/></cfoutput>
</cfloop>
<cfdump var="#tempstruct#">
The line
<cfdump var="#StructFindKey(qPartsOrder, columnOrder2)#">
is throwing a BLANK!! error message, so I can't debug it and I'm stuck.
Any and all help would be appreciated (and YES I have to use SELECT *, this is a generic product display page for displaying ALL information in the database except a few which are denoted by a zero in the order column, remember?).
I'm not 100% sure that I understand the problem you are trying to solve. The is exacerbated by a very unconventional way of setting up a database.
To begin with, if you are not lucky you may run into a documented error where using a cfqueryparam tag throws an error of Value cannot be converted to requested type although I don't know if this still happens with current versions of ColdFusion (8+).
In any case, you can always select all of the columns of the table manually even if you don't know how many of them will ultimately be used:
SELECT partNumber, secondColumn, thirdColumn, ... , nthColumn
FROM Turbos
This is generally preferable to just using SELECT * although it presents some problems if you are in the habit of frequently adding/removing columns to tables.
Unless you need to use a Struct for good reason, you should use an Array instead. Structs don't store ordering information while Arrays do. Here is one way to sort through the records in qParts:
<cfset RecordsArray=ArrayNew(2)>
<cfset ColumnIndex=StructNew()>
<cfloop list="#qPartsOrder.ColumnList#" index="order_column">
<cfset ColumnIndex[order_column]=val(qPartsOrder[order_column][1])>
</cfloop>
<cfloop query="qParts">
<cfloop list="#qPartsOrder.ColumnList#" index="order_column">
<cfif val(ColumnIndex[order_column])>
<cfset RecordsArray[ColumnIndex[order_column]][qParts.CurrentRow]=qParts[order_column][qParts.CurrentRow]>
</cfif>
</cfloop>
</cfloop>
The result of this code will be a 2D array, with the first number referring to the column index and the second index pointing to the record row.
All in all, I think that unless you have zero control over how the database is structured, there is a better way to implement this, starting with how you've set up your database. It would really help to see some fake sample data as well as having a clearer idea of what you are trying to accomplish -- what will you do with these ordered fields once you have them, for example?
Dun you try to use StructSort ?

Resources