我们有一个脚本,我们的开发人员为php提供了从我们的数据库中生成最畅销的列表,但我们需要它在coldfusion中!
是否有一种简单的转换方式或者是否需要完全重写?
提前感谢任何建议: - )
// // ---------- // Get Top Selling Products (by sku) // ---------- //
function CWgetBestSelling($max_products=5, $sub_ids=0)
{
$productQuery = '';
$returnQuery = '';
$idList = '0';
$itemsToAdd = '';
if (!is_numeric($idList[0])) {
$idList = '0';
}
$q_productQuery = mysql_query( "
SELECT count(*) as prod_counter,
p.product_id,
p.product_name,
p.product_preview_description,
p.product_date_modified
FROM cw_products p
INNER JOIN cw_order_skus o
INNER JOIN cw_skus s
WHERE o.ordersku_sku = s.sku_id
AND s.sku_product_id = p.product_id
AND NOT p.product_on_web = 0
AND NOT p.product_archive = 1
AND NOT s.sku_on_web = 0
GROUP BY product_id
ORDER BY prod_counter DESC
LIMIT ".$max_products
,$_ENV["request.cwapp"]["db_link"]);
$productQuery = array();
while ($qd = mysql_fetch_assoc($q_productQuery)) {
$productQuery[] = $qd;
}
// add values to list
foreach ($productQuery as $values) {
$idList = $values['product_id'] . "," . $idList;
}
// if not enough results, fill in from sub_ids
if (count($productQuery) < $max_products) {
// number needed
$itemsToAdd = $max_products - count($productQuery);
for ($i = 1; $i <= $itemsToAdd; $i++) {
if (substr_count($sub_ids, ',') >= $i) {
$idListArray = explode(',', $sub_ids);
$idList .= "," . $idListArray[$i];
}
}
$q_resultsQuery = mysql_query("
SELECT 0 as prod_counter,
p.product_id,
p.product_name,
p.product_preview_description,
p.product_date_modified
FROM cw_products p
WHERE p.product_id in(".CWqueryParam($idList).")
AND NOT p.product_on_web = 0
AND NOT p.product_archive = 1
ORDER BY product_date_modified DESC
",$_ENV["request.cwapp"]["db_link"]);
} else {
$q_resultsQuery = mysql_query("
SELECT count(*) as prod_counter,
p.product_id,
p.product_name,
p.product_preview_description,
p.product_date_modified
FROM cw_products p
INNER JOIN cw_order_skus o
INNER JOIN cw_skus s
WHERE o.ordersku_sku = s.sku_id
AND s.sku_product_id = p.product_id
AND NOT p.product_on_web = 0
AND NOT p.product_archive = 1
AND NOT s.sku_on_web = 0
GROUP BY product_id
ORDER BY prod_counter DESC, product_date_modified
",$_ENV["request.cwapp"]["db_link"]);
}
while ($qd = mysql_fetch_assoc($q_resultsQuery)) {
$returnQuery[] = $qd;
}
return $returnQuery;
}
答案 0 :(得分:6)
代码转换问题不会长时间保持开放,因为它们被视为懒惰。所以,这里有一些参考来帮助你入门。我不是PHP专家,但在快速浏览一下你的代码后,我认为这个链接列表会给你一个良好的开端。
为了不为你工作并给你机会学习,我会提供一些样本而不是整个代码,这样你就可以了解你的PHP适合CF的地方。这也是标签版本,而不是脚本版本。
<cffunction name = "CWgetBestSelling" ...>
<cfargument name = "max_products" default = "5" ...>
<cfargument ...>
<cfset var local.productQuery = "">
<cfset var local.returnQuery = "">
<cfset ...>
<cfset ...>
<cfquery name = "q_productQuery " datasource = "yourDatasource">
SELECT
count(*) as prod_counter,
p.product_id,
p.product_name,
p.product_preview_description,
p.product_date_modified
FROM
cw_products p
INNER JOIN cw_order_skus o
INNER JOIN cw_skus s
WHERE
o.ordersku_sku = s.sku_id
AND s.sku_product_id = p.product_id
AND NOT p.product_on_web = 0
AND NOT p.product_archive = 1
AND NOT s.sku_on_web = 0
GROUP BY
product_id
ORDER BY
prod_counter DESC
LIMIT #arguments.max_products#
</cfquery>
...
...
...
<cfreturn yourReturnVariable>
</cffunction>