How to Display results in columns with PHP & MYSQL

Discussion in 'PHP' started by B R Bhatt, Oct 3, 2007.

  1. #1
    Hello Friends, Please try to solve my problem and edit below code if possible.

    I need to display result in multiple columns from database . like this.....

    PHP Program ASP Programming Java Scripts Programming
    C Programming C++ Programming Java Programming
    C# Programming Low Level Program Sever side programmin




    <?PHP

    include 'library/config.php';
    include 'library/opendb.php';

    echo "<table border='1' width='100%' cellspacing='0' cellpadding='0' ";
    echo "<tr >";
    echo "<td align='center'>";
    echo "<b>ID</b>";
    echo"</td>";
    echo “</tr>”;

    $query=mysql_query("select *from table ") or die("Error Occured");

    while($row=mysql_fetch_array($query))
    {
    echo "<tr>";
    echo "<td align='center' width='90'>";
    echo $row['name'];
    echo"</td>";
    echo “</tr>”;
    }
    echo “</table>”;

    ?>

    Above code display all result in single columns. like..
    PHP Programming
    ASP Programming
    Java Scripts Programming
    C Programming
    C++ Programming
    Java Programming
    C# Programming
    Low Level Program
    Sever side programmin

    I need to display in multipale columns
    Please try to give ur logic.
     
    B R Bhatt, Oct 3, 2007 IP
  2. ravish_83

    ravish_83 Peon

    Messages:
    221
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    how many columns per row ..?
     
    ravish_83, Oct 4, 2007 IP
  3. ravish_83

    ravish_83 Peon

    Messages:
    221
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    let's assume you want to show n records per row

    then the equation will be like this

    
    $perline = n;
    $set = $perline;
    echo "<table>";
    while($row=mysql_fetch_array($query))
    {
    
           if(($set%$perline) == 0){
              echo  "<tr>";
           }
           
           echo "<td align='center' >";
           echo $row['name'];
           echo "</td>";
    
           if((($set+1)%$perline) == 0){
    	       echo "</tr>";
           }
           $set = $set+1;
    	
    }
    echo "</table>";
    PHP:
     
    ravish_83, Oct 4, 2007 IP
  4. B R Bhatt

    B R Bhatt Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks. I have solved my problem



     
    B R Bhatt, Oct 8, 2007 IP