EasyCFM.COM ColdFusion Forums / PHP to CFML / Add To Cart Conversion

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: Add To Cart Conversion

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

Caliber
03-18-2011 @ 3:18 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Junior Member
Posts: 106
Joined: Sep 2003

Hey ECFers! I'm in need of your help once again. I
found this cool add to cart code that was done in PHP
and JQuery (
http://www.webre
sourcesdepot.com/wp-
content/uploads/file/jbasket/standard/
) and I
tried to convert it to coldfusion...key word tried!

ORIGINAL PHP CODE
========================

<?php require("db.php"); ?>
<?php

session_start();
$sessionID = $_COOKIE['PHPSESSID'];

if($_POST['action']
!= ' || $_GET['action'] != ') {
     if($_POST['action'] == ')
     {
          $action      = $_GET['action'];
          $productID     =
$_GET['productID'];
          $noJavaScript = 1;
     }
else {
          $action      = $_POST['action'];
          $productID     =
$_POST['productID'];
          $noJavaScript = 0;
     }
}
     
if
($action == "addToBasket"){

     $productInBasket      = 0;
     $productTotalPrice     =
0;

     $query  = "SELECT * FROM products WHERE
productID = " . $productID;
     $result = mysql_query($query);
     $row
= mysql_fetch_array( $result );

     $productPrice           = $row['productPrice'];     
     $productName          =
$row['productName'];     
     

     $query = "INSERT INTO baskets (productID,
productPrice, basketSession) VALUES ('$productID',
'$productPrice', '$sessionID')";
     mysql_query($query) or die('Error, insert query
failed');     

     $query  = "SELECT * FROM baskets WHERE
productID = " . $productID . " AND basketSession = '" .
$sessionID . "'";
     $result = mysql_query($query) or
die(mysql_error());;
     
     while($row = mysql_fetch_array($result,
MYSQL_ASSOC))
     {
          $totalItems      = $totalItems + 1;
          $productTotalPrice      =
$productTotalPrice
+ $row['productPrice'];
     }
     
     if ($noJavaScript == 1) {
          header("Location: ../index.php");
     } else {
          echo ('<li id="productID_' . $productID
. '"
>
<a href="inc/functions.php?
action=deleteFromBasket&productID=' . $productID . '"

onClick="return false;">
<img src="images/delete.png"
id="deleteProductID_' . $productID . '">
</a> ' .
$productName . '(' . $totalItems . ' items) - $' .
$productTotalPrice . '</li>');
     }

}


if ($action == "deleteFromBasket"){
     
     $query = "DELETE FROM baskets WHERE productID =
" . $productID . " AND basketSession = '" . $sessionID
. "'";
     mysql_query($query) or die('Error, delete query
failed');
          
     if ($noJavaScript == 1) {
          header("Location: ../index.php");
     }     
}


function getBasket(){
     
     session_start();
     $sessionID = $_COOKIE['PHPSESSID'];
     
     $query  = "SELECT * FROM baskets WHERE
basketSession = '" . $sessionID . "' GROUP BY productID
ORDER By basketID DESC";
     $result = mysql_query($query);
     //echo $query;
     
     while($row = mysql_fetch_array($result,
MYSQL_ASSOC))
     {
          
          $query2  = "SELECT * FROM products
WHERE productID = " . $row['productID'];
          $result2 = mysql_query($query2);
          $row2 = mysql_fetch_array( $result2 );
     
          $productID                =
$row2['productID'];
          $productPrice           =
$row2['productPrice'];     
          $productName          =
$row2['productName'];     
     
          $query2  = "SELECT COUNT(*) AS
totalItems FROM baskets WHERE basketSession = '" .
$sessionID . "' AND productID = " . $productID;
          $result2 = mysql_query($query2);
          $totalItems = $row2['totalItems'];
          $basketText = $basketText . '<li
id="productID_' . $productID . '">
<a
href=inc/functions.php?
action=deleteFromBasket&productID='
. $productID . '
onClick="return false;">
<img src="images/delete.png"
id="deleteProductID_' . $productID . '">
</a> ' .
$productName . '(' . $totalItems . ' items) - $' .
($totalItems * $productPrice) . '</li>';
          
     }
     echo $basketText;
}
     

?>



COLDFUSION CONVERSION CODE
============================

<cffunction name="session_start" access="public"
output="false">

     
     <cfset var sessionID = "cookie.phpsessID" />
     <cfset var session = "" />
     
          <cfif form.action NEQ ' OR url.action
NEQ '>

               <cfif form.action EQ '>
                    <cfset action =
'url.action'
/>

                    <cfset productID =
'url.productID'
/>

                    <cfset noJavaScript = 1
/>

               <cfelse>
                    <cfset action =
'form.action'
/>

                    <cfset productID =
'form.productID'
/>

                    <cfset noJavaScript = 0
/>

               </cfif>
          </cfif>
     
          <cfif action EQ "addToBasket">
               
               <cfset productInBasket = 0 />
               <cfset productTotalPrice = 0 />
               
               <cfquery name="getAllProducts"
datasource="jBasket">

                    SELECT          *
                      FROM          
products
                     WHERE          
productID = '#productID#'
               </cfquery>
               
               <cfloop query="getAllProducts">
                    #productPrice#
#productName#
               </cfloop>
               
               <cfquery>
                    INSERT INTO baskets
(productID, productPrice, basketSession)
                    VALUES('#productID#',
'#productPrice#', '#sessionID#')
               </cfquery>     
               
               <cfquery name="getAllBaskets"
datasource="jBasket">

                    SELECT          *
                      FROM        baskets
                     WHERE          
productID = '#productID#'
                       AND          
basketSession = '#sessionID#'
               </cfquery>     
               
               <cfloop query="getAllBaskets">
                    <cfset totalItems =
totalItems
+ 1 />

                    <cfset
productTotalPrice = productTotalPrice +
getAllBaskets.productPrice />

               </cfloop>
               
               <cfif noJavaScript EQ 1>
                    <cflocation
url="../index.cfm">

               <cfelse>
                    <li
id="productID=#productID#">
<a href="inc/functions.cfm?
action=deleteFromBasket&productID=#productID#"

onclick="return false;"
                    <img
src="images/delete.png"
id="deleteProductID=#productID#">
</a>#productName#
(#totalItems# items) - #productTotalPrice#</li>
               </cfif>
          </cfif>
          
          <cfif action EQ "deleteFromBasket">
               <cfquery>
                     DELETE FROM          
baskets
                           WHERE          
productID = '#productID#'
                              AND          
basketSession = '#sessionID#'
               </cfquery>
               
               <cfif noJavaScript EQ 1>
                    <cflocation
url="../index.cfm">

               </cfif>
          </cfif>          
          
     <cfreturn session>     
     
</cffunction>


<cffunction name="getBasket" access="public"
output="false">

     
     <cfset var basketText = "" />
     
     <cfset session_start() />
     <cfset sessionID = cookie.phpsessid />
     
     <cfquery name="getBaskets"
datasource="jBasket">

          SELECT          *
            FROM          baskets
           WHERE          basketSession =
'#sessionID#'
       GROUP BY          productID
       ORDER BY          basketID DESC
     </cfquery>
     
     <cfloop query="getBaskets">
          <cfquery name="getProducts"
datasource="jBasket">

               SELECT          *
                 FROM          products
                WHERE          productID =
'#productID#'
          </cfquery>
          
          <cfset productID = '#productID#' />
          <cfset productPrice = '#productPrice#'
/>

          <cfset productName = '#productName#' />
          
          <cfquery name="getItemCount"
datasource="jBasket">

               SELECT COUNT(*) AS totalItems
                FROM                     
baskets
               WHERE                    
basketSession = '#sessionID#'
                 AND                    
productID = '#productID#'
          </cfquery>
          
          <cfset totalItems =
#getItemCount.totalItems#
/>

          
          <cfset basketText = '<li
id="productID=#productID#">
<a href="inc/functions.cfm?
action=deleteFromBasket&productID=#productID#"

onclick="return false;"
          <img src="images/delete.png"
id="deleteProductID=#productID#">
</a>#productName#
(#totalItems# items) - DollarFormat((#totalItems# *
#productPrice#))</li>' />
     </cfloop>
          
     <cfreturn basketText>
     
</cffunction>







It is hard to think outside the box when working with pixels - E.A. Williams


Website Designed and Developed by Pablo Varando.