Time:
Scotland: Fri, 10:41 pm
Rhode Island: Fri, 5:41 pm
Florida: Fri, 5:41 pm
New Mexico: Fri, 3:41 pm
California: Fri, 2:41 pm

Click here to visit Livelife365.com

Click here to visit nmdarts.com



Buy this Ad Space.

180px wide.

Please get in touch with KH@ if you are interested and make an offer.

CLICK HERE TO GET AUCTION BAR NOW
US$10 per year - Save $100s!
The Fabulously Unfair
WebX Auction Bar. For Ebay etc.
Ro-Sham-Bo the opposition. Laugh like Eric Cartman when you win! CLICK HERE NOW!


More information and sign-up.

WebXpertz Hosting.
Custom fit from $5pm. PHP/MySQL
You'll save money, we'll save money. Seems fair to me. Interested? If so Please PM me here and tell me what you need. Thanks!


Please click here for more information

For continued disscussion on this topic : quantity in shopping cart


cfnewbie23
04-23-2004, 07:13 AM
Can someone help me on how to store the quantity information. I don't want to add a duplicate entry into the array but rather incrementing it. I've put some code but I get an error saying "Variable I is not defined". I can't find the error

Here is the code:

<head>
<title>Book Info</title>
<style type = "text/css">
table { border-collapse: collapse }
td { border: 2px solid lightgray; padding: 5px }
</style>
</head>
<body>
<!--- Make sure that an ISBN was submitted to this page --->
<cfif NOT isDefined( "form.ISBN" )>

<!--- CFML URL Forwarder --->
<cflocation url = "booklist.cfm" addtoken = "no">
<cfabort>
</cfif>

<!--- CFML Query using SQL on 'books' DSN --->
<!--- Dynamically insert form.ISBN variable --->
<cfquery name = "bookInfo" datasource = "books">
SELECT ISBN, Title, EditionNumber, Copyright, Description,
ImageFile, PublisherName
FROM Titles, Publishers
WHERE Titles.ISBN = '#form.ISBN#'
AND Titles.PublisherID = Publishers.PublisherID
</cfquery>

<!--- Using cfoutput with a query to loop result set --->
<cfoutput query = "bookInfo">

<!--- Based on ISBN, use SQL to retrieve authors --->
<cfquery name = "authorInfo" datasource = "books">
SELECT FirstName, LastName
FROM AuthorISBN, Authors
WHERE AuthorISBN.isbn = '#bookInfo.ISBN#'
AND AuthorISBN.AuthorID = Authors.AuthorID
</cfquery>
<h1>#bookInfo.Title#</h1>
<hr />

<!--- Display authors by using a CFML Loop --->
<cfloop query = "authorInfo">
#authorInfo.FirstName# #authorInfo.LastName# <br />
</cfloop>

<!---Output information from database about book --->
<table>
<tr>
<td>ISBN:</td>
<td>#bookInfo.ISBN#</td>
<td rowspan ="4">

<!--- Dynamically insert the filename into the tag --->
<img src = "images/#bookInfo.ImageFile#" alt = "Image File" />
</td>
</tr>
<tr>
<td>Edition:</td>
<td>#bookInfo.EditionNumber#</td>
</tr>
<tr>
<td>Copyright:</td>
<td>#bookInfo.Copyright#</td>
</tr>
<tr>
<td>Publisher:</td>
<td>#bookInfo.PublisherName#</td>
</tr>
</table>
#bookInfo.Description#

<!--- Used in Section 27.8 --->
<!--- Button to add book to shopping cart --->
<form action = "shoppingcart.cfm" method = "post">
<input type = "hidden" name = "ISBN" value = "#bookInfo.ISBN#" />
<input type = "hidden" name = "title" value = "#bookInfo.Title#" />
<input type = "hidden" name = "edition" value="#bookInfo.EditionNumber#" />
<input type = "submit" value = "Add To Cart"
!--disabled = "disabled" />
</form>
</cfoutput>

<!--- Button to go back to the book list --->
<form action = "booklist.cfm" method = "post">
<input type = "submit" value = "Book List" />
</form>
</body>

Thanks!

KH@
04-23-2004, 02:40 PM
Hi, Diades sent me (forever the optimist :) ) but I'm not sure I understand your question.

I looked at the code but obviously I can't run it without the datasource, however something looks a bit odd in that you are running a loop within CFOUTPUT which is itself a loop. Never tried that myself but it could be that CFOUTPUT doesn't like it. The undefined variable "I" in your error message may refer to the "I" used in a list loop rather than a query loop but that is just a shot in the dark.

normally a query loop contains CFoutput tags as follows:

EG: <CFLOOP QUERY="MessageRecords">
<CFOUTPUT>#Message_ID#</CFOUTPUT><BR>
</CFLOOP>

Yours does not ie:

<!--- Display authors by using a CFML Loop --->
<cfloop query = "authorInfo">
#authorInfo.FirstName# #authorInfo.LastName# <br />
</cfloop>

If you put CFoutput tags in you will get another error message because you can't have cfoutput tags within cfoutput tags :) but you can try this.

Create a new file called say... loop.cfm

Then put your loop code in the new file with output tags as follows.

<!--- Display authors by using a CFML Loop --->
<cfloop query = "authorInfo">
<CFOUTPUT>#authorInfo.FirstName# #authorInfo.LastName# <br /></CFOUTPUT>
</cfloop>

Then replace your original loop code with an include

<cfinclude template="loop.cfm">

This may allow you to use the CFOUTput tags in your loop without an error as the code in loop.cfm will be run seperately.

However this is a real shot in the dark as I don't really follow your ref. to incremented arrays and quantities. I'm a bit slow today. :bambooz:

I'm not sure how much CF you have done but CFINCLUDE is a handy tag to use if you wish to fusebox your application. You may like to take a look at CFMODULE and CF custom tags too. Both of these tags allow you to pass variables to and from your application using the attributes.scope and as the code is in seperate files, it is processed independantly.

There is a book I read about Fuseboxing your applications called:

FUSEBOX: Methodology and Techniques (ColdFusionEdition) by Steve Nelson and Craig Girard

I think I got it from Amazon. It has been very handy. If you haven't already been, you might like to visit Ben Forta's web site at http://www.forta.com/ I learnt CF from his books.

cfnewbie23
04-23-2004, 11:32 PM
Hi KH@, I have attached the database. Can you try run the program and tell me what else could be wrong?

Thanks!!

KH@
04-24-2004, 08:22 AM
Hello again,

I installed your sample dBase and ran your code after deleting/disabling the ISBN checker and redirect at the top and replacing it with an ISBN value from your dBase. IE <cfset form.ISBN="0-13-012507-5">

This is the code I am running.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Book Info</title>
<style type = "text/css">
table { border-collapse: collapse }
td { border: 2px solid lightgray; padding: 5px }
</style>
</head>
<body>
<!--- Make sure that an ISBN was submitted to this page --->

<!--- CFML URL Forwarder

DELETED/DISABLED

<cfif NOT isDefined( "form.ISBN" )>
<cflocation url = "booklist.cfm" addtoken = "no">
<cfabort>
</cfif>

--->

<!--- INSTEAD

Pretend an ISBN has been passed from a form --->

<cfset form.ISBN="0-13-012507-5">

<!--- CFML Query using SQL on 'books' DSN --->
<!--- Dynamically insert form.ISBN variable --->
<cfquery name = "bookInfo" datasource = "books">
SELECT ISBN, Title, EditionNumber, Copyright, Description,
ImageFile, PublisherName
FROM Titles, Publishers
WHERE Titles.ISBN = '#form.ISBN#'
AND Titles.PublisherID = Publishers.PublisherID
</cfquery>

<!--- Using cfoutput with a query to loop result set --->
<cfoutput query = "bookInfo">

<!--- Based on ISBN, use SQL to retrieve authors --->
<cfquery name = "authorInfo" datasource = "books">
SELECT FirstName, LastName
FROM AuthorISBN, Authors
WHERE AuthorISBN.isbn = '#bookInfo.ISBN#'
AND AuthorISBN.AuthorID = Authors.AuthorID
</cfquery>
<h1>#bookInfo.Title#</h1>
<hr />

<!--- Display authors by using a CFML Loop --->
<cfloop query = "authorInfo">
#authorInfo.FirstName# #authorInfo.LastName# <br />
</cfloop>

<!---Output information from database about book --->
<table>
<tr>
<td>ISBN:</td>
<td>#bookInfo.ISBN#</td>
<td rowspan ="4">

<!--- Dynamically insert the filename into the tag --->
<img src = "images/#bookInfo.ImageFile#" alt = "Image File" />
</td>
</tr>
<tr>
<td>Edition:</td>
<td>#bookInfo.EditionNumber#</td>
</tr>
<tr>
<td>Copyright:</td>
<td>#bookInfo.Copyright#</td>
</tr>
<tr>
<td>Publisher:</td>
<td>#bookInfo.PublisherName#</td>
</tr>
</table>
#bookInfo.Description#

<!--- Used in Section 27.8 --->
<!--- Button to add book to shopping cart --->
<form action = "shoppingcart.cfm" method = "post">
<input type = "hidden" name = "ISBN" value = "#bookInfo.ISBN#" />
<input type = "hidden" name = "title" value = "#bookInfo.Title#" />
<input type = "hidden" name = "edition" value="#bookInfo.EditionNumber#" />
<input type = "submit" value = "Add To Cart"
!--disabled = "disabled" />
</form>
</cfoutput>

<!--- Button to go back to the book list --->
<form action = "booklist.cfm" method = "post">
<input type = "submit" value = "Book List" />
</form>
</body>
</html>


When I run the code CF spits out the following HTML page, without errors as far as I can see.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Book Info</title>
<style type = "text/css">
table { border-collapse: collapse }
td { border: 2px solid lightgray; padding: 5px }
</style>
</head>
<body>




<h1>Java How to Program</h1>
<hr />


Harvey Deitel <br />
Paul Deitel <br />



<table>
<tr>
<td>ISBN:</td>
<td>0-13-012507-5</td>
<td rowspan ="4">


<img src = "images/0130125075.jpg" alt = "Image File" />
</td>
</tr>
<tr>
<td>Edition:</td>
<td>3</td>
</tr>
<tr>
<td>Copyright:</td>
<td>1999</td>
</tr>
<tr>
<td>Publisher:</td>
<td>Prentice Hall</td>
</tr>
</table>
Learn to program in the Java programming language. This edition has been updated to the Sun Microsystems Java 2 platform (formerly called Java 1.2). The book includes the Swing GUI components and new chapters on JDBC, Servlets, RMI, Collections and JavaBeans.



<form action = "shoppingcart.cfm" method = "post">
<input type = "hidden" name = "ISBN" value = "0-13-012507-5" />
<input type = "hidden" name = "title" value = "Java How to Program" />
<input type = "hidden" name = "edition" value="3" />
<input type = "submit" value = "Add To Cart"
!--disabled = "disabled" />
</form>



<form action = "booklist.cfm" method = "post">
<input type = "submit" value = "Book List" />
</form>
</body>
</html>


So can you explain exactly where you are having trouble, and what with. ???? :bambooz:

cfnewbie23
04-24-2004, 11:18 AM
What i need help on is to modify the code to store quantity information along with the currently stored information. Also, ensure that adding a duplicate book to the cart results in incrementing the quantity of that book instead of adding a duplicate entry to the array.


THanks!

KH@
04-24-2004, 04:58 PM
If I understand you correctly then you need to allow the visitor to say how many copies they want then modify the code in shoppingcart.cfm

The quantity could be included as below.

<cfform action = "shoppingcart.cfm" method = "post">

<input type = "hidden" name = "ISBN" value = "#bookInfo.ISBN#" />

<input type = "hidden" name = "title" value = "#bookInfo.Title#" />

<input type = "hidden" name = "edition" value="#bookInfo.EditionNumber#" />

<input type = "submit" value = "Add To Cart"
!--disabled = "disabled" />

<p>Quantity <cfinput type="Text" name="quantity" value="1" message="Please enter a number between 1 and 999 in the quantity box" validate="integer" required="Yes" size="3" maxlength="3"></p>

</cfform>

Is this what you mean?

cfnewbie23
04-25-2004, 05:39 AM
I think thats what i mean, but is there anything in my code that I should delete? I added those lines you gave me, but it seems not to work. Did you try to run it?

KH@
04-25-2004, 09:53 AM
I am confused again. :(

The page of code you posted only does this...

It accepta a passed parameter (ISBN) from another page (probably a booklist) then collects information pertaining to the passed ISBN number from your database.

It then displays the information collected to the visitor and offers two options.

A/ buy the book (add to cart)
B/ go back to the booklist

Your page is doing all of this just fine.

If the visitor chooses to buy the book they click the relevent button. Then the purchase information is passed to another cfm page called shoppingcart.cfm.

shoppingcart.cfm will then process the information passed to it.

In short, the page you posted only SELECTS and DISPLAYS book information and it seems to work fine.

If your shopping cart page isn't working that is another matter.

May I ask if you wrote this code yourself? I have been presuming you did. If you are trying to modify someone elses application code then I think you may be looking at the wrong page for the problem which would explain why I am confused. :)

cfnewbie23
04-25-2004, 01:04 PM
Actually, its an exercise problem. I got that code from this worksheet and the problem asks to modify it. I don't know coldfusion that much because my teacher didn't teach us. The problem asks to modify the shoppingcart.cfm.

KH@
04-25-2004, 08:05 PM
Okey Dokey :)

Post the Question and the code supplied for shopingcart.cfm and lets look at it and see if we can get an "A" :cool:

cfnewbie23
04-26-2004, 01:44 AM
Its due tomorrow and i hope to get it done by tonight =)

Here is the code:

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!--- Fig. 27.14 : shoppingcart.cfm --->
<!--- Bookstore - Simple Shopping Cart --->
<cfif isDefined( "form.ISBN" )>
<!--- Add a new book to the shopping cart. --->
<cfif arrayAppend( session.shoppingCart[ 1 ], form.ISBN )></cfif>
<cfif arrayAppend( session.shoppingCart[ 2 ], form.title )></cfif>
<cfif arrayAppend( session.shoppingCart[ 3 ], form.edition )></cfif>
<cfelseif isDefined( "form.clearCart" )>
<!--- Empty the shopping cart. --->
<cfif arrayClear( session.shoppingCart )></cfif>
</cfif>
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Bookstore Shopping Cart</title>
<style type = "text/css">
table { border-collapse: collapse }
td { border: 2px solid lightgray;
padding: 5px }
</style>
</head>
<body>
<h1>Shopping Cart</h1>
<!--- Check the size of the shopping cart --->
<cfset arraySize = arrayLen( session.shoppingCart[ 1 ] )>
<cfif arraySize GTE 1>
<table>
<!--- Display the items in the shopping cart --->
<cfloop index="i" from="1" to="#arraySize#" step="1">
<cfoutput>
<tr>
<td>#session.shoppingCart[ 1 ][ i ]#</td>
<td>#session.shoppingCart[ 2 ][ i ]#</td>
<td>Edition #session.shoppingCart[ 3 ][ i ]#</td>
</tr>
</cfoutput>
</cfloop>
</table>
<cfelse>
<p>Empty Shopping Cart.</p>
</cfif>
<!--- Button back to the book list --->
<form action = "booklist.cfm" method = "post">
<p><input type = "submit" value = "Book List" /></p>
</form>
<!--- Button to clear the shopping cart --->
<form action = "shoppingcart.cfm" method = "post">
<p>
<input name = "clearCart" type = "submit"
value = "Empty Cart" />
</p>
</form>
</body>
</html>

THe question is:
Modify the code above to store quantity information along with the currently stored information. Also, ensure that adding a duplicate book to the cart results in incrementing the quantity of that book instead of adding a duplicate entry to the array.

Hope this helps. Thanks!

KH@
04-26-2004, 08:51 AM
Hi, I think your application is using an array to store purchase information in the SESSION.SCOPE. I am guessing that your set of application files includes a file named Application.cfm which enables session management and creates the shopping cart array. I don't have this so I can't run the code or find out the structure of the array already created. However, I can take a rough guess that the quantity value may be stored in session.shoppingCart[ 4 ] but that may not be the case.

The section of code you need to modify is this bit.

<!--- Add a new book to the shopping cart. --->
<cfif arrayAppend( session.shoppingCart[ 1 ], form.ISBN )></cfif>
<cfif arrayAppend( session.shoppingCart[ 2 ], form.title )></cfif>
<cfif arrayAppend( session.shoppingCart[ 3 ], form.edition )></cfif>

F.Y.R.

ArrayAppend

Syntax : ArrayAppend(array, value)

Appends an array index to the end of the specified array.
Returns a Boolean TRUE on successful completion.


What I think you need to do is

check session.shoppingCart[ 1 ] to see if it contains the ISBN of the book to be added.

If it does you need to find the position of that particular ISBN in the array,

then get the corrisponding quantity value from session.shoppingCart[ 4 ] , then add 1 to it.

Then replace the original value with the new one.

Else if the ISBN is not already in session.shoppingCart[ 1 ] then this is a new book so just add the new info to the array with quantity = to 1


If there is time left you could ZIP up and attach the whole set of application files so I can see the full picture and run the code, otherwise you better just give it your best shot and hope for the best.

cfnewbie23
04-26-2004, 10:43 AM
I zipped up the files. I'll try to do it also. I've got an extension so its due tuesday =).

Thanks!!

KH@
04-26-2004, 11:50 AM
OK got it. This might take a while as I have to work my way through the app to understand how it works before attempting to add the code required to satisfy your assignment.

If I need further info I'll yell. :)

PS. Do not rely on me to fix it :D

KH@
04-26-2004, 02:07 PM
Please note:

I am not clear on using cfif to append the array as I never used this method so my solution may not be what your teacher expects. However that said, this solution works.

If you don't understand what I've done please ask. If the solution is too different than your teacher expects he/she may ask you what you did and why. :eek:

Basically the code works like this.

Your ISBN is a unique value for each book

Your shopping cart is a 2 dimentional array. Think of it as a table.

So if you loop through the the ISBNs in the array and compare (test) each to the new books ISBN you can discover if a book has already been added and what its number is.

I created a variable called "position" that is incremented if the test fails for an existing ISBN. And a variable called stop that stops incrementing the "position" variable when the test finds a matching ISBN.

Therefore after the loop is completed the variable "position" will contain the right number of the matching book if any.

If the test finds a match then only the quantity needs updating

If the loop completes without finding a match then arraySize will = position -1 and therefore when this is true you know you need to add a new book.

To test the new code. make sure the shopping cart is empty then add three different books. Each new book creates a new row in the array. Next try adding any book for the second time. When you do the quantity for the duplicate book will increase by 1.

___________________-

Only shoppingcart.cfm needed changing. heres the new code.



<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<!--- Fig. 27.14 : shoppingcart.cfm --->
<!--- Bookstore - Simple Shopping Cart --->





<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Bookstore Shopping Cart</title>

<style type = "text/css">
table { border-collapse: collapse }
td { border: 2px solid lightgray;
padding: 5px }
</style>
</head>

<body>

<cfif isDefined( "form.ISBN" )>

<cfset arraySize = arrayLen( session.shoppingCart[ 1 ] )>

<!--- first loop though the array to see if the book has been added previously and create a variable called poition to store the position of the book if it has --->

<cfset position = "1">
<cfset stop = "no">
<cfloop index="i" from="1" to="#arraySize#" step="1">
<cfoutput>
<cfif (form.ISBN is #session.shoppingCart[ 1 ][ i ]#)>
<cfset stop = "yes">
<cfelse>
<cfif stop is "no">
<cfset position = evaluate(position + 1)>
</cfif>
</cfif>
</cfoutput>
</cfloop>

<cfoutput>

<!--- if this IF is true then its a new book so add it--->

<cfif arraySize is evaluate(position-1)>
<!--- Add a new book to the shopping cart if its a new one --->
<cfif arrayAppend( session.shoppingCart[ 1 ], form.ISBN )></cfif>
<cfif arrayAppend( session.shoppingCart[ 2 ], form.title )></cfif>
<cfif arrayAppend( session.shoppingCart[ 3 ], form.edition )></cfif>
<cfif arrayAppend( session.shoppingCart[ 4 ], "1" )></cfif>
<cfelse>
<!--- if its not true then its an already added book so increment the quantity of the existing book --->

<cfset session.shoppingCart[ 4 ] [position] = evaluate(session.shoppingCart[ 4 ] [position] + 1)>

</cfif>

</cfoutput>




<cfelseif isDefined( "form.clearCart" )>

<!--- Empty the shopping cart. --->
<cfif arrayClear( session.shoppingCart )></cfif>
</cfif>


<h1>Shopping Cart</h1>

<!--- Check the size of the shopping cart --->
<cfset arraySize = arrayLen( session.shoppingCart[ 1 ] )>

<cfif arraySize GTE 1>
<table>

<!--- Display the items in the shopping cart --->
<cfloop index="i" from="1" to="#arraySize#" step="1">
<cfoutput>
<tr>
<td>#session.shoppingCart[ 1 ][ i ]#</td>
<td>#session.shoppingCart[ 2 ][ i ]#</td>
<td>Edition #session.shoppingCart[ 3 ][ i ]#</td>
<td>Quantity #session.shoppingCart[ 4 ][ i ]#</td>
</tr>
</cfoutput>
</cfloop>
</table>
<cfelse>
<p>Empty Shopping Cart.</p>
</cfif>

<!--- Button back to the book list --->
<form action = "booklist.cfm" method = "post">
<p><input type = "submit" value = "Book List" /></p>
</form>

<!--- Button to clear the shopping cart --->
<form action = "shoppingcart.cfm" method = "post">
<p>
<input name = "clearCart" type = "submit"
value = "Empty Cart" />
</p>
</form>
</body>
</html>

<!--
**************************************************************************
* (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************
-->