Alcain Posted September 19, 2017 Share Posted September 19, 2017 THIS IS FOR HOMEWORK, so please do not give me the direct answer but maybe the logic behind it. I cannot seem to figure this out. The problem: I need to generate a dynamic table by column and not by row. The user enters a score for an exam (ie 100) on an HTML page. That value is passed and I need to create 100 different entries in a table. There is also a box that allows the user to select how many columns they want the 100 entries displayed in. So instead of having 100 rows going all the way down the page, a table with 5 columns (if they choose 5) will show while still having all 100 entries in it. What I currently have is (all the way to 1, in 1 big column) 100 -> 100% (A) 99 -> 99% (A) 98 -> 98% (A) 97 -> 97% (A) 96 -> 96% (A) 95 -> 95% (A) 94 -> 94% (A) 93 -> 93% (A) 92 -> 92% (A) 91 -> 91% (A) 90 -> 90% (A) 89 -> 89% (B) 88 -> 88% (B) 87 -> 87% (B) In essence, the table will be generated with 100 entries, still going down the page, but if the user selects 5 columns then the 100 entries needs to be broken down into 5 columns. So it should look like this 100 -> 100% (A) 93 -> 93% (A) continued on till 1 99 -> 99% (A) 92 -> 92% (A) 98 -> 98% (A) 91 -> 91% (A) 97 -> 97% (A) 90 -> 90% (A) 96 -> 96% (A) 89 -> 89% (B) 95 -> 95% (A) 88 -> 88% (B) 94 -> 94% (A) 87 -> 84% (B) $max_points is the score for the exam. $precision is if the user wants decimals or not $percent is the percent of the users score. for ($i = $max_points; $i > 0; $i--) { $p = ($i / $max_points) * 100; if ($precision == 0) $percent = round($p, 0); else $percent = number_format($p, 2, '.', ''); echo "<tr>"; echo "<td class='points'>$i ⇒</td>"; echo "<td class='pctGrade'>$percent%</td>"; if ($percent >= $a_min) echo "<td class='ltrGrade'>(A)</td>"; elseif ($percent >= $b_min) echo "<td class='ltrGrade'>(B)</td>"; elseif ($percent >= $c_min) echo "<td class='ltrGrade'>(C)</td>"; elseif ($percent >= $d_min) echo "<td class='ltrGrade'>(D)</td>"; else echo "<td class='ltrGrade'>(F)</td>"; echo "</tr>"; echo "</th>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/305043-dynamic-table-by-column/ Share on other sites More sharing options...
Barand Posted September 19, 2017 Share Posted September 19, 2017 (edited) A couple of simple approaches 1) Output each entry into a <div> with left float. Set the width of the divs to just a little less than an Nth of the page width (N= no of columns.) or 2) Put the entries into an array then use array_chunk() to give a chunk for each row. Loop through the chunks outputting each into a row. Edited September 19, 2017 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/305043-dynamic-table-by-column/#findComment-1551570 Share on other sites More sharing options...
dalecosp Posted September 19, 2017 Share Posted September 19, 2017 And a 3rd approach: implement a counter and check its modulus against the desired number of columns ...Older-school, hackish, should work though. Quote Link to comment https://forums.phpfreaks.com/topic/305043-dynamic-table-by-column/#findComment-1551572 Share on other sites More sharing options...
Solution Sepodati Posted September 19, 2017 Solution Share Posted September 19, 2017 (edited) Focusing on the math here, instead of counting from 100 to 1, you're going to count in two loops, based on the $columns value the user wants and the numbers of $rows (100/$columns) that are necessary. So if you have 3 columns, you'll have 33.333 rows, which you round up to 34 rows. With 5 columns, you have 20 rows. Some of the final cells in the final row may be empty, since the columns may not evenly divide into 100. Your outer "row" loop ($r) is going to count from 100 down to (100-$rows). Your inner "column" loop ($c) is going to count from 0 to ($columns - 1). For each column loop, you output the values related to ($r - ($c * $rows)). So the first time, you're showing (100 - (0 * 34)) = 100 in the first cell, (100 - (1 * 34)) = 66 in the second column and (100 - (2 * 34)) = 32 in the third column. You'll notice that should be 33, so you'll need to play with the math here as you sort this out. Probably need a +1 in some places and remember the rounded-up and rounded-down $rows value. If ($r - ($c * $rows) < 1, then output a blank cell. Combine those two loops and you output 34 * 3 cells, with some of the last ones being blank, when the user requests 3 columns. There are probably much easier ways to do this with some fancy CSS or something, but from a purely math standpoint in determining rows & columns from arbitrary user input, that's how I'd do it. -John Edited September 19, 2017 by Sepodati 2 Quote Link to comment https://forums.phpfreaks.com/topic/305043-dynamic-table-by-column/#findComment-1551573 Share on other sites More sharing options...
Alcain Posted September 19, 2017 Author Share Posted September 19, 2017 Thank you, everyone, for the input. I found Sepodati's answer to be the easiest to implement. I have the code working perfectly now. Quote Link to comment https://forums.phpfreaks.com/topic/305043-dynamic-table-by-column/#findComment-1551576 Share on other sites More sharing options...
Sepodati Posted September 19, 2017 Share Posted September 19, 2017 (edited) Sounds like I gave too many details, then! Edited September 19, 2017 by Sepodati 1 Quote Link to comment https://forums.phpfreaks.com/topic/305043-dynamic-table-by-column/#findComment-1551579 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.