Finding midpoint of an array with odd number of records

  I am doing a search on a database. It is pulling up all records that match the search. I want it to divide the records in two and put the first half in the left hand column, then the 2nd half in the right hand column of a table. It works fine if I have an even number of records that match, but if there is an odd number, it does the left column, then duplicates the values in the 2nd column and repeats them.

Any suggestions?

 <%
dim aRS, x,y
aRS = RS.GetRows()
RS.close
x = ubound(aRS,2) / 2
y = 0
do while y < x
 if x + y < ubound(aRS,2) then
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x+y) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x+y) & "</font></td></tr>"
  else
     response.write "<tr bgcolor=""white""><td>" & aRS(1,y) & "</td><td>&nbsp;</td></tr>"
  end if

   y = y + 1
loop
%>
katleesAsked:
Who is Participating?
 
danataylorConnect With a Mentor Commented:
I usually do stuff like this using nested tables where one crates a table with two columns and then defines an inner table for each of the columns.  I recoded your stuff this way.  I hope it's right.

if ubound(aRS,2) / 2 = int(ubound(aRS,2) / 2) then
  x = int(ubound(aRS,2) / 2) -1
else
  x = int(ubound(aRS,2) / 2)
end if

Response.Write "<table>" & vbcrlf                           'outer table start
response.write "<tr>" & vbcrlf
response.write "<td>" & vbcrlf                                 ' outer table left column

Response.Write "<table>" & vbcrlf                           ' left inner table start
for y=lbound(aRS,2) to x
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td></tr>" & vbcrlf
next
Response.Write "</table>" & vbcrlf                            ' left inner table end
response.write "</td>" & vbcrlf

if ubound(aRS,2) / 2 = int(ubound(aRS,2) / 2) then       ' if odd number skip one to leave the middle
      y=y+1                                                       ' record for the end of the left column
end if

response.write "<td>" & vbcrlf                                     ' outer table right column
Response.Write "<table>" & vbcrlf                               ' start of right inner table
for y=y to ubound(aRS,2)
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td></tr>" & vbcrlf
next
Response.Write "</table>" & vbcrlf                              'end of right inner table

response.write "</td>" & vbcrlf
response.write "</tr>" & vbcrlf
if ubound(aRS,2) / 2 = int(ubound(aRS,2) / 2) then              ' check to see if odd and then output the middle record
    response.write "<tr bgcolor=""white""><td>" & aRS(1,int(ubound(aRS,2) / 2)) & "</td><td>&nbsp;</td></tr>" & vbcrlf
end if
Response.Write "</table>" & vbcrlf                                    'end of outer table
0
 
danataylorCommented:
See if this works instead of x = ubound(aRS,2) / 2

if ubound(aRS,2) / 2 = cint(ubound(aRS,2) / 2) then
    x = ubound(aRS,2) / 2
else
    x = cint(ubound(aRS,2) / 2) + 1
end if
0
 
danataylorCommented:
You will also need to do a bounds check for x+y in the aRS(1,x+y) part because it will result in an "out of bounds" error when there are an odd number of elements.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
katleesAuthor Commented:
How do you do that?
0
 
danataylorCommented:
Actually, I believe this should take care of it.

if ubound(aRS,2) / 2 = cint(ubound(aRS,2) / 2) then
    x = ubound(aRS,2) / 2
else
    x = cint(ubound(aRS,2) / 2) + 2
end if

y = 0
for y=lbound(aRS,2) to cint(ubound(aRS,2) / 2)
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x+y) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x+y) & "</font></td></tr>"
next

if x+y < cint(ubound(aRS,2)) then
    response.write "<tr bgcolor=""white""><td>" & aRS(1,y) & "</td><td>&nbsp;</td></tr>"
end if


0
 
katleesAuthor Commented:
I get this..
Microsoft VBScript runtime error '800a0009'
Subscript out of range: '[number: 80]'

/meds/allianzunmedgood.asp, line 109


109 is the response.write line

Here is the code how I have it on the page - was the loop part supposed to be taken out?

    <%
dim aRS, x,y
aRS = RS.GetRows()
RS.close
if ubound(aRS,2) / 2 = cint(ubound(aRS,2) / 2) then
    x = ubound(aRS,2) / 2
else
    x = cint(ubound(aRS,2) / 2) + 2
end if

y = 0
for y=lbound(aRS,2) to cint(ubound(aRS,2) / 2)
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x+y) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x+y) & "</font></td></tr>"
next

if x+y < cint(ubound(aRS,2)) then
    response.write "<tr bgcolor=""white""><td>" & aRS(1,y) & "</td><td>&nbsp;</td></tr>"
end if

 
%>
0
 
danataylorCommented:
Yeah, I know.  Let me work on it a little bit.

After digging into your code a little deeper I have some questions

1. How many columns (fields) are returned in teh recordset?
2. It looks like the array is two-dimensional with 2 columns (0 & 1) and an unknown number of rows.
2. You are only referencing data in column 1. (aRS(1,y), aRS(1,x+y), etc)
3. Using x and y to reference the same array element (aRS(1,y), aRS(1,x+y), etc) is a little confusing.  x is typically used to reference the column while y references the row - just like cartesian coordinates.  I would suggest using something like j & k or j & o.  o would indicate an offset like the value that x represents in your code.

I'll get back to you in a minute.
0
 
katleesAuthor Commented:
The number of rows varies upon the search. Sometimes it is 5, sometimes it is 40
0
 
CyrexCore2kCommented:
Why not just do it this way?

<table><tr><td valign=top><table>
<%
dim aRS, x, half_rows, total
aRS = RS.GetRows()
RS.close
half_rows = (ubound(aRS,2) + 1) / 2 + (ubound(aRS, 2) mod 2)
for x = 0 to (half_rows - 1)
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
Response.Write "</table></td><td valign=top><table>"
total = ubound(aRS, 2) + 1 '+1 for count as opposed to the last index
for x = half_rows to total - 1
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
%>
</table></td></tr></table>

That should work just fine? If there's an odd number of elements the extra will show on the left side.
0
 
CyrexCore2kCommented:
Dana and I think alike it seems.
0
 
danataylorCommented:
I didn't think of using "mod 2" to get the remainder and adding it to the 1st half of the array count.  Less code is always better.
0
 
katleesAuthor Commented:
CyrexCore

Yours gives me 4 columns, 1st column twice, then 2nd column twice
0
 
CyrexCore2kCommented:
Oh oh my mistake I didn't edit your code enough.

<table><tr><td valign=top><table>
<%
dim aRS, x, half_rows, total
aRS = RS.GetRows()
RS.close
half_rows = (ubound(aRS,2) + 1) / 2 + (ubound(aRS, 2) mod 2)
for x = 0 to (half_rows - 1)
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
Response.Write "</table></td><td valign=top><table>"
total = ubound(aRS, 2) + 1 '+1 for count as opposed to the last index
for x = half_rows to total - 1
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
%>
</table></td></tr></table>
0
 
katleesAuthor Commented:
CyrexCore,

Your code did two columns, but the first column has two more records than the 2nd on oens that have an even count.

Dana's works great so I will just use that.

Thanks guys
0
 
CyrexCore2kCommented:
If you're still interested this should work.

<table><tr><td valign=top><table>
<%
dim aRS, x, half_rows, total
aRS = RS.GetRows()
RS.close
half_rows = (ubound(aRS,2) + 1) / 2 + ((ubound(aRS, 2) + 1) mod 2)
for x = 0 to (half_rows - 1)
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
Response.Write "</table></td><td valign=top><table>"
total = ubound(aRS, 2) + 1 '+1 for count as opposed to the last index
for x = half_rows to total - 1
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
%>
</table></td></tr></table>
0
 
katleesAuthor Commented:
CyrexCore,

Thank you. Problem with two tables - one for each column, is if one colum goes to two lines, the rows don't match up and it looks funny.
0
All Courses

From novice to tech pro — start learning today.