Solved

Dynamic HTML Table

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP: Query an Sqlite3 Datatbase return in JSON 6 52
How would I do a website like this? 5 60
tutorial for ebay api 3 35
PHP AJAX JSON 2 61
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.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

803 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