Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding midpoint of an array with odd number of records

Posted on 2006-07-17
16
Medium Priority
?
309 Views
Last Modified: 2008-03-04
  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
%>
0
Comment
Question by:katlees
  • 6
  • 6
  • 4
16 Comments
 
LVL 9

Expert Comment

by:danataylor
ID: 17123764
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
 
LVL 9

Expert Comment

by:danataylor
ID: 17123783
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
 

Author Comment

by:katlees
ID: 17123872
How do you do that?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:danataylor
ID: 17124061
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
 

Author Comment

by:katlees
ID: 17124343
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
 
LVL 9

Expert Comment

by:danataylor
ID: 17124414
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
 

Author Comment

by:katlees
ID: 17124555
The number of rows varies upon the search. Sometimes it is 5, sometimes it is 40
0
 
LVL 9

Accepted Solution

by:
danataylor earned 2000 total points
ID: 17125299
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
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125335
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
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125340
Dana and I think alike it seems.
0
 
LVL 9

Expert Comment

by:danataylor
ID: 17125409
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
 

Author Comment

by:katlees
ID: 17125478
CyrexCore

Yours gives me 4 columns, 1st column twice, then 2nd column twice
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125685
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
 

Author Comment

by:katlees
ID: 17125837
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
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125855
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
 

Author Comment

by:katlees
ID: 17126346
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question