Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp while loop  RSS feed

 
shaz akhtar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, ive gone abit of a problem here. I have 2 tables in mysql..products and download. The products table exists with idnumber..title etc and the download table exists of idnumber and username. So i have joined these tables using the idnumber! what i want to intially do is.. go through each product and see how many times it has been downloaded and get a total price figure for each product.

mysql statment is:
queryStr1 = "select * from products, download where products.idnumber = download.idnumber and products.title = 'IROBOT'";

(i have just hardcoded the title in for now)

current i have got:

while (rs1.next())
{
String title = rs1.getString(2);
price = Float.parseFloat(rs1.getString(6));
total++;


} // end while ()
sumtotal = total * price;

%>
<tr>

<td valign = 'Top'> <%=title%> </td>
<td valign = 'Top'>� <%=sumtotal%> </td>
</tr>

ok what it does here is..it adds the total for 1 product..but i gte an error!..it display's the sumtotal..but getting error displaying the title outside the while loop. If i display the title inside the while loop i get the title repeating how many times the product has been downloaded...any suggestions???

plus this is only for one product..how would i possibly get this repeating for all products? so i get the different title names of the product and a total figure for each?

Many thanks in advance

Shaz
 
Anil Sadi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shaz,

The following code may work after simple syntax error fixes:

queryStr1 = "select * from products, download where products.idnumber = download.idnumber and products.title = 'IROBOT' order by products.title";

String title = "";
String oldTitle = "";
float totalPrice = 0.0f;
float price = 0.0f;

while (rs1.next())
{
String title = rs1.getString(2);
if(oldTitle.equals("") || oldTitle.equals("title")) {
price = Float.parseFloat(rs1.getString(6));
totalPrice += price;
oldTitle = title;
} else { %>
<tr>
<td valign = 'Top'> <%=oldTitle%> </td>
<td valign = 'Top'>� <%=totalPrice%> </td>
</tr>
<%totalPrice = 0;
oldTitle = title;
}

} // end while ()

%>


Anil S.
 
shaz akhtar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anil, thanks for ur reply

that didnt work..what it was doing was printing each dvd title..(ie how many times its been downloaded) and just display's the current price such as 25.00 for the first title and repeats itself till the end of titles.

i have changed my sql statment to
queryStr1 = "select * from products, download where products.idnumber = download.idnumber order by products.title";

i will need to somehow calculate one dvd total (ie how many times its been downloaded * price)..and then move on to the next..is this possible?

thanks shahnaz
 
Anil Sadi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shaz,

This is quite possible. There is a small problem in code. Please correct the following.

Change condition to 'if(oldTitle.equals("") || oldTitle.equals(title))' from
'if(oldTitle.equals("") || oldTitle.equals("title"))'

Hope it works!!!

Anil S.
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JSP forum.

Mark
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!