Jump to content
  • Advertisement
Sign in to follow this  
ArchG

[web] Building A Table Column by Column

This topic is 4855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.. I have some information organized in a database table like this
Name   |   Column    |    Position
-------------------------------------
Pete        1                1            
Chuck       2                1       
Peter       2                2         
Paul        2                3
Mary        1                2         
Clary       1                3
And I need to make an html table that is ordered by those positions in those columns as above. An example of what my html table would have to be with the dat a above would be::

Pete   |   Chuck   |
Mary   |   Peter   |
Clary  |   Paul    |
I'm wondering what would be the most efficient way to get the data out of database table, and organize it into the dynamic html table. Right now I'm thinking going to read everything out of the database table with column=1,then build the html table...then read everything with column=2 out of the dbase table, and modify the DOM with javascript to build the other column of the html table. 1. Is this a good way to go? 2. What other options do I have to build it like this? 3. What is the fastest way to do it? This table is rather large, and i'm concerned about the page load time. As always.., ratings to the helping hands ;-) Thanks, ArchG

Share this post


Link to post
Share on other sites
Advertisement
Can you show us visually how it should look? The most "proper" way of doing it would be:


<ul>
<li>Pete</li>
<li>Chuck</li>
<li>Mary</li>
<li>Peter</li>
<li>Clary</li>
<li>Paul</li>
</ul>




Then, using CSS:


ul {
display: block;
list-style-type: none;
margin: 0;
padding: 0;
}

li {
display: block;
width: 300px;
float: left;
}




You might have to play with the CSS a bit more, especially if you need some exact look. This style would only work if you don't really need exact column and positions.

If you cannot get it to work with your intended look, then I'd go with simple HTML tables. I would not use JS to manipulate the DOM; that really seems like a bad abuse to me. With PHP, a few lines of code would do what you want (with tables). Regardless of the language, it seems like a trivial server side task.

Share this post


Link to post
Share on other sites
Thanks for your reply konForce,

What I need it to look like is just a basic 2 column html table..the problem is getting the information out of the database and into the table, with everything in the correct order.

Also if someone could think of a better way to store that data, that would be awesome too.

When I get home, if no one has replied i'll try to explain what I mean a little better. I'm in a bit of a hurry now...Sorry for the sloppy explination
Thanks Again
ArchG

Share this post


Link to post
Share on other sites
Well, here is an example using PHP/PEAR::DB:


<?php
$table = array();
$rows = 0; // number of rows
$cols = 0; // number of cols

foreach($db->getAll("SELECT * FROM mytable") as $row)
{
$table[$row['Position']][$row['Column']] = $row;
if ($row['Position'] > $rows) $rows = $row['Position'];
if ($row['Column'] > $cols) $cols = $row['Column'];
}

print '<table>';
for ($i=0; $i<$rows; ++$i)
{
print '<tr>';
for ($j=0; $j<$cols; ++$j)
{
print '<td>';
if (isset($table[$i][$j]))
{
print $table[$i][$j]['Name'];
}
print '</td>';

}
print '</tr>';
}
print '</table>';
?>


If there can never be a gap in the rows, then you could do it without the lookup $table array. Also, if there is a lot of data that is usually static, I'd cache the HTML table to disk/memory and load it instead of building it every page view.

Share this post


Link to post
Share on other sites
How about this, using ORDER BY?


$num_columns = 2; // You could figure this out with an SQL query as well
$result = mysql_query('SELECT * FROM table ORDER BY position, column');

echo '<table>';
while ($row = mysql_fetchrow($result))
{
if ($row['column'] == 1)
{
echo '<tr>';
}
echo '<td>'.$row['name'].'</td>';
if ($row['column'] == $num_columns)
{
echo '</tr>';
}
}
echo '</table>';

Share this post


Link to post
Share on other sites
Sander's method is what I was refering to if there are no gaps in the rows. His example will mess up if not everything is sequential. You could still work around that without needing a $table lookup, but the code will be be more complex. Basically, you'd be doing this:


print "<table>";
$r = next_record_from_ordered_set()
for ($y = 1; $y <= $total_rows; ++$y)
{
print "<tr>";
for ($x = 1; $x <= $total_cols; ++$x)
{
print "<td>";
if (is_array($r) && $r['row'] == $y && $r['col'] == $x)
{
print $r['name'];
$r = next_record_from_ordered_set();
}
print "</td>";
}
print "</tr>";
}
print "</table>";


That method assumes the record set has been ordered by row,col and there is no duplicate col/row information.

Share this post


Link to post
Share on other sites
SELECT name, column, position FROM mytable ORDER BY position DESC, column DESC;

That will get them out in the right order, add PHP checks that writes out & nbsp ; or something if a name is missing in one column and that also advances to the next row if that missing name was for column 2 (if you know your dataset always will have integrity you needn't though).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!