Jump to content

Creating Dynamic Tables


iwato

Recommended Posts

BACKGROUND: The below function creates a neatly formatted, two-column dynamic table with a caption, column headings, grouped-row headings, an observation counter, and a footnote. The input array is organized as an associative array with first-level associative nested sub-arrays. The keys of the principal array appear only once for each subarray in the first column. The key-value pairs of each nested sub-array appear as paired rows in the second column; the key of each pair appears in the top row, and the value of each pair appears in the bottom row.As expected, with the appearance of each new subarray a new grouped-row heading appears in the first column.PROBLEM: Everything works until I get to the final row of the table. No matter what I do the footnote appears at the bottom of the second column and not at the bottom of the first column, where I would like it to appear. Any suggestions toward rectifying the problem? I have tried everything within my own power of imagination.

	function create_2Col_DoubleRow_Table($array, $id = '', $caption='', $col_headings='') {		$cnst_count = 0;		$cnst_total = 0;		echo '<div class=\'table\'><table id=\'' . $id . '\'>';		echo '<tr><th class=\'left_col_head\'>', $col_headings[0], '</th><th class=\'right_col_head\'>', $col_headings[1], '</th></tr>';		foreach ($array as $key => $value) {			foreach ($value as $param_key) {				$cnst_count++;			}			list($param_key, $param_value) = each($value);				echo '<tr><th rowspan=\'', ($cnst_count * 2), '\'>', $key, '</th><td>', $param_key, '</td></tr>';				echo '<tr><td>', $param_value, '</td></tr>';			while (list($param_key, $param_value) = each($value)) {				echo '<tr><td>', $param_key, '</td></tr>';				echo '<tr><td>', $param_value, '</td></tr>';			}			$cnst_total =+ $cnst_count;		}		echo '<tr><td class=\'table_footer\'>total number of constants: ', $cnst_total, '</td><td></td></tr>';  		echo '<caption>' . $caption . '</caption>';		echo '</table></div>';	}

Roddy

Link to comment
Share on other sites

Maybe posting a portion of the generated HTML would help.
As it is a dynamically generated table, showing you the output in the context of this forum would be of little use. So, I will provide you with sample data, and let you run it yourself.The only thing that is missing is the CSS, but I cannot see how it is relevant to the problem, as it is only used to manipulate font attributes and table and text alignment.
$caption = 'The PECL-HTTP Constants by Category of Constant'; $col_headings = array('constant category', 'individual constant'); $http_cnsts_arr = array( 'Constants usable with http_support()' => array( 'HTTP_SUPPORT' => '(integer) querying for this constant will always return TRUE', 'HTTP_SUPPORT_REQUESTS' => '(integer) whether support to issue HTTP requests is given, ie. libcurl support was compiled in', 'HTTP_SUPPORT_MAGICMIME' => '(integer) whether support to guess the Content-Type of HTTP messages is given, ie. libmagic support was compiled in', 'HTTP_SUPPORT_ENCODINGS' => '(integer) whether support for zlib encodings is given, ie. libz support was compiled in', 'HTTP_SUPPORT_SSLREQUESTS' => '(integer) whether support to issue HTTP requests over SSL is given, ie. linked libcurl was built with SSL support'), 'Constants usable with http_parse_params()' => array( 'HTTP_PARAMS_ALLOW_COMMA' => '(integer) allow commands additionally to semicolons as separator', 'HTTP_PARAMS_ALLOW_FAILURE' => '(integer) continue parsing after an error occurred', 'HTTP_PARAMS_RAISE_ERROR' => '(integer) raise PHP warnings on parse errors', 'HTTP_PARAMS_DEFAULT' => '(integer) all three values above, bitwise or ed'));
Roddy
Link to comment
Share on other sites

As it is a dynamically generated table, showing you the output in the context of this forum would be of little use. So, I will provide you with sample data, and let you run it yourself.
What's the difference if you post the output from the sample data, or if I take the sample data and run your code myself to see the output?I'm only concerned with the structure of the table. If we can see how PHP is putting the table together perhaps we can figure out how to correct the problem.
Link to comment
Share on other sites

What's the difference if you post the output from the sample data, or if I take the sample data and run your code myself to see the output?I'm only concerned with the structure of the table. If we can see how PHP is putting the table together perhaps we can figure out how to correct the problem.
exactly. one of the most common way's to debug PHP generated HTML is by viewing the HTML.
Link to comment
Share on other sites

$cnst_count is never initialized, so it's automatically created at the scope of the function. The problem is you keep increasing it within the inner foreach loop. You need to instead give it a proper value, where it's resetted each time.The easiest way is to simply elimiate the foreach loop, and use count() instead, like:

function create_2Col_DoubleRow_Table($array, $id = '', $caption='', $col_headings='') {	$cnst_count = 0;	$cnst_total = 0;	echo '<div class=\'table\'><table id=\'' . $id . '\'>';	echo '<tr><th class=\'left_col_head\'>', $col_headings[0], '</th><th class=\'right_col_head\'>', $col_headings[1], '</th></tr>';	foreach ($array as $key => $value) {		$cnst_count = count($value);				list($param_key, $param_value) = each($value);		echo '<tr><th rowspan=\'', ($cnst_count * 2), '\'>', $key, '</th><td>', $param_key, '</td></tr>';		echo '<tr><td>', $param_value, '</td></tr>';		while (list($param_key, $param_value) = each($value)) {			echo '<tr><td>', $param_key, '</td></tr>';			echo '<tr><td>', $param_value, '</td></tr>';		}		$cnst_total += $cnst_count;	}	echo '<tr><td class=\'table_footer\'>total number of constants: ', $cnst_total, '</td><td></td></tr>';	echo '<caption>' . $caption . '</caption>';	echo '</table></div>';}

(I've also fixed the "=+"... it should be "+=")ShadowMage and thescientist had a valid point about also posting the output source code, as the evidence was there - the rowspan of the second group was "18", which is obviously not what you really need (8), and is therefore the reason the last cell also gets on there. Looking a little closely at how it's generated showed that it's actually 10 (the number of rows from before) plus 8 (the number of rows to be used now).

Link to comment
Share on other sites

Let me begin by stating that your suggestion about eliminating the additional foreach() statement with the count() function was very helpful and somewhat magically resolved the problem, as the footnote now appears in the left-column where I had originally intended it to appear.

$cnst_count is never initialized, so it's automatically created at the scope of the function. The problem is you keep increasing it within the inner foreach loop. You need to instead give it a proper value, where it's resetted each time.
Actually, the counting aspect of the footnote worked well. It was only the placement of the footnote that failed. It was stuck in the right column for some reason.
(I've also fixed the "=+"... it should be "+=")
Originally I had written it, as you have written it, but it did not count properly. So, I switched the order, and it began counting properly. As a result, I thought no more of it, and looked elsewhere for the resolution of my problem. I will never do it again. Sometimes, "cheating" pays off. Many times it does not and creates more trouble. This appears to be what happened here.Many thanks!Roddy
Link to comment
Share on other sites

What's the difference if you post the output from the sample data, or if I take the sample data and run your code myself to see the output?
The difference is that the structure of the table is lost when I insert it into this box. This is the reason that I provided the detailed background statement to help you imagine what the table looked like. Next time I will upload a TIFF file with an image of the HTML output.I just noticed the function for uploading image files in the menu bar.Roddy
Link to comment
Share on other sites

Actually, the counting aspect of the footnote worked well. It was only the placement of the footnote that failed. It was stuck in the right column for some reason.
The counting (of the group) did not work well. It only appeared like that because of the "=+" (which essentially gave $cnst_total the same value as $cnst_count... the "+" there is like a plus in front of a positive number - it has no effect). And the wrong posisiton was a consequence of the wrong counting - because of the extra count in the second group, there were enough positions for the last row, and even additional ones, if you had any.The difference before and after the fix is more evident if you have 3 or more groups.
Link to comment
Share on other sites

The difference is that the structure of the table is lost when I insert it into this box. This is the reason that I provided the detailed background statement to help you imagine what the table looked like. Next time I will upload a TIFF file with an image of the HTML output.I just noticed the function for uploading image files in the menu bar.
FWIW, I wasn't concerned with what it looked like, I wanted to see the markup that produced it. As boen_robot pointed out, the colspan was incorrect, which can only be seen in the markup.
Link to comment
Share on other sites

FWIW, I wasn't concerned with what it looked like, I wanted to see the markup that produced it.
Ah, you mean the source code of the output, but this you could have gotten with the data I provided. In any case, thanks for the suggestion. I did not think to look there. It may have triggered a new thought or two.
As boen_robot pointed out, the colspan was incorrect, which can only be seen in the markup.
This is confusing as my code contains only the rowspan attribute. In any case, the problem has been solved, and I am now, and hopefully others the wiser for it.Roddy
Link to comment
Share on other sites

The counting (of the group) did not work well. It only appeared like that because of the "=+" (which essentially gave $cnst_total the same value as $cnst_count... the "+" there is like a plus in front of a positive number - it has no effect).
This makes good sense.
And the wrong posiiton was a consequence of the wrong counting - because of the extra count in the second group, there were enough positions for the last row, and even additional ones, if you had any.
In the belief that my count was proper I could not have realized that this was the source of the error.
The difference before and after the fix is more evident if you have 3 or more groups.
All 110 observations are now perfectly aligned and accounted for.Once again, many thanks, boen_robot.You are on top!Roddy
Link to comment
Share on other sites

Ah, you mean the source code of the output, but this you could have gotten with the data I provided.
For future reference, with virtually any question that people have about stuff like this we'll ask for the relevant PHP code and then the output (HTML) from it. This isn't because we're lazy and don't want to run your code, it's because we're trying to walk you through the debugging process instead of doing it for you. If you would have posted the HTML output then people would have made comments about how it looks vs. how it's supposed to look, etc, and you would have understood that checking the HTML output of the problem script (debugging from the top down instead of the bottom up) is often the quickest way to find the problem.There's also the notion that you want to make it as easy as possible for people to answer your questions (i.e. by not requiring they run their own code), but the reason we take this approach is because we're trying to teach people how to do this stuff, not just do it for them. Most of us, anyway... there are still a few people who offer help by posting large blocks of code without explanation.
Link to comment
Share on other sites

If you would have posted the HTML output then people would have made comments about how it looks vs. how it's supposed to look, etc, and you would have understood that checking the HTML output of the problem script (debugging from the top down instead of the bottom up) is often the quickest way to find the problem.
If the following statement requires further explanation, please let me know in a private email. Can it really be interpreted to mean anything other than an acknowledgment of the wisdom of what was requested, but not forthcoming?
Ah, you mean the source code of the output, but this you could have gotten with the data I provided. In any case, thanks for the suggestion. I did not think to look there. It may have triggered a new thought or two.
Roddy
Link to comment
Share on other sites

When dealing with PHP, the output from version to version may differ. That's not the case with your particular code, but still - it could be. Posting the generated HTML, as seen from your run of the code lets us first check where the output is not what you expect it to be, and then determine why, which could (in theory; in practice, that's rarely the case) be due to different PHP versions or a particular PHP bug on that one platform.It's about eliminating as much of the possible causes as possible... it's like the case about validating vs. non validating HTML code - an invalid code with a proper doctype on doesn't always throw off the parser into guessing stuff incorrectly, and will not always trigger quirks mode. But to eliminate the possibility of parser errors or sudden quirks mode switch, most people on this board (myself included) will assist in validation first, and only then tackle the actual problem.

Link to comment
Share on other sites

When dealing with PHP, the output from version to version may differ. That's not the case with your particular code, but still - it could be. Posting the generated HTML, as seen from your run of the code lets us first check where the output is not what you expect it to be, and then determine why, which could (in theory; in practice, that's rarely the case) be due to different PHP versions or a particular PHP bug on that one platform.It's about eliminating as much of the possible causes as possible... it's like the case about validating vs. non validating HTML code - an invalid code with a proper doctype on doesn't always throw off the parser into guessing stuff incorrectly, and will not always trigger quirks mode. But to eliminate the possibility of parser errors or sudden quirks mode switch, most people on this board (myself included) will assist in validation first, and only then tackle the actual problem.
Got it. First HTML code validation, then more PHP.As always, you're on top, boen-robot.Roddy
Link to comment
Share on other sites

Can it really be interpreted to mean anything other than an acknowledgment of the wisdom of what was requested, but not forthcoming?
Maybe it's too early, but frankly I don't even know what you mean by that. I'm just trying to help you ask your questions as effectively as possible to get the most effective answers quickly. The key to getting good help is to make it easy for people to help you, and posting the output of your code instead of asking people to run your code in their environment is one way to make it easy.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...