iPixel Posted August 8, 2011 Share Posted August 8, 2011 To keep it short, i pull info from database. DISTINCT ItemName. i need to give the results in 4 columns. $total_results = $items->num_rows; // contains total # of results $columns = 4; // max columns $items_per_column = ceil($total_results / $columns); //estimated results per column. let's say the letters of the alphabet are these "ItemNames" I dont want to display [A][C][D][E][F][G] but rather [A][C][E][G] [D][F] I cant for the life of me find any examples or the math logic to figure this out. Thanks! Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 use a counter and check like this: if($count % 4 == 0){ // change row } Quote Link to comment Share on other sites More sharing options...
iPixel Posted August 8, 2011 Author Share Posted August 8, 2011 Here's what i was working with. Could you explain the % 4 to me, and i assume $count starts at 0 ? <table cellpadding="3" cellspacing="0" border="0" width="100%"> <?php for($i=0;$i<=$items_per_column;$i++) { echo "<tr>"; for($r=1;$r<=$columns;$r++) { echo "<td>"; print_r($items->result[$i][itemName]); echo "</td>"; } echo "</tr>"; } ?> </table> Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 % is the modulus operator... ($count % 4) devides $count by 4 and returns the remainder. if the remainder is equal to 0, that means your number (in $count) is a multiple of 4, so it's time to change to a new row. Quote Link to comment Share on other sites More sharing options...
iPixel Posted August 8, 2011 Author Share Posted August 8, 2011 Aha i see. Ok but my bigger problem is figuring out which result to show. so if you have 96 results, 4 columns that's 24 results per column so the first row of the results would look like [0][24][48][72] Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 Aha i see. Ok but my bigger problem is figuring out which result to show. so if you have 96 results, 4 columns that's 24 results per column so the first row of the results would look like [0][24][48][72] I honestly have no idea what you mean by that. What is the logic (in plain english) you wish to use for deciding what goes in which column ? Quote Link to comment Share on other sites More sharing options...
iPixel Posted August 8, 2011 Author Share Posted August 8, 2011 This really is the best way i can explain it. [#] = result shown within a table cell. So as opposed to showing the results like this [zero][1][2][4] [4][5][6][7] [8][9] i want to display them like so... [zero][3][6][9] [1][4][7] [2][5][8] Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 in that case, you need to separate the values first (because html tables are built in rows)... so you could do something like this: $cols = array(); $count = 1; foreach ($original_array as $value){ $cols[$count][] = $value; $count++ if($count == 5) $count = 1; } this will build $cols[1], $cols[2], $cols[3] and $cols[4] with the values you want for each column Quote Link to comment Share on other sites More sharing options...
iPixel Posted August 8, 2011 Author Share Posted August 8, 2011 Oooh i managed to figure it out before i saw this post... but thank you for the help. <?php $total_results = $items->num_rows; $columns = 4; $rows = ceil($total_results / $columns); ?> <table cellpadding="3" cellspacing="0" border="0" width="100%"> <?php for ($r=0; $r < $rows; $r++) { echo "<tr>"; for ($c=0; $c < $columns; $c++) { if ($r + $c * $rows < $total_results) { echo "<td>"; print_r($items->result[$r + $c * $rows][itemName]); echo "</td>"; } else { echo "<td> </td>"; } } echo "</tr>"; } ?> </table> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.