Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Porting From ColdFusion to PHP

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
Although I know PHP fairly well, I know nothing about ColdFusion and very little about arrays in either one. Can someone help me in porting over some code from ColdFusion to PHP?

What I have in ColdFusion is:

Code:
<cfoutput query="get_price"><cfset Tprice = price></cfoutput>

<!--- cart logic --->
<CFIF NOT IsDefined("SESSION.aCart") OR ArrayIsEmpty(SESSION.aCart)>
<CFSET SESSION.aCart = ArrayNew(1)>
</CFIF>

<CFSET i = ArrayLen(SESSION.aCart) + 1>
<CFSET SESSION.aCart[i] = STRUCTNEW()>
<CFSET SESSION.aCart[i].ItemID = FORM.ItemID>
<CFSET SESSION.aCart[i].Price = FORM.Price>
<CFSET SESSION.aCart[i].Quantity = FORM.Quantity>
<CFSET SESSION.aCart[i].SUB_TOTAL = FORM.Price * FORM.QUANTITY>

Obviously this is a shopping cart and is only a portion of the code although the rest is similarly simple.

What I have so far for creating the new array is:

Code:
session_start();
$_SESSION["aCart", array("ItemID" => array($ItemID, "Price" => $Price, "Quantity" => aGetParam("Quantity"), "SubTotal" => number_format($Price * aGetParam("Quantity"), "2", ".", "")))];

but I am uncertain how to add or delete items, or edit quantities when the same item is selected again or changed.

As mentioned, I don't know arrays well but it appears that this is creating only a single array, then replacing it when a new item is added to the cart. I've searched this site and the Internet but much that I've found seems overkill for what I am trying to do and/or is over my head.

Note that aGetParam() is a custom function for getting form values.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
To be most correct, in the CF code, SESSION is an object. That object contains the value aCart, which is itself an array.

The line that reads:
<CFSET i = ArrayLen(SESSION.aCart) + 1>

is getting the number of elements in the array and adding one. In short, by then saying:

CFSET SESSION.aCart = ....

It's appending a new element to the end of the array.

This part:
Code:
<!--- cart logic --->
<CFIF NOT IsDefined("SESSION.aCart") OR ArrayIsEmpty(SESSION.aCart)>
<CFSET SESSION.aCart = ArrayNew(1)>
</CFIF>

<CFSET i = ArrayLen(SESSION.aCart) + 1>
<CFSET SESSION.aCart[i] = STRUCTNEW()>
<CFSET SESSION.aCart[i].ItemID = FORM.ItemID>
<CFSET SESSION.aCart[i].Price = FORM.Price>
<CFSET SESSION.aCart[i].Quantity = FORM.Quantity>
<CFSET SESSION.aCart[i].SUB_TOTAL = FORM.Price * FORM.QUANTITY>

Would map to:

Code:
if (!isset($_SESSION['aCart']) OR count ($_SESSION['aCart']) == 0)
	$_SESSION['aCart'] = array()

$_SESSION['aCart'][] = array
(
	'ItemID'    => $_REQUEST['ItemID'],
	'Price'     => $_REQUEST['Price'],
	'Quantity'  => $_REQUEST['Quantity'],
	'SUB_TOTAL' => $_REQUEST['Price'] * $_REQUEST['QUANTITY']
);

Although I would replace the references to $_REQUEST to references within $_POST or $_GET as appropriate (whatever the "method" attribute of the submitting form is -- it's probably "POST")

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
the coldfusion looks more like it is creating a multidimensional array

Code:
$i = count($_SESSION['aCart']);
//you do not need the above line.  you could just leave out the $i in the line below and refer to it as [] instead  doing the above is more true to the original coldfusion code though (note php arrays are zero based so there is no need to increment the count to get a fresh index)

$_SESSION["aCart"][$i] = array (
             "ItemID"=>$ItemID,
             "Price"=>$Price,
             "Quantity"=>aGetParam(Quantity),
             "SubTotal"=>number_format($Price * aGetParam(Quantity))
             );
endfor;

to manipulate the array as the user changes things you need to keep track of the array index that is actually being targeted. you could most sensibly do this programmatically by tagging the relevant button or form action with the index number. for extra safety instead of just using an incremental counter you could create a unique id that you reference for each line item (here the index would be passed through in a hidden field or you could append it to a url as lineitem=####

Code:
//if the lineitem element is posted in then we know it is a change otherwise it is a new addition. obviously change post to get if you are going down the url route.

$line = isset($_POST['lineitem']) ? $_POST['lineitem'] : uniqid("line_");

$_SESSION["aCart"][$line] = array (
             "ItemID"=>$ItemID,
             "Price"=>$Price,
             "Quantity"=>aGetParam(Quantity),
             "SubTotal"=>number_format($Price * aGetParam(Quantity))
             );
 
oops - should have refreshed before posting...

sorry to duplicate.
 
Thanks everyone! I'll see what I can do with all the great information, which makes sense "even" to me. As for the index, I was thinking of using the item numbers ($ItemID). Actually, all the add form submits is the ItemID and quantity so a select from the MySQL table gets $Price and anything else needed.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
don't use itemid as that stops the user from adding the same item twice. yes i agree that users should not do this but instead should increase the quantity but there is no stopping users ... although you could trap this error and increment the quantity this may have the effect of confusing the users as they will be looking for two line items...
 
I would prefer to trap the error, as you said, and then increment an existing item in the cart rather than having the same item submitted twice. It's probably not a big deal as usually only a single item is purchased at a time anyway but I may want to put the code to other uses later where it would be better to not have duplicates for the same item. They may also want to increase the quantity of an item already added so I suppose the code would be similar.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
What ended up doing the trick was a nice tutorial on a simple cart that was found at another site.


It saves the info as a simple array of item IDs so, in the example below, there are three different items in the cart: three for ID 1 and one each for IDs 2 & 3.

1,1,3,1,2

However, I am having trouble submitting and saving the order to a table.

Code:
$cart = $_SESSION["aCart"];
	$OrderID = $_SESSION["OrderID"][;
	$conn = new clsDBconn();

if ($cart) {
	$items = explode(',', $cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}

	foreach ($contents as $id=>$qty) {
		$SQL = "INSERT INTO gift_orders (OrderID, ItemID, Quantity) VALUES ". $OrderID . ", " . $id . ", " . $qty;
		$conn->query($SQL);
	}
}

There are no errors when submitted but nothing inserts either. What am I doing wrong?

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
you need to put the values into quotes
Code:
$SQL = "INSERT INTO gift_orders (OrderID, ItemID, Quantity) VALUES '". $OrderID . "', '" . $id . "', '" . $qty ."'";
 
I found the problem. The SQL was missing the quotes around the values. I didn't think they were needed on numeric values but apparently they are!

Code:
foreach ($contents as $id=>$qty) {
   $SQL = "INSERT INTO gift_orders (OrderID, ItemID, Quantity) ". 
   "VALUES ('". $OrderID . "', '" . $id . "', '" . $qty . "')";
   $conn->query($SQL);
}

Now it inserts perfectly.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top