Sunday, April 27, 2008

Display the dyanamic column in php/mysql

$query_cat = "SELECT * FROM "table" WHERE";
    $query_cat .= " "filtered_row" = "parameter"";

    $cat = mysql_query($query_cat) or die(mysql_error());
    $total = mysql_num_rows($cat);

    while($row = mysql_fetch_array($cat))
    {
// -- This array will hold your data,
// -- I used it to hold a title, an id and an author
// -- but it can be extended based on your own needs
        $items[] = array(1 => $row['title'], 2 => $row['id'], 3 => $row['author']);
    }
        // Default # of Columns
        $numcols = 4;

        // Number of Items
        $numitems = count($items);

        // Number of Rows
        $numrows = ceil($numitems/$numcols);

        echo '<table width="100%">';
        for ($row=1; $row <= $numrows; $row++)
        {
            $cell = 0;
            echo ' <tr>'."\n";
            for ($col=1; $col <= $numcols; $col++)
            {
            echo '  <td width="'.round(100/$numcols).'%">'."\n";

            if ($col===1)
            {
                $cell += $row;
                print $items[$cell - 1][1]; /*  $items[$cell - 1][1] to display title  $items[$cell - 1][2] etc... */
            }
            else {
                $cell += $numrows;
                print $items[$cell - 1][1];
            }
            echo '  </td>'."\n";
            }
            echo ' </tr>'."\n";
        }
        echo '</table>';

No comments:

Post a Comment