View Full Version : Translating a ColdFusion page into PHP - newbie questions

12-17-2003, 01:58 AM
I'm trying to translate a simple application I wrote in ColdFusion into PHP. Does anyone know what the PHP equivalent of a <CFPARAM> tag would be?

Also, I'm trying to translate the following conditional query, and am unsure of the best way to do it:

<CFQUERY DATASOURCE="MarketNotes" NAME="GetNotes">
SELECT NoteID, Date, Headline, SubjectName, SourceName
FROM notes, subjectareas, sources
WHERE subjectareas.SubjectAreaID = notes.SubjectAreaID AND notes.SourceID = sources.SourceID
<!--- if a particular subject was selected --->
<cfif NOT SubjectView IS "all">
AND #SubjectView# = notes.SubjectAreaID
ORDER BY #sort# #sortdir#

I can't figure out how to throw a conditional statement into the middle of a php-based sql query. I start out with

$GetNotes ="SELECT (yada yada yada) ... "

but can't figure out how you throw a conditional statement into the middle of that.

Any suggestions would be greatly appreciated, thanks!

12-17-2003, 03:05 AM
1. I have no idea what the CFPARAM tag is actually doing, but I have the feeling that it could be related to HTTP/Server parameters sent to the script? If so, check the PHP manual for $_GET, $_POST, and $_SERVER.

2. You can declare an empty string that receives its content on a certain condition, and later use that variable in your SQL query. Example:

$where = "";

if ($num < 20) { // arbitrary condition
$where = " AND num < 20"

// ...

$sql = "
id < 1000

Depending on the evaluation of the if-statement, $where contains either nothing and has no effect on the SQL query, or its contents refine the WHERE statement. The string contents of $where are actually inserted into the $sql string, so this portion looks like

id < 1000
AND num < 20

if the condition is true, that is.

12-17-2003, 05:14 PM
ahhh, that's a great idea...thanks.

the CFPARAM tag is a tag that sets a default value for a variable if that variable is not passed to the page through a POST or GET method.


<CFPARAM name="SomeVariable" default="value">

sets the variable #SomeVariable# to the value "value". but if the page was called from a link (say mypage.cfm?SomeVariable="yomommasofat") then #SomeVariable# would have the value "yomommasofat" and the CFPARAM tag will be ignored.

the same can also be accomplished in CF as follows

<CFIF NOT IsDefined(SomeVariable)>
<CFSET SomeVariable="value">

Where the IsDefined() function returns TRUE is the variable it is passed has a value, and FALSE otherwise.

Is there a function in PHP that is equivalent to the IsDefined() CF function? I looked in the function reference at php.net but I couldn't find anything.