Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

echo results to td 6 in a tr

retrieve values from mysql and format html table



9:55 pm on Feb 18, 2012 (gmt 0)

5+ Year Member

hello, I need some help in formating a table dynamically i have a db table that has values which are created into checkboxes which looks terrible when displayed i.e.

all in a line one after another is there a way to get the results from the db and create say a table with 6 tds in a tr?

any suggestions appreciated!


4:46 pm on Feb 19, 2012 (gmt 0)

5+ Year Member


I use HTML tables to reflect tabular data from databases, client objects like javascript objects and arrays, or just hand-keyed tabular data.

Someone correct me if I'm wrong, but it sounds as though you are trying to use a table in lieu of a form.

You may find it easier and more in keeping with standards if you use a form (data-driven form I suspect since you may be populating the checkbox elements with values from a db query) by building the HTML server-side.

By doing it this way you'll have access to the form native input elements and attribute type='submit' for your check boxes.


12:45 pm on Feb 20, 2012 (gmt 0)

5+ Year Member

thanks for your input but you have totally lost me,
anyway maybe i wasnt clear in what im asking for.
I need to print from a while loop, usually while its looping you can put in a paragraph, td etc. I need a way to print in multiple tds in a line if thats possible.

Someone correct me if I'm wrong, but it sounds as though you are trying to use a table in lieu of a form.

Im using a table to format form fields.

You may find it easier and more in keeping with standards if you use a form..

The HTML table model allows authors to arrange data -- text, preformatted text, images, links, forms, form fields, other tables, etc. -- into rows and columns of cells.

[w3.org ]

Appreciate your reply though.


4:56 pm on Feb 20, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Read the whole section from that document.

Tables should not be used purely as a means to layout document content as this may present problems when rendering to non-visual media.

Tables are for tabular data, not layout. At any rate - table layout argument aside - if something looks bad in this browser or that, first stop is to use a full document type [webmasterworld.com], then validate [validator.w3.org] the document (i.e., your script output) so you are rendering in Standards Compliance Mode.

Most likely you have a mismatch of rows, columns, colspans. If this is the only place in the table you split out the td's, you'll have to devise some method of pre-counting them so you can apply the appropriate colspans to all other cells (and without two queries, one to count, one to output.) Something like . . .

$query = "select option_key,option_value from options order by option_key asc";
$result = mysql_query($query) or die("Cannot get options " . mysql_error());
while ($row = mysql_fetch_array($result)) {
$options[$row[0]] = $row[1];

(This would also be one of the good uses of mysql_num_rows, but for various reasons you may not want to do that.)

This will give you an array,

$options = Array (
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value4'
// etc

count the keys,

$count = count(array_keys($options));

And now you have the colspan matching this set of options.

<tr><td><label for="myname">My Name:</label></td><td colspan="<?php echo $count; ?>"> <input type="text" name="myname" id="myname" value=""></td></tr>

foreach ($options as $key => $value) {
echo "<td><input type=\"checkbox\" name=\"$key\" id=\"$key\" value="$value\"";
// Checkbox values don't have to match. They will be in post or they won't
if (isset($_POST[$key])) { echo ' checked'; }
// or checked="checked" for XHTML
echo "> <label for=\"$key\">$value</label></td>\n";
<!-- continue as above, with colspan="$colspan" -->

Featured Threads

Hot Threads This Week

Hot Threads This Month