equal amount of data in 3 colums!!!?

Discussion in 'PHP' started by jmansa, Nov 21, 2005.

  1. #1
    I'm trying to figure this out. I have a database table with several of names. Now I want these names listed alfabetic order with an *.gif header for each letter. Then I want the names output in 3 colums with equal amount of names in each colum... Can this be done? and if yes... HOW!!!

    jmansa
     
    jmansa, Nov 21, 2005 IP
  2. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    of course it can be done - though i'm not a php person, i do asp. but the logic is the same - perhaps you could do the syntax.

    in your sql statement, order the results by name asc... that'll give you your alpha.

    to make the 3 equal columns, you'll need to grab the recordset count, and divide by 3. that'll give you the amount of records to display in each column.

    you'll then need to draw your html tables and cells, and insert a loop into each of your three cells that iterates through your recordset at varying increments, which you will define dynamically based on the value you get by determining 1/3rd of your recordset count. In your first column, iterate starting at 1 and going up to your "third" value, then in the second column, iterate starting at "third" value +1 through your "third" value * 2, and in your third column, iterate starting at ("third" value * 2) + 1 and ending with the end of your recordset. you'll end up your remainders in the third column that way, though you can do a more sophisticated method than this to even it out more if you need to.

    Personally, i would loop through the recordset once, and insert a closing & opening cell tag at each interval determined above by using if... then statements, thereby alleviating the need to cycle through it three times. Either way, it should yield the same result - thats just personal preference.

    VG
     
    vectorgraphx, Nov 21, 2005 IP
  3. jmansa

    jmansa Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can you give an example in asp? That would be great... I'm not a hardcoder!
     
    jmansa, Nov 21, 2005 IP
  4. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    sure, here ya go: (sorry its a bit sloppy, but it works)

    
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Vectorgraphx is really cool! :)</title>
    </head>
    
    <body>
    <% 'define db connection
    Set DSNConn = Server.CreateObject("ADODB.Connection") 
    dsnconn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\inetpub\wwwroot\keypersonnel.mdb"
     usersql = "SELECT * FROM RESULTS order by personname asc"
      '   Create ADO Recordset Component  '             
              Set oRs = Server.CreateObject("ADODB.Recordset")
              oRs.Open usersql, dsnconn,3,3
    'get totalcount
    oRs.movelast
    totalcount = oRs.recordcount
    'move back to the beginning of the recordset
    oRs.movefirst
    'get thirdvalue based on totalcount
    thirdvalue = totalcount / 3
    'now draw the beginnings of your table
    %>
    <table width="100%"  border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td><div align='center'>
          <%
    	'define a variable with which to count through the loop iteration
    loopcounter = 1
    'iterate through the loop	
    'loop
    	while not ors.eof
    	response.write "<img src='"&ors("Personname")&".gif' >"
    'draw the closing/opening cell tags based on the thirdvalue variable
    	if loopcounter = thirdvalue or loopcounter = thirdvalue * 2 then
    	response.write"</div></td><td><div align='center'>"
    	else
    	response.write"<br>"
    	end if
    'step up the loop
    	loopcounter = loopcounter + 1
    'move to the next record
    	ors.movenext
    	wend
    'now draw the rest of the table - done.	%></div>
        </td>
      </tr>
    </table>		  
    </body>
    </html>
    
    Code (markup):
     
    vectorgraphx, Nov 21, 2005 IP
  5. hdpinn

    hdpinn Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    EDIT TO SUITE YOUR NEEDS:

    PUT BEFORE YOUR LOOP:
    $total_rows_returned = mysql_num_rows($query);
    $count = 1;
    PHP:
    THEN PUT THIS IN YOUR LOOP (USUALLY NEAR THE BOTTOM):

    if (($count/ceil($total_rows_returned/3) == ceil($count/ceil($total_rows_returned/3))) && ($count<$total_rows_returned)) echo '</td><td valign="top">';
    $count++;
    PHP:
     
    hdpinn, Nov 26, 2005 IP