RE: Sepating MySQL result set into html tables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Might I suggest you count the fields and divide it by the cols you want to display? 
Example
$forest = mysql_query("SELECT * FROM your_table");
$gump = mysql_num_fields($forest);

Because I know my table contains 15 rows I can do this.
$tulip = floor($gump /5);
I know how many fields to display before a row break.
May seems extremely simple but works very well for me.



Hello everybody,
    I have a mysql result set that I want to print out in an html table. 
But some times it gets so big that I will probably need to separate it 
in to multiple ones. I wrote a function that separates the result set 
into three ones like this:

function showTable($columnNames,$data,$alignment,$wrap) {
   
    $limit = round(count($data)/3,0);
   
    $align = 
array('l'=>'align="left"','c'=>'align="center"','r'=>'align="right"');

    $table = "<table class='inner'>";


    $table .= "<table><tr><td><table class='inner'>";

    $table .= "<tr>";
    foreach ($columnNames as $col_name){
        $table .= "<th>".$col_name."</th>";
    }
    $table .= "</tr>\n";

    foreach ($data as $j => $row) {

        if(($j % 2) == 0){
            $table .= "<tr class=even>";
        } else {
            $table .= "<tr class=odd>";
        }
        for($i=0;$i<count($row);$i++){

            $table .= "<td ".$wrap." 
".$align[$alignment[$i]].">".$row[$i]."</td>";

        }

        $table .= "</tr>\n";

        if (($j == $limit-1) || ($j == (2*$limit)-1)) {
            $table .= "</table></td><td><table>";
            $table .= "<tr>";
            foreach ($columnNames as $col_name){
                $table .= "<th>".$col_name."</th>";
            }
            $table .= "</tr>\n";
        }
    }
    $table .= '</table></td></tr>';
    $table .= "</table>\n";
    print $table;
}


But I really need to tell the function in how many tables I need the 
result set to be separated. How can I do this?

What can I put instead of this for 3 tables: (($j == $limit-1) || ($j == 
(2*$limit)-1))

I can't find the algorithm although I think I have done this before but 
I can't remember how.

-- 
Thodoris


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux