Solved

Dynamic HTML Table

Posted on 2003-12-08
3
309 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

708 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

15 Experts available now in Live!

Get 1:1 Help Now