Solved

Dynamic HTML Table

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 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