homepage Welcome to WebmasterWorld Guest from 54.237.98.229
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
echo results to td 6 in a tr
retrieve values from mysql and format html table
abushahin

5+ Year Member



 
Msg#: 4419184 posted 9:55 pm on Feb 18, 2012 (gmt 0)

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!

 

nyteshade



 
Msg#: 4419184 posted 4:46 pm on Feb 19, 2012 (gmt 0)

abushahin:

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.

abushahin

5+ Year Member



 
Msg#: 4419184 posted 12:45 pm on Feb 20, 2012 (gmt 0)

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.

rocknbil

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



 
Msg#: 4419184 posted 4:56 pm on Feb 20, 2012 (gmt 0)

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

$options=array();
$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>
<tr><td><strong>Options:</strong></td><td>

<?php
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";
}
?>
</tr>
<!-- continue as above, with colspan="$colspan" -->

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved