Need help with algorithm

1. Senior Member
Join Date
Dec 1969
Posts
2,092

Need help with algorithm

MY SITUATION<BR>--------------------------------------------------------------------------------------------------------<BR>I’ve created an SQL Query that retrieves data from my SQL Server database.<BR>My SQL Query has an order by clause so that my recordset is ordered alphabetically.<BR><BR>I then create a While objrst.EOF…Wend loop to create a &#060;table&#062;…&#060;/table&#062;<BR>Inside each &#060;tr&#062;…&#060;/tr&#062; of my &#060;table&#062; I create two &#060;td&#062;…&#060;/td&#062;<BR><BR>The end result is basically something like this:<BR><BR>&#060;table&#062;<BR>Data1 Data2<BR>Data3 Data4<BR>Data5 Data6<BR>&#060;/table&#062;<BR><BR><BR><BR>MY QUEST<BR>--------------------------------------------------------------------------------------------------------<BR>My problem is that the customer wants the data to be presented in a vertical way. Meaning something like that:<BR><BR>&#060;table&#062;<BR>Data1 Data4<BR>Data2 Data5<BR>Data3 Data6<BR>&#060;/table&#062;<BR>Notice the difference between the two!<BR><BR>Here is another example:<BR><BR>If I do a regular loop with my recordset creating a simple &#060;table&#062; with two cells. I would end up having something like this:<BR><BR>Item A Item B<BR>Item C Item D<BR>Item E Item F<BR><BR>But the customer would like to be see it this way:<BR><BR>Item A Item D<BR>Item B Item E<BR>Item C Item F<BR><BR>Notice they are displayed in a vertical fashion!<BR><BR><BR><BR>MY PROBLEM<BR>--------------------------------------------------------------------------------------------------------<BR>Can you help me figure out an algorithm that could make this work? <BR>I’ll need the to get the .RecordCount of my recordset and divide it by the number of columns I wish to have.<BR><BR>For instance: If I want 5 columns, and my RecordCount gives me a total of 38 records<BR>I need to do: 38 / 5 = 7.6<BR><BR>This means that my first column will hold 8 items <BR>The last four columns *should* hold 7 items<BR><BR><BR>But:<BR>7 items X 4 columns = 28 items <BR>8 items X 1 column = 8 items<BR><BR>This gives me a total of 36 items but my .RecordCount is 38 so I’m missing two<BR><BR>And what if I wanted 3 columns instead of 5 columns?<BR><BR>As you can see I’m in desperate need for your help to figure out an way!<BR><BR>Sincerely<BR>Vlince<BR>

2. tTt
Member
Join Date
Dec 1969
Posts
55

Do you need to use a single table?

If not, you could do a little trick like:<BR>&#060;table&#062;<BR> &#060;tr&#062;<BR> &#060;td&#062;<BR> &#060;table border=1&#062;<BR> &#060;tr&#062;&#060;td&#062;test1&#060;/td&#062;&#060;/tr&#062;<BR> &#060;tr&#062;&#060;td&#062;test2&#060;/td&#062;&#060;/tr&#062;<BR> &#060;tr&#062;&#060;td&#062;test3&#060;/tr&#062;&#060;/tr&#062;<BR> &#060;/table&#062;<BR> &#060;/td&#062;<BR> &#060;td&#062;<BR> &#060;table border=1&#062;<BR> &#060;tr&#062;&#060;td&#062;test4&#060;/td&#062;&#060;/tr&#062;<BR> &#060;tr&#062;&#060;td&#062;test5&#060;/td&#062;&#060;/tr&#062;<BR> &#060;tr&#062;&#060;td&#062;test6&#060;/tr&#062;&#060;/tr&#062;<BR> &#060;/table&#062;<BR> &#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>That&#039;ll get you to display the information vertically. As for the number of records to display... let&#039;s see, where did my old code go...<BR>

3. tTt
Member
Join Date
Dec 1969
Posts
55

As for the algorithm...

...looks like you&#039;ll need to perform the algorithm each time you want to write another row (vertically, that is). So, the first time you use getRows and it returns 38. If you want 5 rows, then 38/5 = 7.6. We need to round up if there is any remainder, so 7.6 becomes 8. Display 8 items, then make a new row. This time subtract the items you displayed, which leaves 30. Divide by 4 (the number of rows left to display). This gives 30/4 = 7.5, rounded = 8. Doing this all the way should make something like this:<BR>Records:38, Rows:5, Display: 7.6 = 8<BR>Records:30, Rows:4, Display: 7.5 = 8<BR>Records:22, Rows:3, Display: 7.3 = 8<BR>Records:14, Rows:2, Display: 7<BR>Records: 7, Rows:1, Display: 7<BR><BR>Hope this helps you build your function.<BR><BR>T

4. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

I would LOVE to see how to solve this

ok lets see what i can come up with<BR><BR><BR>NoColumns = SomeValue <BR>YourArray = Recordset.GetRows()<BR>Counter = ubound(YourArray, 2)<BR>NoOfRows = int(Counter/NoColumns)<BR><BR><BR>now lets "do it" :)<BR>Response.Write "&#060;table&#062;"<BR>for i = 0 to NoOfRows<BR>Response.Write "&#060;tr&#062;"<BR>for j = 0 to NoColumns-1 <BR><BR>This line i guess will be the KEY<BR><BR>you disply the first record then in the next cell you display the record AFTER the total rows to be displayed in each column and so on till the number of columns (the j loop)<BR>Response.Write "&#060;td&#062;" & SomArr(0,(NoOfRows*j)+i) & "&#060;/td&#062;"<BR><BR>throw the rest of the HTML<BR>next <BR>Response.Write "&#060;/tr&#062;"<BR>next<BR><BR><BR>but now the issue i cant figure out is HOW will you display the remaining rows....well easy....just get the diff between Counter-(NoOfRows*NoColumns) now you can display the last few rows BUT how in the world will you decide when to display these <BR><BR>if i again loop for the remaining records then you will be display the last few records in the recordset BUT not in the order you want<BR><BR>jeez tough one for me at least<BR><BR><BR>will have to think some more.....but have to go now....hopefully this will be a nice start. If you figure the rows that this code will leave out to order it well, let me know<BR><BR>

5. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

dont get it

how do you know you only want 3 records in a column??<BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

Oops

some variable mistakes SomArr shold be YourArray <BR><BR><BR>watch out for those<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
2,092

Actually

It is a great start and thank you very much Akhilesh<BR><BR>Although I did forget to mention, and I&#039;m sorry for all the readers, that I can have a nested &#060;table&#039;s&#062;...&#060;/table&#039;s&#062; inside my first &#060;table&#062;...&#060;/table&#062;.<BR><BR>In other words:<BR><BR>I have this MAIN &#060;table&#062;...&#060;/table&#062;<BR>Now if I want 3 columns then I&#039;ll create 3 nested &#060;table&#039;s&#062;<BR><BR>If I want 5 columns or 2 columns then I&#039;ll want to create the appropriate amount of nested &#060;table&#039;s&#062;<BR><BR>This makes the process more possible thanks to the nested &#060;table&#039;s&#062;.<BR><BR>I&#039;ll look for something else thanks again for starting it :-)<BR><BR>Sincerely<BR>Vlince

8. tTt
Member
Join Date
Dec 1969
Posts
55

RE: dont get it

I didn&#039;t put any logic in there that would determine the number of records to display at all. It was simply a demonstration of how to put two rows side-by-side, displayed in the manner wanted, but coded in a top-down way so that a simple loop and some logic could easily produce the desired effect.<BR><BR>T

9. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

God that was easy

ONE simple IF condition<BR><BR><BR>let me write the whole thing again<BR><BR><BR>NoColumns = SomeValue <BR>YourArray = Recordset.GetRows()<BR>Counter = ubound(YourArray, 2)<BR>NoOfRows = int(Counter/NoColumns)<BR>if NoOfRows*NoColumns &#060;&#062; Counter then<BR>&#039;there are some "reamaining rows" so increase the row count<BR>NoOfRows = NoOfRows+1<BR>end if<BR>Response.Write "&#060;table&#062;"<BR>for i = 0 to NoOfRows<BR>Response.Write "&#060;tr&#062;"<BR>for j = 0 to NoColumns-1<BR>if (NoOfRows*j)+i &#060;= Counter then<BR>Response.Write "&#060;td&#062;" & YourArray (0,(NoOfRows*j)+i) & "&#060;/td&#062;"<BR>end if<BR>next <BR>Response.Write "&#060;/tr&#062;"<BR>next<BR><BR>see what i did<BR><BR>increase the rowcount if you will have some "extra" rows BUT allow the display ONLY if the ctr for the array element is less than the total records<BR><BR><BR>HMPH...cant believe i did not get it the first time<BR><BR><BR>

10. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

See my post below

you dont need nested tables<BR><BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•