Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004
    Posts
    2

    Unanswered: Displaying SQL Result By Row Using PHP

    I would like to display list of numbers from a database and format it in such a specific way.

    The result will be in a table with 3 columns and separate by 5 records in a column. Example is shown below :-

    ---------------------------
    | 001 | 002 | 003 |
    | 004 | 005 | 006 |
    | 007 | 008 | 009 |
    | 010 | 011 | 012 |
    | 013 | 014 | 015 |
    ---------------------------
    | 016 | 017 | 018 |
    | 019 | 020 | 021 |
    | 022 | 023 | 024 |
    | 025 | 026 | 027 |
    | 028 |----------------|
    ----------

    Is there way to do it in PHP? I am quite new to PHP and any help is greatly appreciated.

  2. #2
    Join Date
    Jan 2004
    Posts
    6
    Change the host, user, pass, db, tableName and colName according to what you have.
    amtPerRow and amtPerBlk can be modified to what you want and it *should* render the table in the expected way.

    PHP Code:

    <?php

    $host 
    "";                //Host Name
    $user "";                    //MySQL User Name
    $pass "";                //MySQL Password
    $db "";                    //Database to connect to
    $tableName "";                //Table to be used
    $colName "";                    //Column to be printed

    mysql_connect($host,$user,$pass);
    mysql_select_db($db);

    $qr mysql_query("SELECT * FROM `$tableName`");
    $rows mysql_num_rows($qr);

    $amtPerRow 3;                    //Number of Columns in a Block
    $amtPerBlk 5;                    //Number of Rows in a Block
    $blkSize $amtPerRow $amtPerBlk;        //Total in a Block

    $blks = ($rows - ($rows $blkSize)) / ($blkSize);
    if (
    $rows $blkSize != 0)
        
    $blks++;

    for (
    $bCounter 1$bCounter <= $blks$bCounter++)
        {
        echo 
    "<table border='1'>";
        for (
    $rCounter 1$rCounter <= $amtPerBlk$rCounter++)
            {
            echo 
    "<tr>";
            for (
    $cCounter 1$cCounter <= $amtPerRow$cCounter++)
                {
                echo 
    "<td>";
                if (
    $resultRow mysql_fetch_assoc($qr))
                    {
                    echo 
    $resultRow["$colName"];
                    }
                else
                    {
                    echo 
    "&nbsp;";
                    }
                echo 
    "</td>";
                }
            echo 
    "</tr>";
            }
        echo 
    "</table>";
        echo 
    "<br>";
        }

    mysql_close();

    ?>
    Last edited by killerkooki; 01-20-04 at 00:39.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •