Result like a group with Classic ASP - asp-classic

I have a table (MSSQL) with data like:
1 AAA 100
2 BBB 101
3 C 100
4 D 100
I would like to list itens like:
100:
- 1 AAA
- 3 C
- 4 D
101:
- 2 BBB
How can I do this? I have done a While inside a While but the result isn't what I am waiting:
<%
Set Group= TarefasConexaoMSSQL.Execute("SELECT DISTINCT GroupName FROM _NewDummy")
GroupName= Group("GroupName")
WHILE NOT Group.EOF
%>
<p>
<% Response.Write Group("GroupName") %>
</p>
<%
Set Itens= TarefasConexaoMSSQL.Execute("SELECT * FROM _NewDummy WHERE GroupName= '"& GroupName &"'")
One= Itens("One")
Two= Itens("Two")
%>
<%
WHILE NOT Itens.EOF
%>
- One: <% Response.Write One %>
- Two: <% Response.Write Two %>
<%
Itens.MOVENEXT
WEND
%>
<%
Group.MOVENEXT
WEND
%>
Thanks for any info on this.

You don't need any nested loop here, just sort by the group name and keep track of the previous value vs. the current value. When the value changes (or first value) show the group name.
Code:
<%
Set oRS= TarefasConexaoMSSQL.Execute("SELECT * FROM _NewDummy ORDER BY GroupName ASC")
Dim currentGroupName, previousGroupName
currentGroupName = ""
previousGroupName = ""
Do Until oRS.EOF
currentGroupName = oRS("GroupName")
One = oRS("One")
Two = oRS("Two")
If currentGroupName<>previousGroupName Then
Response.Write("<p>")
Response.Write("" & currentGroupName & "")
Response.Write("</p>")
End If
Response.Write("- One: " & One & ", Two: " & Two & "<br />")
previousGroupName = currentGroupName
oRS.MoveNext
Loop
oRS.Close
%>

Related

Print data in table like single row with multiple column in classic asp?

Like the date with would print in column 1 to 30, I need need employee login time and log off time in single days column ...``
I need to print data in table like single row with multiple columns in classic asp
<form name="frm_display" method="post">
Welcome: <%=Session("emp_fname")%>
<br>ID: <%=Session("emp_id")%>
<%
dim con
dim query
dim rs2
set con = Server.CreateObject("ADODB.Connection")
set rs2 = Server.CreateObject( "ADODB.RECORDSET")
con.Open "Provider=SQLOLEDB.1;Password=swtest.net;Persist Security Info=True;User ID=sa;Initial Catalog=hr;Data Source=172.16.1.208"
%>
<table align="center">
<tr>
<td>Month:
<select name="selmonth">
<%for i=1 to 12%>
<%if cint(SelectedMonth)=i then%>
<option selected value="<%=i%>">
<%=monthname(i)%>
</option>
<%else%>
<option value=< %=i%>>
<%=monthname(i)%>
</option>
<%end if%>
<%next%>
</select>
Year:
<select name="selyear">
<%for i=2000 to year(now)%>
<option select value=<%=i%>>
<%=i%>
</option>
<%next%>
</select>
<input type="Submit" value="Submit" name="btn_submit">
</td>
</tr> <!-- <-- missing! -->
</table>
</form>
<%
dim dt
dt = "1-" & monthname(Request.Form("sel_month")) & "-" & Request.Form("sel_year")
query = "select e.emp_id,e.emp_name,e.CompDepart,e.des_name,U.login_time,U.logoff_time,U.login_date " &_
"from egv_emp_departments e " &_
"inner join login_mast U on U.emp_id=e.emp_id " &_
"where month(login_date)='" & request.form("selmonth") &_
"' AND year(login_date)='" & request.form("selyear") &_
"' AND e.emp_id='" & Session( "emp_id") & "'"
rs2.open query,con
%>
<table border="1" width="100%">
<tr> <!-- added missing tr tag -->
<td>emp_id</td>
<td>emp_name</td>
<td>CompDepart</td>
<td>des_name</td>
<%
dim intDay
dim daysInMonth
dim strMonth
dim stryear
intDay=1
strMonth = request("selmonth")
stryear = request("selyear")
If strMonth <> "" Then
If stryear <> "" Then
Select Case strMonth
Case 1,3,5,7,8, 10,12 daysInMonth = 31
Case 4, 6, 9, 11 daysInMonth = 30
Case 2
if stryear mod 4 = 0 Then
If stryear mod 100 = 0 AND stryear mod 400 <> 0 Then
daysInMonth = 28
Else
daysInMonth = 29
End if
Else
daysInMonth=28
End if
End Select
Do While intDay <= daysInMonth
Response.Write "<td>" & intDay & "</td>"
intDay = intDay + 1
Loop
Else
End if
End if
for each x in rs2.fields ' <-- fixed typo "feilds"
response.write "<th>" & x.name & "<th>"
next
%>
</tr> <!-- added missing /tr tag -->
<%do until rs2.EOF %>
<tr>
<%for each x in rs2.Fields%>
<td>
<%Response.Write(x.value)%>
</td>
<% next %>
<% rs2.MoveNext %>
</tr>
<%loop
rs2.close
con.close %>
</table>
At first, I thought you wanted what's called a "PivotTable" in Excel, but then I noticed that you're filtering to a single employee. It's still not clear to me what you mean by "single days column" vs. "like single row with multiple column", but I assume you mean something like:
Employee: Smith, John
ID: 123456
Month: March
—————————————————
Day | In | Out
1 | 8 | 5
2 | 8 | 5
3 | - | -
4 | 9 | 5
... | ... | ...
30 | 9 | 4
31 | 8 | 4
—————————————————
In other words, your problem is that there might be days with no data, but you still want to print those rows. The simplest way to handle that is probably a temp table with the numbers 1 through 31 in it, which you then use in an outer join with the rest of your query. If you can't do that (e.g. you're not allowed to create temp tables - don't laugh, it happens), then you can do it in code instead.
The easiest way to do it in code is to put the values from the database into an array, but not by using GetRows. Instead, put each day's value into the corresponding row of the array.
dim listdays(31,1)
...
Do Until rs2.EOF
dt = Day(rs2("login_date"))
listdays(dt,0) = rs2("login_time")
listdays(dt,1) = rs2("logoff_time")
rs2.Movenext
Loop
(I'll pause while the purists finish having conniptions. Trust me, any performance hit from using Movenext on a recordset that contains, at most, 31 rows, is going to be so infinitesimal as to be nonexistent.)
Then you simply write out the contents of the array.
Response.Write "<table><tr><th>Day</th><th>Login</th><th>Logoff</th></tr>"
For i = 1 to DaysInMonth
Response.Write "<tr>"
Response.Write "<th>" & i & "</th>"
Response.Write "<td>" & listdays(i,0) & "</td>"
Response.Write "<td>" & listdays(i,1) & "</td>"
Response.Write "</tr>"
Next
Response.Write "</table>"

Getting range of numbers

I was wondering if someone could help me out.
I need to work out a range that a dynamic number sits in.
For example, i have a script like so
<% upperlimit = 50000.0 %>
<% lowerlimit = -30000.0 %>
<%=Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit) %>
This spits out the value 30366.
The upperlimit and lowerlimit are dynamic so i dont know what the output would be.
This value is in the range 30001 - 40000 ... How can i get those values dynamically?
Cheers
I believe this example covers your situation:
<%
max=100
min=1
Randomize
%>
<%=Int((max-min+1)*Rnd+min)%>
Update 1
<% upperlimit = 50000.0 %>
<% lowerlimit = -30000.0 %>
<% range = 10000 %>
<% newValue =Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit) %>
<% lowerBand=(newValue\range)*range %>
<% upperBand=lowerBand+range %>
<%=newValue%>
<br>
<%=lowerBand+1%>-<%=upperBand%>
if you wanted to generate 30000-40000
<%
Dim MyNewRandomNum
Randomize
MyNewRandomNum = Int(Rnd * 10000)+30000
response.write MyNewRandomNum
%>
the value you random
30000 is minimal value
and 10000 is value you wanted to random (1-10000)
i hope this is you want
This works for both negative and positive values of newValue:
<%
dim upperlimit, lowerlimit, newValue, lowerBand, upperBand
upperlimit = 50000.0
lowerlimit = -30000.0
Randomize
newValue = Int((upperlimit - lowerlimit + 1)*Rnd + lowerlimit)
lowerBand = Fix(newValue/10000) * 10000 + Sgn(newValue)
upperBand = Sgn(lowerBand) * 10000 + lowerBand - Sgn(newValue)
response.write "newValue: " & newValue & "<br>"
response.write "lowerBand: " & lowerBand & "<br>"
response.write "upperBand: " & upperBand & "<br>"
%>
Please see Running example

Add a bottom line at the end of loop group in classic asp

Shadow Wizard help me showing this code to me:
<%
Set oRS= TarefasConexaoMSSQL.Execute("SELECT * FROM apptabela ORDER BY oito ASC")
Dim currentGroupName, previousGroupName
currentGroupName = ""
previousGroupName = ""
Do Until oRS.EOF
currentGroupName = oRS("oito")
One = oRS("um")
Two = oRS("dois")
If currentGroupName<>previousGroupName Then
Response.Write("<p>")
Response.Write("<a href='#'>" & currentGroupName & "</a>")
Response.Write("</p>")
End If
Response.Write("- One: " & One & ", Two: " & Two & "<br />")
previousGroupName = currentGroupName
oRS.MoveNext
Loop
oRS.Close
%>
it's generating something like:
1000
One: Apples, Two: Pear
One: Volks, Two: Lexus
1001
One: Car, Two: Boat
1002
One: Chicken, Two: Cow
One: Pen, Two: Pencil
One: C#, Two: C++
What I want is to add a line at the bottom of the each group, like a Sum, so later I can do some calculations:
1000
One: Apples, Two: Pear
One: Volks, Two: Lexus
SUM: X, Y
1001
One: Car, Two: Boat
SUM: X, Y
1002
One: Chicken, Two: Cow
One: Pen, Two: Pencil
One: C#, Two: C++
SUM: X, Y
Thanks
http://www.pengoworks.com/workshop/jquery/calculation/calculation.plugin.htm
you can use this jquery plugin for your requirement. If you can not understand how to implement then please let me know. I have used this recently.
<%
Set oRS= TarefasConexaoMSSQL.Execute("SELECT * FROM apptabela ORDER BY oito ASC")
Dim currentGroupName, previousGroupName, sumone, sumtwo
currentGroupName = ""
previousGroupName = ""
Do Until oRS.EOF
currentGroupName = oRS("oito")
One = oRS("um")
Two = oRS("dois")
If currentGroupName<>previousGroupName Then
(!)Response.write("- Sum: " & sumone & ", " & sumtwo)
(!)sumone = 0 / ""
(!)sumtwo = sumone
Response.Write("<p>")
Response.Write("<a href='#'>" & currentGroupName & "</a>")
Response.Write("</p>")
End If
Response.Write("- One: " & One & ", Two: " & Two & "<br />")
(!)sumone = sumone + one
(!)sumtwo = sumtwo + two
previousGroupName = currentGroupName
oRS.MoveNext
Loop
oRS.Close
%>
this shoud fix it. as you are ordering by groupname and want a sum for every group you have to sum things up when printing the group elements and print the sum when goint into a new group + clear the sum before summing up again. this should also work if you are using string, but maybe you would want to add whitespaces when summing up then

RecordSet, Checking the Last Record

I am using the google chart framework based on a data in database...
I need to generate an output like:
([
['Year', 'ON', 'OFF', 'X1', 'X2'],
['Jan/12', 2, 5, 10, 9],
['Fev/12', 5, 10, 54, 10],
['Mar/12', 10, 36, 15, 10]
]);
Note that the last line do not have a comma. My code looks like:
([
['Year', 'ON', 'OF', 'X1', 'X1'],
<%
WHILE NOT DB.EOF
ON= DB("ON")
OFF= DB("OFF")
X1= DB("X1")
X2= DB("X2")
%>
['<% Response.Write Month %>/<% Response.Write Year %>', <% Response.Write ON %>, <% Response.Write OFF %>, <% Response.Write X1 %>, <% Response.Write X2 %>,
<%
DB.MOVENEXT
WEND
%>
]);
My question: How can I check when the loop come to the last line, instead of put the comma, put just nothing?
Thanks!
AnthonyWJones's answer seems ok for me, but you could also ask inside loop if after move next you're not at EOF then you write ","
([
['Year', 'ON', 'OFF', 'X1', 'X2']
<% Do While Not DB.EOF %>
[
'<%= Month %>/<%= Year %>',
<%= DB("ON") %>,
<%= DB("OFF") %>,
<%= DB("X1") %>,
<%= DB("X2") %>
]
<%
DB.MoveNext
%>
<% If Not DB.EOF Then %>
,
<% End If %>
<% Loop %>
]);
;-)
You get RecordCount = -1 because a forward only recordset does not know how many records are yet to come.
The approach to take here is to insert some content at the top of the loop.
([
['Year', 'ON', 'OFF', 'X1', 'X2']
<%
Do While Not DB.EOF
Response.Write ", " & vbCrLf
Response.Write " ['" & Month & "/" & Year & "', " & DB("ON") & ", " & DB("OFF") & ", " & DB("X1") & ", " & DB("X2") & "]"
DB.MoveNext
Loop
%>
]);

Classic ASP sub procedure call in Response.Write

I have a classic ASP question.
Attempting to do this: the recordset is a simple list of years from 1995 to 2020; and i am trying to make 2010(current year) the default selection in the drop down.
issue: I trying to call a Sub proc in "Response.Write", but it keeps giving me this error:
"Error '800a000d' Type mismatch: 'selectyear' "
Below is the code, the Attempt 1 works with out any problem. But when i move that "if" logic to a sub procedure and call it in the Request.Write, it gives me the error.
Can any one please explain why Attempt1 works and Attempt2 wouldnt.
' Attempt 1:
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">"
dim selyr
while not rsYEAR.EOF
if CINT(rsYEAR.fields("year")) = year(now) then
selyr = "selected"
else selyr = ""
end if
Response.Write"<option value='" & rsYEAR.fields("year") & "' "& selyr &" >" & cstr(rsYEAR.Fields("year"))
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
' Attempt 2:
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">"
dim selyr2
while not rsYEAR.EOF
Response.Write "<option value='" & rsYEAR.fields("year") & "' " & cstr(selectyear(cint(rsYEAR.fields("year")))) &" >" & cstr(rsYEAR.Fields("year"))
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
'close and clean up
rsYEAR.Close
set rsYEAR = nothing
I would greatly appreciate your response.
thank you,
Shiva
I am guessing that cint(rsYEAR.fields("year")) is throwing the error because there is data that cannot be converted to int. I would expect that to happen in both cases though.
You shouldn't need the cstr in cstr(selectyear(cint(rsYEAR.fields("year")))) in the second attempt, as I assume selectyear is already returning a string. Can you show the code for selectyear?
(How has this sat for so long without a correct answer?)
A Sub does not have a value, so you can't Response.Write it. You need to either use a function, or put the Sub call on its own line.
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">"
dim y
while not rsYEAR.EOF
y = rsYEAR.fields("year")
Response.Write "<option value='" & y & "'" & IsCurr(y) & ">" & y & "</option>"
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
Function IsCurr(yr)
if Cstr(yr) = Cstr(year(now)) then
IsCurr = " selected"
else
IsCurr = ""
end if
End Function
Using a sub instead of a function, this would become
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">"
dim y
while not rsYEAR.EOF
y = rsYEAR.fields("year")
Response.Write "<option value='" & y & "'"
IsCurr y
Response.Write ">" & y & "</option>"
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
Sub IsCurr(yr)
if Cstr(yr) = Cstr(year(now)) then
Response.Write " selected"
end if
End Sub

Resources