What i am trying todo in coldfusion cfscript is iterate through a request collection of variables and do some evaluation, which i can do easily in PHP, but am running into issue translating to coldfusion cfscript, because it seems i cannot build a dynamic if statement
PHP
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
//If there was no where clause
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
I have tried this, errored out
for (i=1; i<= iColumnsLen; i++) {
if (rc.bSearchable_&i EQ true and rc.sSearch&i NEQ '') {
if ( sWhere EQ "" )
{ sWhere = " WHERE "; }
else
{ sWhere &= " AND ";}
}
}
also tried changing the if statement line to this, same
if (rc.bSearchable_+i EQ true and rc.sSearch+i NEQ '') {
finally i attempted building a string and using that , i knew it wouldnt work but i thought i'd give it a shot, error was could not convert var to boolean
for (i=1; i<= iColumnsLen; i++) {
var iterator = "rc.bSearchable_"&i&" EQ true and rc.sSearch_"&i&" NEQ ''";
if (#iterator#) {
Here's the static coldfusion without iteration that i am desiring to be done problematically
if (rc.bSearchable_1 EQ true and rc.sSearch_1 NEQ '') {
if ( sWhere EQ "" )
{ sWhere = " WHERE "; }
else
{ sWhere &= " AND ";}
}
if (rc.bSearchable_2 EQ true and rc.sSearch_2 NEQ '') {
if ( sWhere EQ "" )
{ sWhere = " WHERE "; }
else
{ sWhere &= " AND ";}
}
if (rc.bSearchable_3 EQ true and rc.sSearch_3 NEQ '') {
if ( sWhere EQ "" )
{ sWhere = " WHERE "; }
else
{ sWhere &= " AND ";}
}
any help would be much obliged
As Leigh says, you just need to refer to dynamic columns like this:
rc["bSearchable_" & i]
rc["sSearch_" & i]
Related
The tried below logic to generate the possible set of balanced parenthesis. but each possible output is getting printed twice.
void balanced_parenthesis(vector<string>& out_vec,string out_str,int is_open,int is_close)
{
if(is_open == 0 && is_close == 0)
{
cout<<out_str<<endl;
out_vec.push_back(out_str);
return;
}
if(is_open != 0)
{
out_str += '(';
is_open = is_open-1;
balanced_parenthesis(out_vec,out_str,is_open,is_close);
}
if( is_close > is_open)
{
out_str += ')';
is_close = is_close-1;
balanced_parenthesis(out_vec,out_str,is_open,is_close);
}
}
however by decrementing is_close and is_open variables in the function call prints the output exactly once.
void balanced_parenthesis(vector<string>& out_vec,string out_str,int is_open,int is_close)
{
if(is_open == 0 && is_close == 0)
{
cout<<out_str<<endl;
out_vec.push_back(out_str);
return;
}
if(is_open != 0)
{
out_str += '(';
balanced_parenthesis(out_vec,out_str,is_open,is_close);
}
if( is_close > is_open)
{
out_str += ')';
balanced_parenthesis(out_vec,out_str,is_open,is_close-1);
}
}
Could anyone explain me behaviour of the first code ?
The problem is that when the answer is null, it should show as "lollie or flavour did not enter", but it is not working.
var lollie;
var flavour;
lollie = prompt("Please enter type of lollie", "");
if (lollie != null) {
document.writeln("<p>lollie);
} else {
document.writeln(lollie = "lollie was not entered");
}
flavour = prompt("Please enter flavour", "");
if (flavour != null) {
document.writeln("<p> flavour <p>");
} else {
document.writeln(flavour = "You did not enter flavour");
}
You need to use && (logical AND) instead of || (logical OR):
var firstname;
var lastname;
var height;
firstname = prompt("Please enter first name of player 1", "");
if (firstname != null && firstname != "") {
document.writeln("<td>" + firstname + "</td>");
} else {
document.writeln(firstname = "First name not supplied");
}
lastname = prompt("Please enter Last name of player 1", "");
if (lastname != null && lastname != "") {
document.writeln("<td>" + lastname + "</td>");
} else {
document.writeln(lastname = "Last name not supplied");
}
height = prompt("Please enter height of player 1", "");
if (height != null && height != "") {
document.writeln("<td>" + height + "</td>");
} else {
document.writeln(height = "Height not supplied");
}
You'll also need to work a bit on your HTML markup, but I guess that is a Work In Progress.
Delete
firstname != ""
Only write this in if statement:
if(firstname !=null)
use && in your condition. and make sure you trim those variables.
Hi guys I did this little grammar to implement a simple calculator , but I need
to add IF - ELSE and DO WHILE to my grammar but I don't know how to work with the jumps and I don't really know what I need to add this new function.
code
grammar Calculadora;
#header {
import java.util.HashMap;
}
#members {
HashMap memory = new HashMap();
}
prog : stat+
;
stat : expr ';' { System.out.println($expr.value); }
| ID '=' expr ';' { memory.put($ID.text, new Integer($expr.value));
System.out.println($ID.text + " = " + $expr.value); }
;
expr returns [int value]
: e=multExpr { $value = $e.value; }
( '+' e=multExpr { $value += $e.value; }
|
'-' e=multExpr { $value -= $e.value; }
)*
;
multExpr returns [int value]
: e=atom { $value = $e.value; }
( '*' e=atom { $value *= $e.value; } )*
;
atom returns [int value]
: INT { $value = Integer.parseInt($INT.text); }
| ID { Integer v = (Integer)memory.get($ID.text);
if ( v != null ) $value = v.intValue();
else System.err.println("variable no definida " + $ID.text); }
| '(' expr ')' { $value = $expr.value; }
;
ID : ('a'..'z'|'A'..'Z')+
;
INT : '0'..'9'+
;
NEWLINE : '\r'? '\n'
;
WS : (' '|'\t')+ { skip(); }
;
i have built a fully working search page for my WebMatrix property site. To summarize, it checks the query string, and if any of the defined values are present, it adds it to a list so it can iterate through them.
The problem, as you will see, if that it currently adds everything to the list as a string. This causes problems when i need to use the value as an INT (PropertyType and NumBedrooms should be int's). is there a way i can use both strings and int's in this scenario? can i add ints AND strings to the same list?
here's the code:
var db = Database.Open("StayInFlorida");
var proptype = db.Query("SELECT * FROM Property_Type");
IEnumerable<dynamic> queryResults;
//Search Variables
string searchTerm = "";
string resortID = "";
string propertyType = "";
string numBedrooms = "";
List<string> argList = new List<string>();
//Paging Variables
var pageSize = 6;
var totalPages = 0;
var count = 0;
var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1;
var offset = (page -1) * pageSize;
//Request querystrings from URL
if (!String.IsNullOrWhiteSpace(Request.QueryString["searchTerm"]))
{
searchTerm = Request.QueryString["searchTerm"];
}
if (!String.IsNullOrWhiteSpace(Request.QueryString["resortID"]))
{
resortID = Request.QueryString["resortID"];
}
if (!String.IsNullOrWhiteSpace(Request.QueryString["propertyType"]))
{
propertyType = Request.QueryString["propertyType"];
}
if (!String.IsNullOrWhiteSpace(Request.QueryString["numBedrooms"]))
{
numBedrooms = Request.QueryString["numBedrooms"];
}
int numOfArguments = 0;
string selectQueryString = "SELECT * FROM Property_Info ";
if (searchTerm != "")
{
argList.Add(searchTerm);
selectQueryString += "WHERE FullDescription LIKE '%' + #0 + '%' OR BriefDescription LIKE '%' + #0 + '%' ";
numOfArguments++; //increment numOfArguments by 1
}
if (resortID != "")
{
argList.Add(resortID);
if (numOfArguments == 0)
{
selectQueryString += "WHERE ResortID = #0 ";
}
else
{
selectQueryString += "AND ResortID = #" + numOfArguments + " ";
}
numOfArguments++;
}
if (propertyType != "")
{
argList.Add(propertyType);
if (numOfArguments == 0)
{
selectQueryString += "WHERE PropertyTypeID = #0 ";
}
else
{
selectQueryString += "AND PropertyTypeID = #" + numOfArguments + " ";
}
numOfArguments++;
}
if (numBedrooms != "")
{
argList.Add(numBedrooms);
if (numOfArguments == 0)
{
selectQueryString += "WHERE NumBedrooms = #0 ";
}
else
{
selectQueryString += "AND NumBedrooms = #" + numOfArguments + " ";
}
numOfArguments++;
}
selectQueryString += "ORDER BY CreatedDate DESC";
string[] argArray = argList.ToArray();
queryResults = db.Query(selectQueryString, argArray);
count = queryResults.Count();
totalPages = count/pageSize;
if(count % pageSize > 0){
totalPages += 1;
}
Edited code after Polynomial's suggestion:
#{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Search Page";
var db = Database.Open("StayInFlorida");
var proptype = db.Query("SELECT * FROM Property_Type");
//Paging Variables
var pageSize = 6;
var totalPages = 0;
var count = 0;
var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1;
var offset = (page -1) * pageSize;
IEnumerable<dynamic> queryResults;
//Request querystrings from URL
string selectQueryString = "SELECT * FROM Property_Info WHERE 0 = 0 AND NumBedrooms = #0 AND NumBathrooms = #1 ";
selectQueryString += "ORDER BY CreatedDate DESC";
queryResults = db.Query(selectQueryString, Request.QueryString["NumBedrooms"], Request.QueryString["NumBathrooms"]);
count = queryResults.Count();
totalPages = count/pageSize;
if(count % pageSize > 0){
totalPages += 1;
}
}
Well, there are lots of ways around the problem, I'm thinking the easiest one that doesn't involve not using named ordinal parameterization would be to use fixed ordinals for each search criteria and just not use the ones you don't need in the SQL statement. Your code is very long so I don't really want to try and rewrite the whole thing here, but basically if your db.Query() call has all of the potential arguments passed as parameters like this:
queryResults = db.Query(selectQueryString, Request.QueryString["searchTerm"], Request.QueryString["resortId"]).AsInt(), Request.QueryString["propertyType"].AsInt() /* etc etc */);
Then your ordinals are always the same number. searchTerm is always #0, resortId is always #1 etc, etc. You then don't need the array at all and just need to build up an appropriate SQL statement for what the user specified, for example some WHERE clauses that would all fit with the code above could be:
"WHERE ResortID = #1"
"WHERE PropertyType = #2"
"WHERE ResortID = #1 AND PropertyType = #2"
"WHERE Name = #0"
"WHERE Name = #0 AND ResortID = #1 AND PropertyType = #2"
In the case of the clauses that only use one or two of the parameters, the others are just ignored and hopefully everything will work as you intend.
Edit to show code:
// Basic beginning of the select statement
string selectQueryString = "SELECT * FROM Property_Info WHERE 0 = 0 ";
// Check to see if the user specified bedrooms, if they did add to the select statement
if (Request.QueryString["NumBedrooms"].AsInt() > 0)
{
selectQueryString += "AND NumBedrooms = #0 ";
}
// Same for the number of bathrooms
if (Request.QueryString["NumBathrooms"].AsInt() > 0)
{
selectQueryString += "AND NumBathrooms = #1 ";
}
// ** Add more criteria to your select list here by checking for valid values and concatenating your string **
// Finally add the order by on the end
selectQueryString += "ORDER BY CreatedDate DESC";
// Execute the statement
queryResults = db.Query(selectQueryString, Request.QueryString["NumBedrooms"].AsInt(), Request.QueryString["NumBathrooms"].AsInt());
This should give you a very flexible and expandable search criteria system that is happy with zero, one or many criteria.
I wrote if statement in javacc and do not know how to ignore if the body if the condition is false
First I declare two object class Token and boolean variable, next I check what return method condition(), next i want to skip rest of block when condition is false. How to do that?
void ifElse() :
{
Token start;
Token next;
boolean cond = false;
}
{
{
start = token;
}
< IF > < LB> cond = condition() < RB> < LP>
{
if (cond == false)
{
String tok = (String) token.image;
while (tok.equals("}"))
{
token = token.next;
}
}
}
(
ifElse()
| declaration()
)*
< RP>
(< ELSE > < LP> < RP >)?
}
I've reconfigured your code, and it works almost fine. Avoids body but does not stop.
`void ifElse() :
{
boolean cond = false;
}
{
< IF > < LB> cond = condition() < RB> < LP>
{
if (cond == false)
{
while (!token.image.equals("}"))
{
token = getNextToken();
}
}
}
(
ifElse()
| declaration()
)*
< RP>
(< ELSE > < LP> < RP >)?
}`