Solved

a Code php which i am trying to convert into coldfusion

Posted on 2009-04-15
12
252 Views
Last Modified: 2013-12-16
Hi experts, i have one php functions which i need to convert into coldfusion. but i cant understand some of its features and do not know how i do it . it is basically a function that deals with categories and subcategories.

if you can please guide me it will thanks from me

Also can if explain what this function is doing,

till what i understand is that it is moving or merging the categories to another categories or top level categories. something like this. please help me
function deleteCategory()
{
    if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) {
        $catId = (int)$_GET['catId'];
    } else {
        header('Location: index.php');
    }
    
	// find all the children categories
	$children = getChildren($catId);
	
	// make an array containing this category and all it's children
	$categories  = array_merge($children, array($catId));
	$numCategory = count($categories);
 
	// remove all product image & thumbnail 
	// if the product's category is in  $categories
	$sql = "SELECT pd_id, pd_image, pd_thumbnail
	        FROM tbl_product
			WHERE cat_id IN (" . implode(',', $categories) . ")";
	$result = dbQuery($sql);
	
	while ($row = dbFetchAssoc($result)) {
		@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_image']);	
		@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_thumbnail']);
	}
	
	// delete the products
	$sql = "DELETE FROM tbl_product
			WHERE cat_id IN (" . implode(',', $categories) . ")";
	dbQuery($sql);
	
	// then remove the categories image
	_deleteImage($categories);
 
    // finally remove the category from database;
    $sql = "DELETE FROM tbl_category 
            WHERE cat_id IN (" . implode(',', $categories) . ")";
    dbQuery($sql);
    
    header('Location: index.php');
}

Open in new window

0
Comment
  • 7
  • 5
12 Comments
 
LVL 16

Expert Comment

by:duncancumming
ID: 24150789
Here's  a rough go at it.  I've rewritten it more in the CF style than the PHP style, e.g. I've removed references to the URL scope, instead just assuming you can pass URL.catID into your function.  Also removed the header calls for changing location; you wouldn't normally call cflocation within a coldfusion function (at least I wouldn't).  

The code refers to a couple of functions which I don't know about, _deleteImage and getChildren.  I assume you've got those covered already?

<cffunction name="deleteCategory">
	<cfargument name="catID" required="yes" type="numeric">
	
	<!--- find all the children categories --->
	<cfset var children = getChildren(arguments.catId)>
	
	<!--- make an array containing this category and all it's children --->
	<cfset var categories = children>
	<cfset var numCategory = "">
	<cfset var result = "">
	
	<cfset ArrayAppend(categories, arguments.catId)>
	<cfset numCategory = ArrayLen(categories)>
		
	<!---  remove all product image & thumbnail 
	 if the product's category is in  $categories --->
	<cfquery name="result" datasource="dsn">
		SELECT pd_id, pd_image, pd_thumbnail
		FROM tbl_product
		WHERE cat_id IN 
		(
			#ArrayToList(categories)#
		)
	</cfquery>
	
	<cfloop query="result">
		<cffile action="DELETE" file="#SRV_ROOT##PRODUCT_IMAGE_DIR##result.pd_image#">   
		<cffile action="DELETE" file="#SRV_ROOT##PRODUCT_IMAGE_DIR##result.pd_thumbnail#">    
	</cfloop>
	
	<!--- delete the products --->
	<cfquery datasource="dsn">
		DELETE FROM tbl_product
		WHERE cat_id IN 
		(
			#ArrayToList(categories)#
		)
	</cfquery>
	
	<!--- then remove the categories image --->
	<cfset _deleteImage(categories)>
	
	<!--- // finally remove the category from database; --->
	<cfquery datasource="dsn">
		DELETE FROM tbl_category 
		WHERE cat_id IN 
		(
			#ArrayToList(categories)#
		)
	</cfquery>
	
	<cfreturn void>
</cffunction>

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 24150947
Perfect on the way mate. Thanks.
  • couple of things i would like to ask.
  • can u please elaborate what this code is actually performing.
  • _thedeleteimage function just delete the file from the folder.
  • the getcategory funcyion i forgot to mention mate:
this is the one below:


$catId is the value coming from url catID
 
function getChildren($catId)
{
    $sql = "SELECT cat_id ".
           "FROM tbl_category ".
           "WHERE cat_parent_id = $catId ";
    $result = dbQuery($sql);
    
	$cat = array();
	if (dbNumRows($result) > 0) {
		while ($row = dbFetchRow($result)) {
			$cat[] = $row[0];
			
			// call this function again to find the children
			$cat  = array_merge($cat, getChildren($row[0]));
		}
    }
 
    return $cat;
}

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 24150965
suppose this: <cfset _deleteImage(categories)>
_deleteimage is function and it have some another function which contains the query and path of the file to delete the file from folder. will writting the above way will delete the image of the selected category or not
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:duncancumming
ID: 24151117
I might need to see the code for the _deleteImage function.  If it was just doing the delete of the file, why wouldn't the original author just have used the @unlink command, like they already did earlier in the code?


Here's a rewrite of the getChildren function.  NB this function is using recursion; I think I've got it correct but you'll want to test it!

<cffunction name="getChildren" returntype="Array" output="no">
	<cfargument name="catID" required="yes" type="numeric">
	
	<cfset var result = "">
	<cfset var cat = ArrayNew(1)>
	
	<cfquery name="result" datasource="dsn">
		SELECT cat_id
		FROM tbl_category
		WHERE cat_parent_id = #arguments.catId#
	</cfquery>
	
	<cfloop query="result">
		<cfset ArrayAppend(cat, result.cat_id)>
			
		<!--- call this function again to find the children --->
		<cfset ArrayAppend(cat, getChildren(result.cat_id))>
	</cfloop>
 
	<cfreturn cat>
</cffunction>

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 24151311
Thanks Mate thanks 
If u like to see the _delete function, i post it here, might give clue:
although i put all these in one cfc and making their method as public, will thry work correctly or i just call in some other page

function _deleteImage($catId)
{
    // we will return the status
    // whether the image deleted successfully
    $deleted = false;
 
	// get the image(s)
    $sql = "SELECT cat_image 
            FROM tbl_category
            WHERE cat_id ";
	
	if (is_array($catId)) {
		$sql .= " IN (" . implode(',', $catId) . ")";
	} else {
		$sql .= " = $catId";
	}

Open in new window

0
 
LVL 16

Expert Comment

by:duncancumming
ID: 24151390
yeah, one CFC would be a good idea.

Have you missed out a bit in your deleteImage function, it looks like it ends abruptly.

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 24154447
okay i tried but i encountered an error:
 Complex object types cannot be converted to simple values.
The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.

The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a cfif tag.
 
The error occurred in C:\Inetpub\wwwroot\simpleCFM\admin\cfcs\deleteZone.cfc: line 40
38 :   FROM tbl_product
39 :   WHERE cat_id IN 
40 :   (#ArrayToList(categories)#)
41 :   </cfquery>
42 :   <cfloop query="result">


The code i tried is somplete in the attach code snippet window:


<cfcomponent>
<cffunction name="getChildren" returntype="Array" output="no">
  <cfargument name="catID" required="yes" type="numeric">
  <cfset var result = "">
  <cfset var cat = ArrayNew(1)>
  <cfquery name="result" datasource="#request.dsn#">
  SELECT cat_id FROM tbl_category
  WHERE cat_parent_id = <cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.catId#">
  </cfquery>
  <cfloop query="result">
    <cfset ArrayAppend(cat, result.cat_id)>
    <!--- call this function again to find the children --->
    <cfset ArrayAppend(cat, getChildren(result.cat_id))>
  </cfloop>
  <cfreturn cat>
</cffunction>
<cffunction name="_deleteImage">
	<cfargument name="catID" default="" required="no" type="any">
    <cfset myset2 = "">
    <cfquery datasource="#request.dsn#" name="myset2">
    SELECT cat_image FROM tbl_category
    WHERE cat_id IN (<cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.catID#" list="yes">)
    </cfquery>
</cffunction>
<cffunction name="deleteCategory">
  <cfargument name="catID" required="yes" type="numeric">
  <!--- find all the children categories --->
  <cfset var children = getChildren(arguments.catId)>
  <!--- make an array containing this category and all it's children --->
  <cfset var categories = children>
  <cfset var numCategory = "">
  <cfset var result = "">
  <cfset ArrayAppend(categories, arguments.catId)>
  <cfset numCategory = ArrayLen(categories)>
  <!---  remove all product image & thumbnail if the product's category is in  categories --->
  <cfquery name="result" datasource="#request.dsn#">
  SELECT pd_id, pd_image, pd_thumbnail
  FROM tbl_product
  WHERE cat_id IN 
  (#ArrayToList(categories)#)
  </cfquery>
  <cfloop query="result">
    <cffile action="DELETE" file="#request.siteURL#products/images/#result.pd_image#">
    <cffile action="DELETE" file="#request.siteURL#products/thumbnails/#result.pd_thumbnail#">
  </cfloop>
  <!--- delete the products --->
  <cfquery datasource="#request.dsn#">
   DELETE FROM tbl_product
   WHERE cat_id IN (#ArrayToList(categories)#)
  </cfquery>
  <!--- then remove the categories image --->
  <cfset _deleteImage(categories)>
  <!--- // finally remove the category from database; --->
  <cfquery datasource="#request.dsn#">
  DELETE FROM tbl_category 
  WHERE cat_id IN (#ArrayToList(categories)#)
  </cfquery>
  <cfreturn void>
</cffunction>
</cfcomponent>
 
i invoked it like this:
 
<cfif isdefined('url.delete')>
  <cfinvoke component="#request.cfcPath#.deleteZone" method="deleteCategory" catID="#trim(url.delete)#"/>

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 24173506
experts please ping here, let check it
0
 
LVL 16

Expert Comment

by:duncancumming
ID: 24174301
The problem was with my code, It's basically created a nested array, but I think what we maybe want is just to use a list instead.

Your deleteImage function isn't doing anything meaningful, and either should be deleted or rewritten.

<cffunction name="getChildren" returntype="String" output="no">
        <cfargument name="catID" required="yes" type="numeric">
        
        <cfset var result = "">
        <cfset var cat = "">
        
        <cfquery name="result" datasource="dsn">
                SELECT cat_id
                FROM tbl_category
                WHERE cat_parent_id = #arguments.catId#
        </cfquery>
        
        <cfloop query="result">
                <cfset cat = ListAppend(cat, result.cat_id)>
                        
                <!--- call this function again to find the children --->
                <cfset cat = ListAppend(cat, getChildren(result.cat_id))>
        </cfloop>
 
        <cfreturn cat>
</cffunction>

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 24174377
Really Thanks, After a long wait i finally got some update on my question, changing the code for the getchildren caused another trouble in the DELETECATEGORY Function: i get the following error:



 Object of type class java.lang.String cannot be used as an array
 
The error occurred in C:\Inetpub\wwwroot\simpleCFM\admin\cfcs\deleteZone.cfc: line 40
Called from C:\Inetpub\wwwroot\simpleCFM\admin\category.cfm: line 38
Called from C:\Inetpub\wwwroot\simpleCFM\admin\category.cfm: line 1
Called from C:\Inetpub\wwwroot\simpleCFM\admin\functions.cfm: line 8
Called from C:\Inetpub\wwwroot\simpleCFM\admin\index.cfm: line 40
 
38 :   <cfset var numCategory = "">
39 :   <cfset var result = "">
40 :   <cfset ArrayAppend(categories, arguments.catId)>
41 :   <cfset numCategory = ArrayLen(categories)>
42 :   <!---  remove all product image & thumbnail if the product's category is in  categories --->

Open in new window

0
 
LVL 16

Accepted Solution

by:
duncancumming earned 500 total points
ID: 24174401
categories is now a list, not an array.  replace ArrayAppend with ListAppend, ArrayLen with ListLen, etc.  NB: be aware of the different syntax required for array and list functions

i.e.
<cfset ArrayAppend(yourArray, someValue)>
adds someValue onto yourArray

but the equivalent with a list is
<cfset yourList = ListAppend(yourList, someValue)>


0
 
LVL 16

Author Closing Comment

by:Gurpreet Singh Randhawa
ID: 31570405
Thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now