Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

a Code php which i am trying to convert into coldfusion

Posted on 2009-04-15
12
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 
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 2000 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

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

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

Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

609 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