Jump to content

Dynamic table by column


Go to solution Solved by Sepodati,

Recommended Posts

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);
				$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>";
				echo "<td class='ltrGrade'>(F)</td>";
		echo "</tr>";
		echo "</th>";
	echo "</table>"; 
Link to comment
Share on other sites

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.)




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 by Barand
  • Like 1
Link to comment
Share on other sites

  • Solution

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.



Edited by Sepodati
  • Like 2
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.