Solved

Dynamic HTML Table

Posted on 2003-12-08
3
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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…

739 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