Solved

Dynamic HTML Table

Posted on 2003-12-08
3
310 Views
Last Modified: 2013-11-19
I think this one is pretty easy.  I have an html table that I want to dynamically create (I can do that easily) but where I'm having trouble is: I have a resultset with 7 rows, I want to create the table so that it has 3 cells across, then put the next 3 cells in the next row, then finish off the table with the final result and put the necessary blank cells in to finish the table.

example:
<table>
<tr>
    <td>Resultset_Row 1</td>
    <td>Resultset_Row 2</td>
    <td>Resultset_Row 3</td>
</tr>
<tr>
    <td>Resultset_Row 4</td>
    <td>Resultset_Row 5</td>
    <td>Resultset_Row 6</td>
</tr>
<tr>
    <td>Resultset_Row 7</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
</table>

Thanks,
0
Comment
Question by:srobia
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
shmert earned 75 total points
ID: 9897840
You could do something like this:

$result = mysql_query("select whatever from mytable where x=123");
while ($row = mysql_fetch_row($result) {
    $row = array_pad($row, 9, '&nbsp;');
    echo '<tr>';
    foreach($row AS $key=>$value) {
        echo '<td>' . $value . '</td>';
        if (($key==2 || $key==5) {
            echo '</tr><tr>';
        }
    }
    echo '</tr>';
}
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 50 total points
ID: 9898586
Just to make shmert's answer a touch more scalable, I would replace the ($key==2 || $key==5) with ($key % 3 == 2) or with (($key+1) % 3 == 0), whichever you find more readable.  You could also replace the 9 in the array_pad line with something like round(mysql_num_rows / 3,0) * 3, if that matches your need.

Something doesn't look quite right with shmert's answer (aside from some unmatched parens).  I assume the query returns 7 separate rows, and we are printing one value from each row.  shmert's answer would work for a query returning 1 row, printing 7 values from that one row.  Will it work if we replace the top three lines with:

$myValues = array();
$result = mysql_query("select whatever from mytable where x=123");
while ($row = mysql_fetch_row($result)) {
    array_push($myValues);
}
$row = array_pad($myValues, 9, '&nbsp;');
...

(adjust curly brackets to match)
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 9898594
sorry, that bit about replacing the 9 should probably be (mysql_num_rows / 3 + 1, 0) * 3
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now