How to number of sub items in grouped list - asp-classic

I am using classical asp. I have to tables. The first table that is called "sorutip" have tiles of every group. The second table that is called "Questions41" have questions. I want to number each group starting from 1 as in the example below.
Complete the gaps.
1- ...........
2- .......
3- ...........
4- ...........
Answer the questions
1- ......?
2- .......?
3- .....?
4- .....?
5- ....?
6- ......?
Put the sentences into the correct order
1- .....
2- ...........
3- ....
4- ......
My code is here ...
<%
Set oRS= conn.Execute("SELECT DISTINCT(questions41.tid),sorutip.tid,sorutip.title, questions41.question FROM questions41 LEFT JOIN sorutip ON questions41.tid=sorutip.tid GROUP BY questions41.tid,sorutip.tid,sorutip.title,questions41.question")
Dim title2, previousGroupName
title2 = ""
previousGroupName = ""
Do Until oRS.EOF
title2 = oRS("title")
question= oRS("question")
If title2<>previousGroupName Then
Response.Write("")
Response.Write(oRS("title"))
Response.Write("")
End If
number=number+1
Response.Write(" "& number &"- " & question & "")
previousGroupName = title2
oRS.MoveNext<br>
Loop
oRS.Close
%>

You just need to reset the number when starting a new group
If title2<>previousGroupName Then
Response.Write("<p>")
Response.Write(oRS("title"))
Response.Write("</p>")
number = 0
End If

Related

Using loop with IIf in Access query

I am trying to calculate a field in an Access 2010 query that takes the number in volumes and either uses the default URL (for values of 1) or loops as many times as there are volumes to create a series of URLs (it's OK if they're all together, it will be text output in a report in the end).
If 1 volume, http://blah.com/1. If 2 volumes, http://blah.com/2/vol1, http://blah.com/2/vol2 .
I was thinking of something like this:
=IIf([volumes]="1",[URL],for i=1 to [volumes] output [URL] & "/vol[i], " next)
but I can't figure out if I need a variable for volumes or how to assign it or how to generated the output. Any advice on how to go about this would be greatly appreciated.
URL Item volumes
http://blah.com/1 Book1 1
http://blah.com/2 Book2 2
http://blah.com/3 Book3 10
Thanks,
Sandy
A simple loop will do:
Public Function PrintUrlVol(ByVal Url As String, ByVal Volume As Integer) As String
Dim UrlList As String
Dim Index As Integer
If Volume = 1 Then
UrlList = Url
ElseIf Volume > 1 Then
For Index = 1 To Volume
If UrlList <> "" Then
UrlList = UrlList & ", "
End If
UrlList = UrlList & Url & "/vol" & CStr(Index)
Next
End If
PrintUrlVol = UrlList
End Function

Splitting a dataframe into parts by detection, then writing to multiple csv's?

I have a csv as shown in the image below. The data is a set of separate tables, separated by a blank line, that I require to be in separate csv files.
After importing to R, I'd like to split the data into the various separate tables, and then write these tables to separate csv files. I had the idea of using some kind of string detect, as a 'new' table is signified by the first instance of 'Area' in the first column. Any ideas of how to approach the code for this in R? There are a bunch of tables and doing this manually isn't advisable.
There's a truncation problem too it seems, as the tables will be required to have a differing amounts of columns, however I don't expect that getting rid of NULL or NA data should be too difficult with this.
Thanks for any help.
I don't think R is the right tool for this kind of thing. You should always try to use the right tool based on the task. Since you have Excel installed run this VBA script. That will do what you want.
Sub page_endings()
Dim i As Long 'how many times for pagebreak
Dim searchvalue_for_break_after 'value to do pagebreak
searchvalue_for_break_after = ""
'column A must be filled in with value break after
'example row 6, 12, 18, 24 whatever row you want
'will loop until empty row in column A
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row + 1
If Range("A" & i).Value = searchvalue_for_break_after Then
'will add a pagebreak after the row with value break after
ActiveWindow.SelectedSheets.HPageBreaks.Add before:=Range("A" & i).Offset(1)
End If
Next i
Call Create_Separate_Sheet_For_Each_HPageBreak
End Sub
Sub Create_Separate_Sheet_For_Each_HPageBreak()
Dim HPB As HPageBreak
Dim RW As Long
Dim PageNum As Long
Dim Asheet As Worksheet
Dim Nsheet As Worksheet
Dim Acell As Range
'Sheet with the data, you can also use Sheets("Sheet1")
Set Asheet = ActiveSheet
If Asheet.HPageBreaks.Count = 0 Then
MsgBox "There are no HPageBreaks"
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'When the macro is ready we return to this cell on the ActiveSheet
Set Acell = Range("A1")
'Because of this bug we select a cell below your data
'http://support.microsoft.com/default.aspx?scid=kb;en-us;210663
Application.Goto Asheet.Range("A" & Rows.Count), True
RW = 1
PageNum = 1
For Each HPB In Asheet.HPageBreaks
'Add a sheet for the page
With Asheet.Parent
Set Nsheet = Worksheets.Add(after:=.Sheets(.Sheets.Count))
End With
'Give the sheet a name
On Error Resume Next
Nsheet.Name = "Page " & PageNum
If Err.Number > 0 Then
MsgBox "Change the name of : " & Nsheet.Name & " manually"
Err.Clear
End If
On Error GoTo 0
'Copy the cells from the page into the new sheet
With Asheet
.Range(.Cells(RW, "A"), .Cells(HPB.Location.Row - 1, "K")).Copy _
Nsheet.Cells(1)
End With
' If you want to make values of your formulas use this line also
' Nsheet.UsedRange.Value = Nsheet.UsedRange.Value
RW = HPB.Location.Row
PageNum = PageNum + 1
Next HPB
Asheet.DisplayPageBreaks = False
Application.Goto Acell, True
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Call SaveWorksheetsAsCsv
End Sub
Sub SaveWorksheetsAsCsv()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "C:\Users\Excel\Desktop\"
For Each WS In ThisWorkbook.Worksheets
Sheets(WS.Name).Copy
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
ThisWorkbook.Activate
Next
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
' about overwriting the original file.
End Sub
You should take each different table to the uppermost part. All in all, you have 5 tables with different dimensions (Table1: 11x13; Table2: 11x9; Table3: 3x12; Table4: 10x5; Table5: 6x7). Take them side-by-side in the above (A1:M11; N1:V11 etc.). The headings of tables would be in 1st row.
library(readxl)
# Use the path returned from getwd() function that is R's working directory
df <- as.data.frame(read_excel("C://Users//User//Documents//Revolution//Your.xlsx"))
Then, you can handle these 5 tables as:
Table1 <- df[,1:13]
Table2 <- df[,14:22]
Table3 <- df[1:3,23:34]
Table4 <- df[1:10,35:39]
Table5 <- df[1:6,40:46]
By caring dimensions stemmed from different row numbers in the assignmets, you do not face any NA or NULL value in Table1...Table5.

loop through two different html tables

I am searching for the answer for the whole day.
Basically, I have two html tables and a recordset in getrows()
The page is designed so that there are two html tables and I need to generate it, no other choice to re-design the page :(
Here are the html tables. They are horizontally aligned.
table1 table2
row row
row row
row row
and so on...
How can I generate these tables and of course I do not know how many records are in the recordset.
lets assume that there are 3 fields, name, quantity and date. so, there will be two html tables and i get six columns, three of each table. i will not distribute different fields to different tables. so tables are just the same, side by side, with different data on them, not the fields
You can create a "container table" that will have the two side-by-side tables within so that they lay out as you desire. You didn't specify, so I have table 1 with the first half of the data, and table 2 with the second half. (It's a bit easier/more straightforward, but you can do it with only a little extra effort to have row 1 in table 1, row 2 in table 2, row 3 in table 1, row 4 in table 2, etc.)
So, starting from the array of your records obtained via GetRows (arrData in my example below), and assuming your array has them in the order (from left to right) you want them displayed:
Response.Write "<table><tr><td>"
dim intLastRow
intLastRow = ubound(arrData, 2)
dim intBreakPoint
intBreakPoint = fix(intLastRow/2)
dim intSecondStartPoint = intBreakPoint + 1
Response.Write "<table>"
dim i
for i = 0 to intBreakPoint
Response.Write "<tr><td>" & arrData(0, i) & "</td><td>" & arrData(1, i) & "</td><td>" & arrData(2, i) & "</td></tr>"
next
Response.Write "</table>"
if intLastRow > 0 then
Response.Write "<table>"
for i = intSecondStartPoint to intLastRow
Response.Write "<tr><td>" & arrData(0, i) & "</td><td>" & arrData(1, i) & "</td><td>" & arrData(2, i) & "</td></tr>"
next
Response.Write "</table>"
end if
Response.Write "</td></tr></table>"

Further problems with counting occurrences of strings in an Array

I am copying a question and answer from elsewhere as it partly goes into what I need but not completely.
In ASP classic, is there a way to count the number of times a string appears in an array of strings and output them based on string and occurrence count?
For example if I have an array which contains the following :
hello
happy
hello
hello
testing
hello
test
happy
The output would be:
hello 4
happy 2
test 1
testing 1
The answer that was given was this:
I'm assuming the language is VBScript (since that's what most people use with classic ASP).
You can use a Dictionary object to keep track of the individual counts:
Function CountValues(pArray)
Dim i, item
Dim dictCounts
Set dictCounts = Server.CreateObject("Scripting.Dictionary")
For i = LBound(pArray) To UBound(pArray)
item = pArray(i)
If Not dictCounts.Exists(item) Then
dictCounts.Add item, 0
End If
dictCounts.Item(item) = dictCounts.Item(item) + 1
Next
Set CountValues = dictCounts
End Function
This is great but I can't work out how to grab the top 2 most used words, display them and be able to put them in their own variable for use elsewhere.
Can anyone help with this?
You can loop through the dictionary object using this method. Inside that loop keep track of the top two keys and their counts in either a new array or two new variables.
You can't sort a Dictionary object in VBScript, so you have to use something else.
My advice is using a disconnected Recordset object to hold the items and their occurrences. Such object natively support sorting and it's pretty easy to use. To achieve this have such function instead:
Function CountValues_Recordset(pArray)
Dim i, item
Dim oRS
Const adVarChar = 200
Const adInteger = 3
Set oRS = CreateObject("ADODB.Recordset")
oRS.Fields.Append "Item", adVarChar, 255
oRS.Fields.Append "Occurrences", adInteger, 255
oRS.Open
For i = LBound(pArray) To UBound(pArray)
item = pArray(i)
oRS.Filter = "Item='" & Replace(item, "'", "''") & "'"
If (oRS.EOF) Then
oRS.AddNew
oRS.Fields("Item").Value = item
oRS.Fields("Occurrences").Value = 1
Else
oRS.Fields("Occurrences").Value = oRS.Fields("Occurrences").Value + 1
End If
oRS.Update
oRS.Filter = ""
Next
oRS.Sort = "Occurrences DESC"
oRS.MoveFirst
Set CountValues_Recordset = oRS
End Function
And using it to achieve the output you want:
Dim myArray, oRS
myArray = Array("happy", "hello", "hello", "testing", "hello", "test", "hello", "happy")
Set oRS = CountValues_Recordset(myArray)
Do Until oRS.EOF
Response.Write(oRS("item") & " " & oRS("Occurrences") & "<br />")
oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing
Don't forget to close and dispose the recordset after using it.

How to add a div after every 5 rows of data?

I'm trying to add div after a group of 5 rows of data inside Listview.
I came up with using Mod 5 = 0 but I realized the last row is missing if total row count can't divided by 5.
For example, I have 11 rows of data.
"Div" will be added after 5 and 10.
I also need to add "Div" after 11 as well. (div will display details of each group)
So, something like this
1|2|3|4|5
DIV1
6|7|8|9|10
DIV2
11|
Div3(missing)
Here is inline code I have so far
<%# IIf((Container.DisplayIndex + 6) Mod 5 = 0, "<div id='temp" + Math.Floor((Container.DisplayIndex + 6) / 5).ToString + "' style='display:none'></div>", "")%>
How do I add the last div when total # can't be divided by 5?
I have to get the total# of display index somehow...
You can compare it with the ListView.Items.Count property.
This should work:
Dim addDiv = (Container.DisplayIndex Mod 5 = 0) OrElse _
(Container.DisplayIndex + 1 = ListView1.Items.Count)
If you are using paging you should use DataItemIndex instead of DisplayIndex.
Edit: (according to your last comment)
If you need to show the total record count you have to cast the ListView's DataSource to it's correct type(f.e. a DataTable).
Dim tbl as DataTable = DirectCast(ListView1.DataSource, DataTable)
Dim totalCount as Int32 = tbl.Rows.Count
or in one line:
DirectCast(ListView1.DataSource, DataTable).Rows.Count
But this works only on databinding and not on every postback because the DataSource will be disposed at the end of the Page-Lifecycle.
the "+6" makes it look like you're guessing around how the modulo function works :)
try this:
<%# IIf(Container.DisplayIndex Mod 5 = 4 Or Container.DisplayIndex = ListView.Items.Count , "<div id='temp" + (1+Math.Floor(Container.DisplayIndex/5)).ToString + "' style='display:none'></div>", "")%>
so first (index+6)%5 == 0 is the same as index%5 == 4,
second floor((index+6)/5) results in 2 when index=4, this is not what you want.
using 1+floor(index/5) will give you the correct results --- you could even drop the "1+" if you want the index to start from 0.
(p.s. i don't know asp.net, sorry if there's compiler errors)

Resources